refactor: Update date range and intraday data query for accurate stock scanning
This commit is contained in:
parent
62b63388a8
commit
843f78851f
@ -118,14 +118,13 @@ def get_valid_tickers(min_price: float, max_price: float, min_volume: int, inter
|
|||||||
|
|
||||||
# Get the most recent trading day
|
# Get the most recent trading day
|
||||||
today = datetime.now().date()
|
today = datetime.now().date()
|
||||||
if today.weekday() >= 5: # If it's weekend
|
yesterday = today - timedelta(days=1)
|
||||||
today = today - timedelta(days=today.weekday() - 4) # Go back to Friday
|
|
||||||
|
|
||||||
# First get valid tickers from daily data
|
# First get valid tickers from daily data
|
||||||
daily_query = f"""
|
daily_query = f"""
|
||||||
SELECT DISTINCT ticker
|
SELECT DISTINCT ticker
|
||||||
FROM stock_db.stock_prices_daily
|
FROM stock_db.stock_prices_daily
|
||||||
WHERE date = '{today}'
|
WHERE date = '{yesterday}'
|
||||||
AND close BETWEEN {min_price} AND {max_price}
|
AND close BETWEEN {min_price} AND {max_price}
|
||||||
AND volume >= {min_volume}
|
AND volume >= {min_volume}
|
||||||
ORDER BY ticker ASC
|
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)
|
result = client.query(daily_query)
|
||||||
tickers = [row[0] for row in result.result_rows]
|
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")
|
print(f"\nFound {len(tickers)} stocks matching price and volume criteria")
|
||||||
|
|
||||||
if interval != "daily":
|
if interval != "daily":
|
||||||
# Now verify these tickers have intraday data
|
# Now verify these tickers have intraday data
|
||||||
market_open = int(datetime.combine(today, datetime.strptime("09:30", "%H:%M").time()).timestamp() * 1000000000)
|
# Convert to Unix timestamp in nanoseconds
|
||||||
market_close = int(datetime.combine(today, datetime.strptime("16:00", "%H:%M").time()).timestamp() * 1000000000)
|
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"""
|
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 - 86400000000000} AND {market_close} -- Include last 24 hours
|
AND window_start BETWEEN {start_ts} AND {end_ts}
|
||||||
GROUP BY ticker
|
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)
|
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
|
# Get user's preferred interval
|
||||||
interval = get_interval_choice()
|
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()
|
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
|
# Get valid tickers
|
||||||
print("\nFetching qualified stocks...")
|
print("\nFetching qualified stocks...")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user