refactor: Modify ClickHouse database operations in journal.py for HTTP client compatibility
This commit is contained in:
parent
d6b6a289ed
commit
15aadbe3da
@ -36,67 +36,95 @@ class TradeEntry:
|
|||||||
|
|
||||||
def create_trades_table():
|
def create_trades_table():
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
client.execute("""
|
query = """
|
||||||
CREATE TABLE IF NOT EXISTS trades (
|
CREATE TABLE IF NOT EXISTS stock_db.trades (
|
||||||
id SERIAL PRIMARY KEY,
|
id UInt32,
|
||||||
ticker VARCHAR(10) NOT NULL,
|
ticker String,
|
||||||
entry_date TIMESTAMP NOT NULL,
|
entry_date DateTime,
|
||||||
shares INTEGER NOT NULL,
|
shares UInt32,
|
||||||
entry_price DECIMAL(10,2) NOT NULL,
|
entry_price Float64,
|
||||||
target_price DECIMAL(10,2) NOT NULL,
|
target_price Float64,
|
||||||
stop_loss DECIMAL(10,2) NOT NULL,
|
stop_loss Float64,
|
||||||
strategy VARCHAR(50) NOT NULL,
|
strategy String,
|
||||||
followed_rules BOOLEAN,
|
followed_rules Nullable(UInt8),
|
||||||
entry_reason TEXT,
|
entry_reason Nullable(String),
|
||||||
exit_price DECIMAL(10,2),
|
exit_price Nullable(Float64),
|
||||||
exit_date TIMESTAMP,
|
exit_date Nullable(DateTime),
|
||||||
exit_reason TEXT,
|
exit_reason Nullable(String),
|
||||||
notes TEXT,
|
notes Nullable(String),
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at DateTime DEFAULT now()
|
||||||
)
|
) ENGINE = MergeTree()
|
||||||
""")
|
ORDER BY (id, entry_date)
|
||||||
|
"""
|
||||||
|
client.execute(query)
|
||||||
|
|
||||||
|
def generate_id() -> int:
|
||||||
|
"""Generate a unique ID for the trade"""
|
||||||
|
return int(datetime.now().timestamp() * 1000)
|
||||||
|
|
||||||
def add_trade(trade: TradeEntry):
|
def add_trade(trade: TradeEntry):
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
client.execute("""
|
query = f"""
|
||||||
INSERT INTO trades (
|
INSERT INTO stock_db.trades (
|
||||||
ticker, entry_date, shares, entry_price, target_price, stop_loss,
|
id, ticker, entry_date, shares, entry_price, target_price, stop_loss,
|
||||||
strategy, followed_rules, entry_reason, exit_price, exit_date,
|
strategy, followed_rules, entry_reason, exit_price, exit_date,
|
||||||
exit_reason, notes
|
exit_reason, notes
|
||||||
) VALUES (
|
) VALUES (
|
||||||
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
|
{generate_id()},
|
||||||
|
'{trade.ticker}',
|
||||||
|
'{trade.entry_date.strftime('%Y-%m-%d %H:%M:%S')}',
|
||||||
|
{trade.shares},
|
||||||
|
{trade.entry_price},
|
||||||
|
{trade.target_price},
|
||||||
|
{trade.stop_loss},
|
||||||
|
'{trade.strategy}',
|
||||||
|
{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'},
|
||||||
|
{f"'{trade.exit_reason}'" if trade.exit_reason else 'NULL'},
|
||||||
|
{f"'{trade.notes}'" if trade.notes else 'NULL'}
|
||||||
)
|
)
|
||||||
""", (
|
"""
|
||||||
trade.ticker, trade.entry_date, trade.shares, trade.entry_price,
|
client.execute(query)
|
||||||
trade.target_price, trade.stop_loss, trade.strategy, trade.followed_rules,
|
|
||||||
trade.entry_reason, trade.exit_price, trade.exit_date, trade.exit_reason,
|
|
||||||
trade.notes
|
|
||||||
))
|
|
||||||
|
|
||||||
def update_trade_exit(trade_id: int, exit_price: float, exit_date: datetime,
|
def update_trade_exit(trade_id: int, exit_price: float, exit_date: datetime,
|
||||||
followed_rules: bool, exit_reason: str, notes: Optional[str] = None):
|
followed_rules: bool, exit_reason: str, notes: Optional[str] = None):
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
client.execute("""
|
query = f"""
|
||||||
UPDATE trades
|
ALTER TABLE stock_db.trades
|
||||||
SET exit_price = %s, exit_date = %s, followed_rules = %s,
|
UPDATE
|
||||||
exit_reason = %s, notes = CASE WHEN %s IS NULL THEN notes ELSE %s END
|
exit_price = {exit_price},
|
||||||
WHERE id = %s
|
exit_date = '{exit_date.strftime('%Y-%m-%d %H:%M:%S')}',
|
||||||
""", (exit_price, exit_date, followed_rules, exit_reason, notes, notes, trade_id))
|
followed_rules = {1 if followed_rules else 0},
|
||||||
|
exit_reason = {f"'{exit_reason}'" if exit_reason else 'NULL'},
|
||||||
|
notes = {f"'{notes}'" if notes else 'notes'}
|
||||||
|
WHERE id = {trade_id}
|
||||||
|
"""
|
||||||
|
client.execute(query)
|
||||||
|
|
||||||
def get_open_trades():
|
def get_open_trades():
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
client.execute("SELECT * FROM trades WHERE exit_price IS NULL ORDER BY entry_date DESC")
|
query = "SELECT * FROM stock_db.trades WHERE exit_price IS NULL ORDER BY entry_date DESC"
|
||||||
return client.fetchall()
|
result = client.execute(query)
|
||||||
|
columns = ['id', 'ticker', 'entry_date', 'shares', 'entry_price', 'target_price',
|
||||||
|
'stop_loss', 'strategy', 'followed_rules', 'entry_reason', 'exit_price',
|
||||||
|
'exit_date', 'exit_reason', 'notes', 'created_at']
|
||||||
|
return [dict(zip(columns, row)) for row in result]
|
||||||
|
|
||||||
def get_trade_history(limit: int = 50):
|
def get_trade_history(limit: int = 50):
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
client.execute("""
|
query = f"""
|
||||||
SELECT * FROM trades
|
SELECT * FROM stock_db.trades
|
||||||
WHERE exit_price IS NOT NULL
|
WHERE exit_price IS NOT NULL
|
||||||
ORDER BY exit_date DESC
|
ORDER BY exit_date DESC
|
||||||
LIMIT %s
|
LIMIT {limit}
|
||||||
""", (limit,))
|
"""
|
||||||
return client.fetchall()
|
result = client.execute(query)
|
||||||
|
columns = ['id', 'ticker', 'entry_date', 'shares', 'entry_price', 'target_price',
|
||||||
|
'stop_loss', 'strategy', 'followed_rules', 'entry_reason', 'exit_price',
|
||||||
|
'exit_date', 'exit_reason', 'notes', 'created_at']
|
||||||
|
return [dict(zip(columns, row)) for row in result]
|
||||||
|
|
||||||
def journal_menu():
|
def journal_menu():
|
||||||
"""Trading journal menu interface"""
|
"""Trading journal menu interface"""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user