From aedd96eef4196a978d1d6229542e8170765473f9 Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Thu, 13 Feb 2025 10:34:46 -0800 Subject: [PATCH] refactor: Simplify open trades summary query calculation --- src/trading/journal.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/trading/journal.py b/src/trading/journal.py index 98cd77d..29c936b 100644 --- a/src/trading/journal.py +++ b/src/trading/journal.py @@ -478,15 +478,8 @@ def get_open_trades_summary() -> dict: query = """ SELECT ticker, - sum(CASE WHEN direction = 'buy' THEN shares - WHEN direction = 'sell' THEN -shares - ELSE 0 END) as total_shares, - sum(CASE WHEN direction = 'buy' THEN shares * entry_price - WHEN direction = 'sell' THEN -shares * entry_price - ELSE 0 END) / - abs(sum(CASE WHEN direction = 'buy' THEN shares - WHEN direction = 'sell' THEN -shares - ELSE 0 END)) as avg_entry_price, + sum(shares) as total_shares, + avg(entry_price) as avg_entry_price, min(entry_date) as first_entry, max(entry_date) as last_entry, count() as num_orders, @@ -494,7 +487,6 @@ 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