refactor: Improve trade display sorting and exit price handling

This commit is contained in:
Bobby (aider) 2025-02-13 08:54:04 -08:00
parent f85299b518
commit b1b22ac026

View File

@ -306,8 +306,8 @@ def trading_journal_page():
# Display trades grouped by position
for position_id, trades in positions.items():
# Sort trades by entry_date
trades.sort(key=lambda x: x['entry_date'])
# Sort trades by entry_date and put sells after buys
trades.sort(key=lambda x: (x['entry_date'], 0 if x.get('direction') == 'buy' else 1))
first_trade = trades[0]
with st.expander(f"{first_trade['ticker']} - Position {position_id}"):
@ -317,10 +317,15 @@ def trading_journal_page():
col1, col2 = st.columns(2)
with col1:
try:
entry_price = float(trade['entry_price'])
if trade['direction'] == 'sell':
st.metric("Exit Price", f"${entry_price:.2f}")
# For sells, use entry_price as the exit price
exit_price = float(trade['entry_price'])
st.metric("Exit Price", f"${exit_price:.2f}")
# Update the trade's exit_price and exit_date
trade['exit_price'] = exit_price
trade['exit_date'] = trade['entry_date']
else:
entry_price = float(trade['entry_price'])
st.metric("Entry Price", f"${entry_price:.2f}")
except (ValueError, TypeError):
price_label = "Exit Price" if trade['direction'] == 'sell' else "Entry Price"
@ -338,14 +343,6 @@ def trading_journal_page():
st.metric("Target", f"${trade['target_price']:.2f}")
if trade.get('stop_loss'):
st.metric("Stop Loss", f"${trade['stop_loss']:.2f}")
if trade.get('exit_price'):
try:
exit_price = float(trade['exit_price'])
st.metric("Exit Price", f"${exit_price:.2f}")
st.metric("Exit Date", format_datetime(trade['exit_date']))
except (ValueError, TypeError):
st.metric("Exit Price", "N/A")
st.metric("Exit Date", format_datetime(trade['exit_date']))
if trade.get('strategy'):
st.text(f"Strategy: {trade['strategy']}")