fix: Handle NaN ATR values and adjust lookback period in signal check
This commit is contained in:
parent
96b4d951b4
commit
0a7d1afcde
@ -44,11 +44,14 @@ def check_entry_signal(df: pd.DataFrame, selected_patterns: list = None) -> list
|
||||
Returns:
|
||||
list: List of tuples (signal, date, signal_data) for each signal found
|
||||
"""
|
||||
if len(df) < 5: # Need at least 5 bars for pattern recognition
|
||||
if len(df) < 14: # Need at least 14 bars for ATR calculation
|
||||
return []
|
||||
|
||||
signals = []
|
||||
|
||||
# Calculate ATR first
|
||||
atr = talib.ATR(df['high'].values, df['low'].values, df['close'].values, timeperiod=14)
|
||||
|
||||
# Use selected patterns or all patterns if none selected
|
||||
patterns_to_scan = {k: v for k, v in CANDLESTICK_PATTERNS.items()
|
||||
if selected_patterns is None or k in selected_patterns}
|
||||
@ -64,7 +67,7 @@ def check_entry_signal(df: pd.DataFrame, selected_patterns: list = None) -> list
|
||||
)
|
||||
|
||||
# Look for signals across all candles
|
||||
for i in range(len(df)):
|
||||
for i in range(14, len(df)): # Start after ATR lookback period
|
||||
found_patterns = []
|
||||
|
||||
for pattern_name, pattern_values in pattern_signals.items():
|
||||
@ -76,11 +79,15 @@ def check_entry_signal(df: pd.DataFrame, selected_patterns: list = None) -> list
|
||||
# Calculate basic target and stop levels using recent price action
|
||||
current_price = df.iloc[i]['close']
|
||||
recent_low = df.iloc[max(0, i-5):i+1]['low'].min() # Look back 5 bars for stop
|
||||
atr = talib.ATR(df['high'].values, df['low'].values, df['close'].values, timeperiod=14)
|
||||
|
||||
# Use 2x ATR for target and 1x ATR for stop
|
||||
target_price = current_price + (2 * atr[i] if not pd.isna(atr[i]) else current_price * 0.02)
|
||||
stop_loss = max(recent_low, current_price - (atr[i] if not pd.isna(atr[i]) else current_price * 0.01))
|
||||
# Default to percentage-based calculations
|
||||
atr_value = atr[i]
|
||||
if pd.isna(atr_value):
|
||||
target_price = current_price * 1.02 # 2% target
|
||||
stop_loss = current_price * 0.99 # 1% stop
|
||||
else:
|
||||
target_price = current_price + (2 * atr_value)
|
||||
stop_loss = max(recent_low, current_price - atr_value)
|
||||
|
||||
signal_data = {
|
||||
'price': current_price,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user