From 4bd9bd8c42a79c2fb73e1e57f4828b7bb56e765e Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Thu, 13 Feb 2025 11:14:28 -0800 Subject: [PATCH] fix: Adjust open trades summary query to handle sell orders and filter zero total shares --- src/trading/journal.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/trading/journal.py b/src/trading/journal.py index 28b85dd..ca0241b 100644 --- a/src/trading/journal.py +++ b/src/trading/journal.py @@ -475,7 +475,10 @@ def get_open_trades_summary() -> dict: query = """ SELECT ticker, - sum(shares) as total_shares, + sum(CASE + WHEN direction = 'sell' THEN -shares + ELSE shares + END) as total_shares, avg(entry_price) as avg_entry_price, min(entry_date) as first_entry, max(entry_date) as last_entry, @@ -484,6 +487,7 @@ def get_open_trades_summary() -> dict: FROM stock_db.trades WHERE exit_price IS NULL GROUP BY ticker + HAVING total_shares > 0 ORDER BY ticker ASC """ print(f"Executing summary query: {query}") # Debug