fix: Update database operations for clickhouse-connect compatibility

This commit is contained in:
Bobby (aider) 2025-02-10 09:39:35 -08:00
parent e906c63893
commit 4ebfcfebd1

View File

@ -60,7 +60,7 @@ def create_trades_table():
) ENGINE = MergeTree()
ORDER BY (position_id, id, entry_date)
"""
client.execute(query)
client.command(query)
def generate_id() -> int:
"""Generate a unique ID for the trade"""
@ -132,7 +132,7 @@ def add_trade(trade: TradeEntry):
{f"'{trade.notes}'" if trade.notes else 'NULL'}
)
"""
client.execute(query)
client.command(query)
def update_trade_exit(trade_id: int, exit_price: float, exit_date: datetime,
followed_rules: bool, exit_reason: str, notes: Optional[str] = None):
@ -147,14 +147,14 @@ def update_trade_exit(trade_id: int, exit_price: float, exit_date: datetime,
notes = {f"'{notes}'" if notes else 'notes'}
WHERE id = {trade_id}
"""
client.execute(query)
client.command(query)
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"
result = client.execute(query)
columns = ['id', 'ticker', 'entry_date', 'shares', 'entry_price', 'target_price',
'stop_loss', 'strategy', 'followed_rules', 'entry_reason', 'exit_price',
result = client.query(query).result_rows
columns = ['id', 'position_id', 'ticker', 'entry_date', 'shares', 'entry_price', 'target_price',
'stop_loss', 'strategy', 'order_type', 'followed_rules', 'entry_reason', 'exit_price',
'exit_date', 'exit_reason', 'notes', 'created_at']
return [dict(zip(columns, row)) for row in result]
@ -166,9 +166,9 @@ def get_trade_history(limit: int = 50):
ORDER BY exit_date DESC
LIMIT {limit}
"""
result = client.execute(query)
columns = ['id', 'ticker', 'entry_date', 'shares', 'entry_price', 'target_price',
'stop_loss', 'strategy', 'followed_rules', 'entry_reason', 'exit_price',
result = client.query(query).result_rows
columns = ['id', 'position_id', 'ticker', 'entry_date', 'shares', 'entry_price', 'target_price',
'stop_loss', 'strategy', 'order_type', 'followed_rules', 'entry_reason', 'exit_price',
'exit_date', 'exit_reason', 'notes', 'created_at']
return [dict(zip(columns, row)) for row in result]