fix TA warning
This commit is contained in:
@ -1334,10 +1334,10 @@ class DataProvider:
|
||||
df['psar'] = psar.psar()
|
||||
|
||||
# === MOMENTUM INDICATORS ===
|
||||
# RSI (multiple periods)
|
||||
df['rsi_14'] = ta.momentum.rsi(df['close'], window=14)
|
||||
df['rsi_7'] = ta.momentum.rsi(df['close'], window=7)
|
||||
df['rsi_21'] = ta.momentum.rsi(df['close'], window=21)
|
||||
# RSI (multiple periods) - using our own implementation to avoid ta library deprecation warnings
|
||||
df['rsi_14'] = self._calculate_rsi(df['close'], period=14)
|
||||
df['rsi_7'] = self._calculate_rsi(df['close'], period=7)
|
||||
df['rsi_21'] = self._calculate_rsi(df['close'], period=21)
|
||||
|
||||
# Stochastic Oscillator
|
||||
stoch = ta.momentum.StochasticOscillator(df['high'], df['low'], df['close'])
|
||||
@ -2191,9 +2191,9 @@ class DataProvider:
|
||||
df['sma_20'] = ta.trend.sma_indicator(df['close'], window=20)
|
||||
df['ema_12'] = ta.trend.ema_indicator(df['close'], window=12)
|
||||
|
||||
# Basic RSI
|
||||
# Basic RSI - using our own implementation to avoid ta library deprecation warnings
|
||||
if len(df) >= 14:
|
||||
df['rsi_14'] = ta.momentum.rsi(df['close'], window=14)
|
||||
df['rsi_14'] = self._calculate_rsi(df['close'], period=14)
|
||||
|
||||
# Basic volume indicators
|
||||
if len(df) >= 10:
|
||||
@ -2212,6 +2212,31 @@ class DataProvider:
|
||||
logger.error(f"Error adding basic indicators: {e}")
|
||||
return df
|
||||
|
||||
def _calculate_rsi(self, prices: pd.Series, period: int = 14) -> float:
|
||||
"""Calculate RSI (Relative Strength Index) - custom implementation to avoid ta library deprecation warnings"""
|
||||
try:
|
||||
if len(prices) < period + 1:
|
||||
return 50.0 # Default neutral value
|
||||
|
||||
# Calculate price changes
|
||||
delta = prices.diff()
|
||||
|
||||
# Separate gains and losses
|
||||
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
|
||||
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
|
||||
|
||||
# Calculate RS and RSI
|
||||
rs = gain / loss
|
||||
rsi = 100 - (100 / (1 + rs))
|
||||
|
||||
# Return the last value, or 50 if NaN
|
||||
last_rsi = rsi.iloc[-1]
|
||||
return float(last_rsi) if not pd.isna(last_rsi) else 50.0
|
||||
|
||||
except Exception as e:
|
||||
logger.debug(f"Error calculating RSI: {e}")
|
||||
return 50.0 # Default neutral value
|
||||
|
||||
def _load_from_cache(self, symbol: str, timeframe: str) -> Optional[pd.DataFrame]:
|
||||
"""Load data from cache"""
|
||||
try:
|
||||
|
Reference in New Issue
Block a user