Update indicater.py
Browse files- indicater.py +61 -46
indicater.py
CHANGED
|
@@ -1,72 +1,87 @@
|
|
| 1 |
# indicater.py
|
| 2 |
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
import talib
|
|
|
|
| 5 |
|
| 6 |
def calculate_indicators(df):
|
| 7 |
"""
|
| 8 |
-
Calculate
|
| 9 |
-
df:
|
| 10 |
-
Returns dict of
|
| 11 |
"""
|
| 12 |
indicators = {}
|
| 13 |
-
|
| 14 |
close = df['Close']
|
| 15 |
high = df['High']
|
| 16 |
low = df['Low']
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
# ---
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
indicators['BB_middle'] = pd.DataFrame({'BB_middle': middle}, index=df.index)
|
| 38 |
-
indicators['BB_lower'] = pd.DataFrame({'BB_lower': lower}, index=df.index)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# --- Custom indicators if not in TA-Lib ---
|
| 46 |
-
# SuperTrend
|
| 47 |
-
indicators['SuperTrend'] = calculate_supertrend(df)
|
| 48 |
|
| 49 |
return indicators
|
| 50 |
|
| 51 |
-
|
|
|
|
| 52 |
"""
|
| 53 |
-
|
|
|
|
| 54 |
"""
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
for i in range(len(df)):
|
| 63 |
if i == 0:
|
| 64 |
-
supertrend.iloc[i] =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
else:
|
| 66 |
-
if
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
else:
|
| 70 |
-
trend = False
|
| 71 |
-
supertrend.iloc[i] = st_upper.iloc[i]
|
| 72 |
-
return pd.DataFrame({'SuperTrend': supertrend}, index=df.index)
|
|
|
|
| 1 |
# indicater.py
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import talib
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
def calculate_indicators(df):
|
| 7 |
"""
|
| 8 |
+
Calculate all possible indicators from TA-Lib for a given OHLCV DataFrame.
|
| 9 |
+
df: DataFrame with columns ['Open','High','Low','Close','Volume']
|
| 10 |
+
Returns: dict of {indicator_name: Series or DataFrame}
|
| 11 |
"""
|
| 12 |
indicators = {}
|
| 13 |
+
|
| 14 |
close = df['Close']
|
| 15 |
high = df['High']
|
| 16 |
low = df['Low']
|
| 17 |
+
open_ = df['Open']
|
| 18 |
+
volume = df['Volume']
|
| 19 |
|
| 20 |
+
# --- Moving Averages ---
|
| 21 |
+
indicators['SMA_5'] = talib.SMA(close, timeperiod=5)
|
| 22 |
+
indicators['SMA_10'] = talib.SMA(close, timeperiod=10)
|
| 23 |
+
indicators['SMA_20'] = talib.SMA(close, timeperiod=20)
|
| 24 |
+
indicators['SMA_50'] = talib.SMA(close, timeperiod=50)
|
| 25 |
+
indicators['EMA_5'] = talib.EMA(close, timeperiod=5)
|
| 26 |
+
indicators['EMA_10'] = talib.EMA(close, timeperiod=10)
|
| 27 |
+
indicators['EMA_20'] = talib.EMA(close, timeperiod=20)
|
| 28 |
+
indicators['EMA_50'] = talib.EMA(close, timeperiod=50)
|
| 29 |
|
| 30 |
+
# --- Trend Indicators ---
|
| 31 |
+
indicators['ADX'] = talib.ADX(high, low, close, timeperiod=14)
|
| 32 |
+
indicators['CCI'] = talib.CCI(high, low, close, timeperiod=14)
|
| 33 |
+
indicators['AROON_UP'], indicators['AROON_DOWN'] = talib.AROON(high, low, timeperiod=14)
|
| 34 |
+
indicators['MACD'], indicators['MACD_signal'], indicators['MACD_hist'] = talib.MACD(close)
|
| 35 |
+
indicators['ATR'] = talib.ATR(high, low, close, timeperiod=14)
|
| 36 |
|
| 37 |
+
# --- Oscillators ---
|
| 38 |
+
indicators['RSI'] = talib.RSI(close, timeperiod=14)
|
| 39 |
+
indicators['STOCH_slowk'], indicators['STOCH_slowd'] = talib.STOCH(high, low, close)
|
| 40 |
+
indicators['STOCHF_fastk'], indicators['STOCHF_fastd'] = talib.STOCHF(high, low, close)
|
| 41 |
+
indicators['WILLR'] = talib.WILLR(high, low, close, timeperiod=14)
|
| 42 |
|
| 43 |
+
# --- Volatility ---
|
| 44 |
+
indicators['BB_upper'], indicators['BB_middle'], indicators['BB_lower'] = talib.BBANDS(close)
|
| 45 |
+
indicators['ATR'] = talib.ATR(high, low, close, timeperiod=14)
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# --- Fallback for SuperTrend ---
|
| 48 |
+
try:
|
| 49 |
+
indicators['SuperTrend'] = supertrend(df)
|
| 50 |
except Exception as e:
|
| 51 |
+
indicators['SuperTrend'] = pd.Series([np.nan]*len(df), index=df.index)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
return indicators
|
| 54 |
|
| 55 |
+
|
| 56 |
+
def supertrend(df, period=10, multiplier=3):
|
| 57 |
"""
|
| 58 |
+
Compute SuperTrend indicator.
|
| 59 |
+
Returns a Series same length as df with SuperTrend values.
|
| 60 |
"""
|
| 61 |
+
high = df['High']
|
| 62 |
+
low = df['Low']
|
| 63 |
+
close = df['Close']
|
| 64 |
+
|
| 65 |
+
atr = talib.ATR(high, low, close, timeperiod=period)
|
| 66 |
+
hl2 = (high + low) / 2
|
| 67 |
+
upperband = hl2 + multiplier * atr
|
| 68 |
+
lowerband = hl2 - multiplier * atr
|
| 69 |
+
|
| 70 |
+
supertrend = pd.Series(index=df.index)
|
| 71 |
+
direction = True # True = bullish
|
| 72 |
|
| 73 |
for i in range(len(df)):
|
| 74 |
if i == 0:
|
| 75 |
+
supertrend.iloc[i] = hl2.iloc[i]
|
| 76 |
+
continue
|
| 77 |
+
if close.iloc[i] > supertrend.iloc[i-1]:
|
| 78 |
+
direction = True
|
| 79 |
+
elif close.iloc[i] < supertrend.iloc[i-1]:
|
| 80 |
+
direction = False
|
| 81 |
+
|
| 82 |
+
if direction:
|
| 83 |
+
supertrend.iloc[i] = lowerband.iloc[i] if lowerband.iloc[i] > supertrend.iloc[i-1] else supertrend.iloc[i-1]
|
| 84 |
else:
|
| 85 |
+
supertrend.iloc[i] = upperband.iloc[i] if upperband.iloc[i] < supertrend.iloc[i-1] else supertrend.iloc[i-1]
|
| 86 |
+
|
| 87 |
+
return supertrend
|
|
|
|
|
|
|
|
|
|
|
|