refactor: Update plot_trade_history to only show realized P/L from closed trades
This commit is contained in:
parent
85c8f97cc5
commit
b1240cd582
@ -77,7 +77,7 @@ def format_datetime(dt):
|
||||
return ''
|
||||
|
||||
def plot_trade_history(trades):
|
||||
"""Create a P/L chart using Plotly"""
|
||||
"""Create a P/L chart using Plotly showing only realized profits/losses"""
|
||||
if not trades:
|
||||
return None
|
||||
|
||||
@ -87,8 +87,10 @@ def plot_trade_history(trades):
|
||||
cumulative_pnl = 0
|
||||
|
||||
for trade in trades:
|
||||
# Skip trades without numeric exit prices
|
||||
if not trade['exit_price'] or not isinstance(trade['exit_price'], (int, float)):
|
||||
# Only include trades with both entry and exit prices (closed trades)
|
||||
if (not trade.get('exit_price') or not trade.get('entry_price') or
|
||||
not isinstance(trade['exit_price'], (int, float)) or
|
||||
not isinstance(trade['entry_price'], (int, float))):
|
||||
continue
|
||||
|
||||
try:
|
||||
@ -97,10 +99,15 @@ def plot_trade_history(trades):
|
||||
entry_price = float(trade['entry_price'])
|
||||
shares = float(trade['shares'])
|
||||
|
||||
# Calculate realized P/L for this trade
|
||||
trade_pnl = (exit_price - entry_price) * shares
|
||||
cumulative_pnl += trade_pnl
|
||||
dates.append(trade['exit_date'])
|
||||
pnl.append(cumulative_pnl)
|
||||
|
||||
# Use exit_date for realized P/L
|
||||
if trade.get('exit_date'):
|
||||
dates.append(trade['exit_date'])
|
||||
pnl.append(cumulative_pnl)
|
||||
|
||||
except (ValueError, TypeError):
|
||||
# Skip trades where conversion to float fails
|
||||
continue
|
||||
@ -112,15 +119,15 @@ def plot_trade_history(trades):
|
||||
fig = go.Figure()
|
||||
fig.add_trace(
|
||||
go.Scatter(x=dates, y=pnl, mode='lines+markers',
|
||||
name='Cumulative P/L',
|
||||
name='Realized P/L',
|
||||
line=dict(color='blue'),
|
||||
hovertemplate='Date: %{x}<br>P/L: $%{y:.2f}<extra></extra>')
|
||||
hovertemplate='Date: %{x}<br>Realized P/L: $%{y:.2f}<extra></extra>')
|
||||
)
|
||||
|
||||
fig.update_layout(
|
||||
title='Cumulative Profit/Loss Over Time',
|
||||
title='Cumulative Realized Profit/Loss Over Time',
|
||||
xaxis_title='Date',
|
||||
yaxis_title='Cumulative P/L ($)',
|
||||
yaxis_title='Realized P/L ($)',
|
||||
hovermode='x unified'
|
||||
)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user