fix: Improve error handling and data resampling in indicator calculation

This commit is contained in:
Bobby (aider) 2025-02-08 11:52:28 -08:00
parent 41380c3638
commit 7ba3f07ad4

View File

@ -138,29 +138,48 @@ def run_atr_ema_target_scanner(min_price: float, max_price: float, min_volume: i
# Make a clean copy for indicator calculation # Make a clean copy for indicator calculation
calc_df = df.copy() calc_df = df.copy()
# Make a clean copy for indicator calculation
calc_df = df.copy()
# Resample to daily data if we have intraday
if interval != "daily":
calc_df = calc_df.resample('D', on='date').agg({
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum'
}).dropna()
# Ensure minimum data points after resampling
if len(calc_df) < 50:
print(f"⚠️ {ticker}: Insufficient data points after resampling ({len(calc_df)})")
continue
# Calculate indicator with validated data # Calculate indicator with validated data
try: try:
results = indicator.calculate(calc_df) results = indicator.calculate(calc_df)
if results.empty: if results.empty:
print(f"⚠️ {ticker}: No valid indicator results") print(f"⚠️ {ticker}: No valid indicator results")
continue continue
# Get the last two rows for signal checking
last_row = results.iloc[-1]
prev_row = results.iloc[-2]
bullish_entry = (
last_row["close"] < last_row["ema"] and
prev_row["close"] <= prev_row["lower_band"] and
last_row["close"] > prev_row["close"]
)
except Exception as e: except Exception as e:
print(f"⚠️ {ticker}: Error calculating indicator:") print(f"⚠️ {ticker}: Error in indicator calculation:")
print(f"Error details: {str(e)}") print(f"Error details: {str(e)}")
print(f"Data shape: {calc_df.shape}") print(f"Data shape: {calc_df.shape}")
print(f"Sample data:\n{calc_df.head()}") print(f"Sample data:\n{calc_df.head()}")
continue continue
results = indicator.calculate(df)
last_row = results.iloc[-1]
prev_row = results.iloc[-2]
bullish_entry = (
last_row["close"] < last_row["ema"] and
prev_row["close"] <= prev_row["lower_band"] and
last_row["close"] > prev_row["close"]
)
if bullish_entry: if bullish_entry:
entry_price = last_row["close"] entry_price = last_row["close"]
target_1 = entry_price * 1.10 # 10% profit target_1 = entry_price * 1.10 # 10% profit