refactor: Improve overall P/L calculation with realized and paper P/L

This commit is contained in:
Bobby (aider) 2025-02-13 13:22:14 -08:00
parent d7834d78f5
commit 4be088c0b2

View File

@ -283,12 +283,20 @@ def trading_journal_page():
f"{weekly_pl_pct:.2f}% | {weekly_metrics['weekly_trade_count']} trades") f"{weekly_pl_pct:.2f}% | {weekly_metrics['weekly_trade_count']} trades")
with col3: with col3:
# Overall P/L metrics (from trade history) # Overall P/L metrics (combining realized P/L from history and current paper P/L)
total_pl = sum(trade.get('position_pl', 0) for trade in trade_history) if trade_history else 0 realized_pl = 0
total_pl_pct = (total_pl / total_portfolio_value * 100) if total_portfolio_value > 0 else 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
overall_pl_pct = (overall_pl / total_portfolio_value * 100) if total_portfolio_value > 0 else 0
st.metric("Overall P/L", st.metric("Overall P/L",
f"${total_pl:,.2f}", f"${overall_pl:,.2f}",
f"{total_pl_pct:.2f}% since inception") f"{overall_pl_pct:.2f}% since inception")
total_paper_pl = 0 total_paper_pl = 0
invested_value = 0 invested_value = 0