fix: Correct trade direction processing in trading journal

This commit is contained in:
Bobby (aider) 2025-02-13 09:26:18 -08:00
parent 94671119f2
commit f162cdb91b

View File

@ -21,18 +21,11 @@ def calculate_position_performance(trades):
shares = float(trade['shares']) shares = float(trade['shares'])
price = float(trade['entry_price']) price = float(trade['entry_price'])
# First check explicit direction field # Check direction field directly
is_buy = True if trade.get('direction', '').lower() == 'buy':
if isinstance(trade.get('direction'), str) and trade['direction'].lower() == 'sell':
is_buy = False
# Fallback to order_type if direction is corrupted
elif trade.get('order_type', '').lower() == 'sell':
is_buy = False
if is_buy:
total_bought += shares total_bought += shares
total_cost += shares * price total_cost += shares * price
else: elif trade.get('direction', '').lower() == 'sell':
total_sold += shares total_sold += shares
total_proceeds += shares * price total_proceeds += shares * price
except (ValueError, TypeError) as e: except (ValueError, TypeError) as e:
@ -414,7 +407,7 @@ def trading_journal_page():
# Show buy trades # Show buy trades
st.subheader("Buy Orders") st.subheader("Buy Orders")
for trade in trades: for trade in trades:
if trade.get('order_type', '').lower() != 'sell': # If not sell, assume buy if trade.get('direction', '').lower() == 'buy': # Check direction field instead of order_type
col1, col2, col3 = st.columns(3) col1, col2, col3 = st.columns(3)
with col1: with col1:
st.text(f"Date: {format_datetime(trade['entry_date'])}") st.text(f"Date: {format_datetime(trade['entry_date'])}")
@ -434,7 +427,7 @@ def trading_journal_page():
# Show sell trades # Show sell trades
st.subheader("Sell Orders") st.subheader("Sell Orders")
for trade in trades: for trade in trades:
if trade.get('order_type', '').lower() == 'sell': if trade.get('direction', '').lower() == 'sell': # Check direction field instead of order_type
col1, col2 = st.columns(2) col1, col2 = st.columns(2)
with col1: with col1:
st.text(f"Date: {format_datetime(trade['entry_date'])}") st.text(f"Date: {format_datetime(trade['entry_date'])}")