refactor: Update timestamp handling for nanosecond precision in stock data queries

This commit is contained in:
Bobby Abellana (aider) 2025-02-06 22:27:40 -08:00
parent 207991b9f0
commit 319f18d882
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -65,15 +65,15 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
# Get 5-minute bars and resample them to the desired interval
query = f"""
SELECT
fromUnixTimestamp(intDiv({date_col}, 300) * 300) as interval_start,
fromUnixTimestamp(intDiv(window_start/1000000000, 300) * 300) as interval_start,
min(open) as open,
max(high) as high,
min(low) as low,
argMax(close, {date_col}) as close,
argMax(close, window_start) as close,
sum(volume) as volume
FROM stock_db.{table}
WHERE ticker = '{ticker}'
AND {date_col} BETWEEN toUnixTimestamp('{start_date}') AND toUnixTimestamp('{end_date}')
AND window_start/1000000000 BETWEEN toUnixTimestamp('{start_date}') AND toUnixTimestamp('{end_date}')
GROUP BY interval_start
ORDER BY interval_start ASC
"""
@ -153,14 +153,14 @@ def get_valid_tickers(min_price: float, max_price: float, min_volume: int, inter
if interval != "daily":
# Now verify these tickers have intraday data
market_open = int(datetime.combine(today, datetime.strptime("09:30", "%H:%M").time()).timestamp())
market_close = int(datetime.combine(today, datetime.strptime("16:00", "%H:%M").time()).timestamp())
market_open = int(datetime.combine(today, datetime.strptime("09:30", "%H:%M").time()).timestamp() * 1000000000)
market_close = int(datetime.combine(today, datetime.strptime("16:00", "%H:%M").time()).timestamp() * 1000000000)
intraday_query = f"""
SELECT DISTINCT ticker
FROM stock_db.stock_prices
WHERE ticker IN ({','.join([f"'{t}'" for t in tickers])})
AND window_start BETWEEN {market_open - 86400} AND {market_close} -- Include last 24 hours
AND window_start BETWEEN {market_open - 86400000000000} AND {market_close} -- Include last 24 hours
GROUP BY ticker
HAVING count() >= 10 -- Ensure we have enough data points
"""