feat: Add docstring and lowercase trade direction in add_trade function

This commit is contained in:
Bobby (aider) 2025-02-13 09:13:03 -08:00
parent a3eae62be3
commit b6117c84f2

View File

@ -346,12 +346,13 @@ def get_order_type() -> Optional[str]:
print("Invalid choice. Please try again.")
def add_trade(trade: TradeEntry):
"""Add a new trade to the database"""
with create_client() as client:
query = f"""
INSERT INTO stock_db.trades (
id, position_id, ticker, entry_date, shares, entry_price, target_price, stop_loss,
strategy, order_type, direction, followed_rules, entry_reason, exit_price, exit_date,
exit_reason, notes
id, position_id, ticker, entry_date, shares, entry_price, target_price,
stop_loss, strategy, order_type, direction, followed_rules, entry_reason,
exit_price, exit_date, exit_reason, notes
) VALUES (
{generate_id()},
'{trade.position_id}',
@ -363,11 +364,11 @@ def add_trade(trade: TradeEntry):
{trade.stop_loss if trade.stop_loss else 'NULL'},
{f"'{trade.strategy}'" if trade.strategy else 'NULL'},
'{trade.order_type}',
'{trade.direction}',
'{trade.direction.lower()}', # Ensure direction is stored in lowercase
{1 if trade.followed_rules else 0},
{f"'{trade.entry_reason}'" if trade.entry_reason else 'NULL'},
{trade.exit_price if trade.exit_price else 'NULL'},
{f"'{trade.exit_date.strftime('%Y-%m-%d %H:%M:%S')}'" if trade.exit_date else 'NULL'},
NULL,
NULL,
{f"'{trade.exit_reason}'" if trade.exit_reason else 'NULL'},
{f"'{trade.notes}'" if trade.notes else 'NULL'}
)