refactor: Improve date handling in get_stock_data for daily and intraday queries

This commit is contained in:
Bobby Abellana (aider) 2025-02-06 22:02:22 -08:00
parent 9c440f08da
commit 971a5dd4a1
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -35,25 +35,22 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
if interval == "daily": if interval == "daily":
table = "stock_prices_daily" table = "stock_prices_daily"
date_col = "date" date_col = "date"
query = f"""
SELECT
{date_col} as date,
open,
high,
low,
close,
volume
FROM stock_db.{table}
WHERE ticker = '{ticker}'
AND {date_col} BETWEEN '{start_date.date()}' AND '{end_date.date()}'
ORDER BY date ASC
"""
else: else:
table = "stock_prices" table = "stock_prices"
date_col = "window_start" date_col = "window_start"
query = f"""
SELECT
{date_col} as date,
open,
high,
low,
close,
volume
FROM stock_db.{table}
WHERE ticker = '{ticker}'
AND {date_col} BETWEEN '{start_date.date()}' AND '{end_date.date()}'
"""
# Add interval filtering for intraday data
if interval != "daily":
minutes_map = { minutes_map = {
"5min": 5, "5min": 5,
"15min": 15, "15min": 15,
@ -61,9 +58,21 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
"1hour": 60 "1hour": 60
} }
minutes = minutes_map[interval] minutes = minutes_map[interval]
query += f" AND (toMinute(fromUnixTimestamp(window_start)) % {minutes}) = 0"
query = f"""
query += " ORDER BY date ASC" SELECT
fromUnixTimestamp({date_col}) as date,
open,
high,
low,
close,
volume
FROM stock_db.{table}
WHERE ticker = '{ticker}'
AND {date_col} BETWEEN toUnixTimestamp('{start_date.date()}') AND toUnixTimestamp('{end_date.date()}')
AND (toMinute(fromUnixTimestamp({date_col})) % {minutes}) = 0
ORDER BY date ASC
"""
try: try:
result = client.query(query) result = client.query(query)