refactor: Enhance get_valid_tickers to check intraday data across yesterday and today

This commit is contained in:
Bobby Abellana (aider) 2025-02-06 22:15:56 -08:00
parent ccf6671f4b
commit 1d04986a07
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -127,16 +127,23 @@ def get_valid_tickers(min_price: float, max_price: float, min_volume: int, inter
print(f"\nFound {len(tickers)} stocks matching price and volume criteria") print(f"\nFound {len(tickers)} stocks matching price and volume criteria")
if interval != "daily": if interval != "daily":
# Now verify these tickers have intraday data today # Now verify these tickers have intraday data in the last trading day
# Get the most recent trading day's timestamp range
today = datetime.now().date() today = datetime.now().date()
market_open = int(datetime.combine(today, datetime.strptime("09:30", "%H:%M").time()).timestamp()) yesterday_open = int(datetime.combine(yesterday, datetime.strptime("09:30", "%H:%M").time()).timestamp())
market_close = int(datetime.combine(today, datetime.strptime("16:00", "%H:%M").time()).timestamp()) yesterday_close = int(datetime.combine(yesterday, datetime.strptime("16:00", "%H:%M").time()).timestamp())
today_open = int(datetime.combine(today, datetime.strptime("09:30", "%H:%M").time()).timestamp())
today_close = int(datetime.combine(today, datetime.strptime("16:00", "%H:%M").time()).timestamp())
intraday_query = f""" intraday_query = f"""
SELECT DISTINCT ticker SELECT DISTINCT ticker
FROM stock_db.stock_prices FROM stock_db.stock_prices
WHERE ticker IN ({','.join([f"'{t}'" for t in tickers])}) WHERE ticker IN ({','.join([f"'{t}'" for t in tickers])})
AND window_start BETWEEN {market_open} AND {market_close} AND (
(window_start BETWEEN {yesterday_open} AND {yesterday_close})
OR
(window_start BETWEEN {today_open} AND {today_close})
)
GROUP BY ticker GROUP BY ticker
HAVING count() >= 10 -- Ensure we have enough data points HAVING count() >= 10 -- Ensure we have enough data points
""" """