fix: correct timestamp handling and add date validation in stock data queries

This commit is contained in:
Bobby (aider) 2025-02-08 11:50:25 -08:00
parent 44beec4b6c
commit 41380c3638

View File

@ -25,7 +25,7 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
# Unified query format # Unified query format
query = f""" query = f"""
SELECT SELECT
toDate(window_start) as date, toDateTime(window_start/1000000000) as date,
open, open,
high, high,
low, low,
@ -34,9 +34,11 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
FROM stock_db.stock_prices FROM stock_db.stock_prices
WHERE ticker = '{ticker}' WHERE ticker = '{ticker}'
AND window_start BETWEEN AND window_start BETWEEN
toUnixTimestamp('{start_date.date()}') * 1000000000 AND {int(start_date.timestamp() * 1e9)} AND
toUnixTimestamp('{end_date.date()}') * 1000000000 {int(end_date.timestamp() * 1e9)}
ORDER BY window_start ASC AND toYear(toDateTime(window_start/1000000000)) <= toYear(now())
AND toYear(toDateTime(window_start/1000000000)) >= (toYear(now()) - 1)
ORDER BY date ASC
""" """
result = client.query(query) result = client.query(query)
@ -47,7 +49,7 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
print(f"⚠️ No daily data for {ticker}, resampling from intraday data") print(f"⚠️ No daily data for {ticker}, resampling from intraday data")
intraday_query = f""" intraday_query = f"""
SELECT SELECT
toStartOfDay(window_start) AS date, toDateTime(window_start/1000000000) as date,
first_value(open) AS open, first_value(open) AS open,
max(high) AS high, max(high) AS high,
min(low) AS low, min(low) AS low,
@ -56,8 +58,10 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
FROM stock_db.stock_prices FROM stock_db.stock_prices
WHERE ticker = '{ticker}' WHERE ticker = '{ticker}'
AND window_start BETWEEN AND window_start BETWEEN
toUnixTimestamp('{start_date.date()}') * 1000000000 AND {int(start_date.timestamp() * 1e9)} AND
toUnixTimestamp('{end_date.date()}') * 1000000000 {int(end_date.timestamp() * 1e9)}
AND toYear(toDateTime(window_start/1000000000)) <= toYear(now())
AND toYear(toDateTime(window_start/1000000000)) >= (toYear(now()) - 1)
GROUP BY date GROUP BY date
ORDER BY date ASC ORDER BY date ASC
""" """