feat: Improve data fetching with wider range and batch validation

This commit is contained in:
Bobby (aider) 2025-02-08 10:57:30 -08:00
parent 114e0f6165
commit 8560214494

View File

@ -11,7 +11,7 @@ def run_atr_ema_target_scanner(min_price: float, max_price: float, min_volume: i
# Set time range
end_date = datetime.now()
start_date = end_date - timedelta(days=1) # Last trading day
start_date = end_date - timedelta(days=30) # Changed from 1 day to 30 days
start_ts = int(start_date.timestamp() * 1000000000)
end_ts = int(end_date.timestamp() * 1000000000)
@ -48,7 +48,11 @@ def run_atr_ema_target_scanner(min_price: float, max_price: float, min_volume: i
print("❌ No stocks found matching criteria.")
return
print(f"\n✅ Found {len(stocks)} stocks matching criteria")
print("\n🔍 Verifying data availability...")
with client.query_stream(f"SELECT ticker FROM stock_db.stock_prices WHERE window_start BETWEEN {start_ts} AND {end_ts} GROUP BY ticker HAVING count() > 50") as stream:
valid_symbols = {row[0] for row in stream.result_rows}
qualified_stocks = [s for s in stocks if s[0] in valid_symbols]
print(f"\n✅ Found {len(qualified_stocks)} stocks with sufficient historical data")
indicator = ThreeATREMAIndicator()
calculator = PositionCalculator(portfolio_size, risk_percentage=1.0, stop_loss_percentage=7.0) if portfolio_size else None
@ -62,16 +66,15 @@ def run_atr_ema_target_scanner(min_price: float, max_price: float, min_volume: i
try:
df = get_stock_data(ticker, start_date, end_date, "1d")
if df.empty or len(df) < 50:
print(f"⚠️ {ticker}: Insufficient historical data ({len(df)} bars)")
df = get_stock_data(ticker, start_date - timedelta(days=30), end_date, "1d") # Try wider range
if df.empty or len(df) < 50:
print(f"⚠️ {ticker}: No valid data in extended timeframe")
continue
if "1d" not in VALID_INTERVALS:
raise ValueError(f"Invalid interval format. Must be one of: {VALID_INTERVALS}")
except Exception as e:
print(f"❌ Data fetch failed for {ticker}: {str(e)}")
print(f"❌ Data fetch failed for {ticker}: {str(e)[:100]}")
continue
results = indicator.calculate(df)
results = indicator.calculate(df).dropna().tail(30) # Use only most recent valid data
last_row = results.iloc[-1]
prev_row = results.iloc[-2]