From 522e889467bc31c043dda90db61e371d6d90a334 Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Thu, 13 Feb 2025 09:04:47 -0800 Subject: [PATCH] refactor: Improve trade direction handling and debug output --- src/pages/journal/trading_journal_page.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/pages/journal/trading_journal_page.py b/src/pages/journal/trading_journal_page.py index ecf1d5f..522a599 100644 --- a/src/pages/journal/trading_journal_page.py +++ b/src/pages/journal/trading_journal_page.py @@ -17,20 +17,19 @@ def calculate_position_performance(trades): total_proceeds = 0 for trade in trades: - # Debug print to see what we're getting - print(f"Processing trade: {trade}") - - # Convert direction to lowercase and strip whitespace - direction = str(trade.get('direction', '')).lower().strip() - try: shares = float(trade['shares']) price = float(trade['entry_price']) - if direction == 'buy': + # Check if this is a buy or sell based on order type and direction + is_buy = True + if isinstance(trade.get('direction'), str) and trade['direction'].lower() == 'sell': + is_buy = False + + if is_buy: total_bought += shares total_cost += shares * price - elif direction == 'sell': + else: total_sold += shares total_proceeds += shares * price except (ValueError, TypeError) as e: @@ -405,7 +404,7 @@ def trading_journal_page(): # Convert direction to lowercase and strip whitespace direction = str(trade.get('direction', '')).lower().strip() - if direction == 'buy': + if isinstance(trade.get('direction'), str) and trade['direction'].lower() == 'buy': col1, col2, col3 = st.columns(3) with col1: st.text(f"Date: {format_datetime(trade['entry_date'])}") @@ -428,7 +427,7 @@ def trading_journal_page(): # Convert direction to lowercase and strip whitespace direction = str(trade.get('direction', '')).lower().strip() - if direction == 'sell': + if isinstance(trade.get('direction'), str) and trade['direction'].lower() == 'sell': col1, col2 = st.columns(2) with col1: st.text(f"Date: {format_datetime(trade['entry_date'])}")