diff --git a/src/pages/journal/trading_journal_page.py b/src/pages/journal/trading_journal_page.py index 6416681..57d9b4e 100644 --- a/src/pages/journal/trading_journal_page.py +++ b/src/pages/journal/trading_journal_page.py @@ -21,7 +21,15 @@ def calculate_position_performance(trades): shares = float(trade['shares']) price = float(trade['entry_price']) - direction = str(trade.get('direction', '')).lower() + # If no direction field, infer from exit_price + if 'direction' not in trade or not trade['direction']: + # If no exit_price, assume it's a buy + if trade.get('exit_price') is None: + direction = 'buy' + else: + direction = 'sell' + else: + direction = str(trade.get('direction', '')).lower() if direction == 'buy': total_bought += shares @@ -29,10 +37,17 @@ def calculate_position_performance(trades): elif direction == 'sell': total_sold += shares total_proceeds += shares * price + + print(f"Processing trade: Direction={direction}, Shares={shares}, Price={price}") # Debug + except (ValueError, TypeError) as e: print(f"Error processing trade: {e}") continue + print(f"Performance calculation results:") # Debug + print(f"Total bought: {total_bought} shares at total cost: ${total_cost}") # Debug + print(f"Total sold: {total_sold} shares with total proceeds: ${total_proceeds}") # Debug + # Avoid division by zero if total_bought == 0: return {