refactor: Update overall P/L calculation to separate realized and unrealized gains
This commit is contained in:
parent
73daf18fc8
commit
735cfdfb65
@ -283,20 +283,33 @@ def trading_journal_page():
|
||||
f"{weekly_pl_pct:.2f}% | {weekly_metrics['weekly_trade_count']} trades")
|
||||
|
||||
with col3:
|
||||
# Overall P/L metrics (combining realized P/L from history and current paper P/L)
|
||||
# Calculate realized P/L from closed trades
|
||||
realized_pl = 0
|
||||
if trade_history:
|
||||
for position_id in set(trade['position_id'] for trade in trade_history):
|
||||
position_trades = [t for t in trade_history if t['position_id'] == position_id]
|
||||
performance = calculate_position_performance(position_trades)
|
||||
realized_pl += performance['realized_pl']
|
||||
|
||||
overall_pl = realized_pl + total_paper_pl
|
||||
for trade in trade_history:
|
||||
if trade.get('exit_price'): # Only count closed trades
|
||||
entry_price = float(trade['entry_price'])
|
||||
exit_price = float(trade['exit_price'])
|
||||
shares = float(trade['shares'])
|
||||
realized_pl += (exit_price - entry_price) * shares
|
||||
|
||||
# Calculate paper P/L from open positions
|
||||
paper_pl = 0
|
||||
if open_summary:
|
||||
for summary in open_summary:
|
||||
ticker = summary['ticker']
|
||||
current_price = current_prices.get(ticker, 0)
|
||||
shares = summary['total_shares']
|
||||
avg_entry = summary['avg_entry_price']
|
||||
paper_pl += (current_price - avg_entry) * shares
|
||||
|
||||
# Calculate overall P/L
|
||||
overall_pl = realized_pl + paper_pl
|
||||
overall_pl_pct = (overall_pl / total_portfolio_value * 100) if total_portfolio_value > 0 else 0
|
||||
|
||||
|
||||
st.metric("Overall P/L",
|
||||
f"${overall_pl:,.2f}",
|
||||
f"{overall_pl_pct:.2f}% since inception")
|
||||
f"{overall_pl_pct:.2f}% (R: ${realized_pl:,.2f} | U: ${paper_pl:,.2f})")
|
||||
|
||||
total_paper_pl = 0
|
||||
invested_value = 0
|
||||
|
||||
Loading…
Reference in New Issue
Block a user