refactor: Update open trades queries to handle empty exit prices and null exit dates

This commit is contained in:
Bobby (aider) 2025-02-13 11:18:00 -08:00
parent 4bd9bd8c42
commit 4196a61b99

View File

@ -485,7 +485,8 @@ def get_open_trades_summary() -> dict:
count() as num_orders, count() as num_orders,
groupArray(position_id) as position_ids groupArray(position_id) as position_ids
FROM stock_db.trades FROM stock_db.trades
WHERE exit_price IS NULL WHERE (exit_price IS NULL OR exit_price = '')
AND (exit_date IS NULL)
GROUP BY ticker GROUP BY ticker
HAVING total_shares > 0 HAVING total_shares > 0
ORDER BY ticker ASC ORDER BY ticker ASC
@ -507,7 +508,13 @@ def get_open_trades_summary() -> dict:
def get_open_trades(): def get_open_trades():
print("\n=== Fetching Open Trades ===") # Debug print("\n=== Fetching Open Trades ===") # Debug
with create_client() as client: with create_client() as client:
query = "SELECT * FROM stock_db.trades WHERE exit_price IS NULL ORDER BY entry_date DESC" query = """
SELECT *
FROM stock_db.trades
WHERE (exit_price IS NULL OR exit_price = '')
AND (exit_date IS NULL)
ORDER BY entry_date DESC
"""
print(f"Executing query: {query}") # Debug print(f"Executing query: {query}") # Debug
try: try:
result = client.query(query).result_rows result = client.query(query).result_rows