diff --git a/src/screener/t_sunnyband.py b/src/screener/t_sunnyband.py index de02bfa..4ceba69 100644 --- a/src/screener/t_sunnyband.py +++ b/src/screener/t_sunnyband.py @@ -118,14 +118,13 @@ def get_valid_tickers(min_price: float, max_price: float, min_volume: int, inter # Get the most recent trading day today = datetime.now().date() - if today.weekday() >= 5: # If it's weekend - today = today - timedelta(days=today.weekday() - 4) # Go back to Friday + yesterday = today - timedelta(days=1) # First get valid tickers from daily data daily_query = f""" SELECT DISTINCT ticker FROM stock_db.stock_prices_daily - WHERE date = '{today}' + WHERE date = '{yesterday}' AND close BETWEEN {min_price} AND {max_price} AND volume >= {min_volume} ORDER BY ticker ASC @@ -135,36 +134,21 @@ def get_valid_tickers(min_price: float, max_price: float, min_volume: int, inter result = client.query(daily_query) tickers = [row[0] for row in result.result_rows] - if not tickers: # If no data for today, try yesterday - yesterday = today - timedelta(days=1) - if yesterday.weekday() >= 5: # If yesterday was weekend - yesterday = yesterday - timedelta(days=yesterday.weekday() - 4) # Go back to Friday - - daily_query = f""" - SELECT DISTINCT ticker - FROM stock_db.stock_prices_daily - WHERE date = '{yesterday}' - AND close BETWEEN {min_price} AND {max_price} - AND volume >= {min_volume} - ORDER BY ticker ASC - """ - result = client.query(daily_query) - tickers = [row[0] for row in result.result_rows] - print(f"\nFound {len(tickers)} stocks matching price and volume criteria") if interval != "daily": # Now verify these tickers have intraday data - 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) + # Convert to Unix timestamp in nanoseconds + start_ts = int(datetime.combine(yesterday, datetime.strptime("09:30", "%H:%M").time()).timestamp() * 1000000000) + end_ts = int(datetime.combine(yesterday, 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 - 86400000000000} AND {market_close} -- Include last 24 hours + AND window_start BETWEEN {start_ts} AND {end_ts} GROUP BY ticker - HAVING count() >= 10 -- Ensure we have enough data points + HAVING count() >= 50 -- Ensure we have enough data points for the indicator """ result = client.query(intraday_query) @@ -292,11 +276,13 @@ def run_sunny_scanner(min_price: float, max_price: float, min_volume: int) -> No # Get user's preferred interval interval = get_interval_choice() - # Set date range to 60 days for proper DMA calculation + # Set date range to look back from current time end_date = datetime.now() - start_date = end_date - timedelta(days=60) + start_date = end_date - timedelta(days=1) # Look at last trading day for signals + lookback_start = end_date - timedelta(days=60) # For DMA calculation - print(f"\nAnalyzing data from {start_date.date()} to {end_date.date()}") + print(f"\nAnalyzing data from {lookback_start.date()} to {end_date.date()}") + print(f"Looking for signals in the last trading day") # Get valid tickers print("\nFetching qualified stocks...")