refactor: Update import paths and add journal_menu function to journal.py

This commit is contained in:
Bobby (aider) 2025-02-10 09:27:13 -08:00
parent 4e5b54c568
commit b887269152

View File

@ -1,8 +1,8 @@
from datetime import datetime from datetime import datetime
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional from typing import Optional
from src.db.db_connection import create_client from db.db_connection import create_client
from src.trading.position_calculator import PositionCalculator from trading.position_calculator import PositionCalculator
@dataclass @dataclass
class TradeEntry: class TradeEntry:
@ -97,3 +97,108 @@ def get_trade_history(limit: int = 50):
LIMIT %s LIMIT %s
""", (limit,)) """, (limit,))
return client.fetchall() return client.fetchall()
def journal_menu():
"""Trading journal menu interface"""
create_trades_table() # Ensure table exists
while True:
print("\nTrading Journal")
print("1. Add New Trade")
print("2. Update Existing Trade")
print("3. View Open Trades")
print("4. View Trade History")
print("5. Return to Main Menu")
choice = input("\nSelect an option (1-5): ")
if choice == "1":
ticker = input("Enter ticker symbol: ").upper()
shares = int(input("Enter number of shares: "))
entry_price = float(input("Enter entry price: "))
target_price = float(input("Enter target price: "))
stop_loss = float(input("Enter stop loss: "))
strategy = input("Enter strategy name: ")
followed_rules = input("Did you follow your rules? (y/n): ").lower() == 'y'
entry_reason = input("Enter entry reason (optional): ") or None
notes = input("Additional notes (optional): ") or None
trade = TradeEntry(
ticker=ticker,
entry_date=datetime.now(),
shares=shares,
entry_price=entry_price,
target_price=target_price,
stop_loss=stop_loss,
strategy=strategy,
followed_rules=followed_rules,
entry_reason=entry_reason,
notes=notes
)
add_trade(trade)
print(f"\nExpected Profit: ${trade.expected_profit_loss:.2f}")
print(f"Maximum Loss: ${trade.max_loss:.2f}")
print("Trade added successfully!")
elif choice == "2":
open_trades = get_open_trades()
if not open_trades:
print("No open trades to update.")
continue
print("\nOpen Trades:")
for trade in open_trades:
print(f"{trade['id']}: {trade['ticker']} - Entered at ${trade['entry_price']}")
trade_id = int(input("\nEnter trade ID to update: "))
exit_price = float(input("Enter exit price: "))
followed_rules = input("Did you follow your rules? (y/n): ").lower() == 'y'
exit_reason = input("Enter exit reason: ")
notes = input("Additional notes (optional): ") or None
update_trade_exit(
trade_id=trade_id,
exit_price=exit_price,
exit_date=datetime.now(),
followed_rules=followed_rules,
exit_reason=exit_reason,
notes=notes
)
print("Trade updated successfully!")
elif choice == "3":
open_trades = get_open_trades()
if not open_trades:
print("No open trades found.")
else:
print("\nOpen Trades:")
for trade in open_trades:
print(f"\nTicker: {trade['ticker']}")
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']}")
elif choice == "4":
history = get_trade_history()
if not history:
print("No trade history found.")
else:
print("\nTrade History:")
for trade in history:
profit_loss = (trade['exit_price'] - trade['entry_price']) * trade['shares'] if trade['exit_price'] else None
print(f"\nTicker: {trade['ticker']}")
print(f"Entry: ${trade['entry_price']} on {trade['entry_date']}")
if trade['exit_price']:
print(f"Exit: ${trade['exit_price']} on {trade['exit_date']}")
print(f"P/L: ${profit_loss:.2f}")
print(f"Strategy: {trade['strategy']}")
if trade['notes']:
print(f"Notes: {trade['notes']}")
print("-" * 40)
elif choice == "5":
break