diff --git a/src/trading/journal.py b/src/trading/journal.py index c20a2f9..dbfd728 100644 --- a/src/trading/journal.py +++ b/src/trading/journal.py @@ -223,6 +223,28 @@ def update_trade_exit(trade_id: int, exit_price: float, exit_date: datetime, """ client.command(query) +def get_open_trades_summary() -> dict: + """Get summary of all open trades grouped by ticker""" + with create_client() as client: + query = """ + SELECT + ticker, + sum(shares) as total_shares, + sum(shares * entry_price) / sum(shares) as avg_entry_price, + min(entry_date) as first_entry, + max(entry_date) as last_entry, + count() as num_orders, + groupArray(position_id) as position_ids + FROM stock_db.trades + WHERE exit_price IS NULL + GROUP BY ticker + ORDER BY ticker ASC + """ + result = client.query(query).result_rows + columns = ['ticker', 'total_shares', 'avg_entry_price', + 'first_entry', 'last_entry', 'num_orders', 'position_ids'] + return [dict(zip(columns, row)) for row in result] + def get_open_trades(): with create_client() as client: query = "SELECT * FROM stock_db.trades WHERE exit_price IS NULL ORDER BY entry_date DESC" @@ -367,18 +389,44 @@ def journal_menu(): elif choice == "3": open_trades = get_open_trades() + open_summary = get_open_trades_summary() + if not open_trades: print("No open trades found.") else: - print("\nOpen Trades:") + print("\n=== Open Trades Summary ===") + for summary in open_summary: + avg_entry = summary['avg_entry_price'] + stop_loss = avg_entry * 0.93 # 7% stop loss + total_value = avg_entry * summary['total_shares'] + max_loss = (avg_entry - stop_loss) * summary['total_shares'] + + print(f"\n{summary['ticker']} Summary:") + print(f"Total Shares: {summary['total_shares']}") + print(f"Average Entry: ${avg_entry:.2f}") + print(f"Total Position Value: ${total_value:.2f}") + print(f"Combined Stop Loss (7%): ${stop_loss:.2f}") + print(f"Maximum Loss at Stop: ${max_loss:.2f}") + print(f"Number of Orders: {summary['num_orders']}") + print(f"Position Duration: {summary['last_entry'] - summary['first_entry']}") + print("-" * 50) + + print("\n=== Individual Trades ===") for trade in open_trades: print(f"\nTicker: {trade['ticker']}") + print(f"Position ID: {trade['position_id']}") print(f"Entry Date: {trade['entry_date']}") print(f"Shares: {trade['shares']}") print(f"Entry Price: ${trade['entry_price']}") print(f"Target: ${trade['target_price']}") print(f"Stop Loss: ${trade['stop_loss']}") print(f"Strategy: {trade['strategy']}") + print(f"Order Type: {trade['order_type']}") + if trade['entry_reason']: + print(f"Entry Reason: {trade['entry_reason']}") + if trade['notes']: + print(f"Notes: {trade['notes']}") + print("-" * 40) elif choice == "4": history = get_trade_history()