Update indicater.py
Browse files- indicater.py +45 -46
indicater.py
CHANGED
|
@@ -1,60 +1,59 @@
|
|
| 1 |
# indicater.py
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
import talib
|
| 5 |
-
|
| 6 |
-
# -------------------------------
|
| 7 |
-
# CUSTOM INDICATOR FUNCTIONS
|
| 8 |
-
# -------------------------------
|
| 9 |
-
|
| 10 |
-
def supertrend(df, period=10, multiplier=3):
|
| 11 |
-
"""
|
| 12 |
-
Simple SuperTrend calculation.
|
| 13 |
-
Returns a Series aligned with df.index
|
| 14 |
-
"""
|
| 15 |
-
atr = ta.ATR(df['High'], df['Low'], df['Close'], timeperiod=period)
|
| 16 |
-
hl2 = (df['High'] + df['Low']) / 2
|
| 17 |
-
final_upperband = hl2 + multiplier * atr
|
| 18 |
-
final_lowerband = hl2 - multiplier * atr
|
| 19 |
-
|
| 20 |
-
st = pd.Series(index=df.index, dtype=float)
|
| 21 |
-
trend = True
|
| 22 |
-
for i in range(1, len(df)):
|
| 23 |
-
if df['Close'].iloc[i] > final_upperband.iloc[i-1]:
|
| 24 |
-
trend = True
|
| 25 |
-
elif df['Close'].iloc[i] < final_lowerband.iloc[i-1]:
|
| 26 |
-
trend = False
|
| 27 |
-
st.iloc[i] = final_lowerband.iloc[i] if trend else final_upperband.iloc[i]
|
| 28 |
-
return st
|
| 29 |
-
|
| 30 |
-
# -------------------------------
|
| 31 |
-
# MAIN INDICATOR FUNCTION
|
| 32 |
-
# -------------------------------
|
| 33 |
|
| 34 |
def calculate_indicators(df):
|
| 35 |
"""
|
| 36 |
-
|
| 37 |
-
Returns dict of indicator
|
| 38 |
"""
|
| 39 |
indicators = {}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
indicators['EMA20'] = ta.EMA(df['Close'], timeperiod=20)
|
| 46 |
-
indicators['EMA50'] = ta.EMA(df['Close'], timeperiod=50)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
return indicators
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 multiple indicators for given OHLCV df.
|
| 9 |
+
Returns dict of indicator name -> DataFrame/Series.
|
| 10 |
"""
|
| 11 |
indicators = {}
|
| 12 |
|
| 13 |
+
close = df['Close'].astype(float)
|
| 14 |
+
high = df['High'].astype(float)
|
| 15 |
+
low = df['Low'].astype(float)
|
| 16 |
+
volume = df['Volume'].astype(float)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# --- MA on main chart ---
|
| 19 |
+
indicators['SMA20'] = talib.SMA(close, timeperiod=20)
|
| 20 |
+
indicators['SMA50'] = talib.SMA(close, timeperiod=50)
|
| 21 |
|
| 22 |
+
# --- MACD ---
|
| 23 |
+
macd, macdsignal, macdhist = talib.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
|
| 24 |
+
indicators['MACD'] = pd.DataFrame({'MACD': macd, 'Signal': macdsignal, 'Hist': macdhist})
|
| 25 |
|
| 26 |
+
# --- RSI ---
|
| 27 |
+
indicators['RSI'] = talib.RSI(close, timeperiod=14)
|
| 28 |
+
|
| 29 |
+
# --- SuperTrend (not in TA-Lib, custom function) ---
|
| 30 |
+
indicators['SuperTrend'] = supertrend(high, low, close, period=10, multiplier=3)
|
| 31 |
|
| 32 |
return indicators
|
| 33 |
+
|
| 34 |
+
def supertrend(high, low, close, period=10, multiplier=3):
|
| 35 |
+
"""
|
| 36 |
+
Simple SuperTrend implementation.
|
| 37 |
+
Returns Series with trend value.
|
| 38 |
+
"""
|
| 39 |
+
atr = talib.ATR(high, low, close, timeperiod=period)
|
| 40 |
+
hl2 = (high + low) / 2
|
| 41 |
+
final_upperband = hl2 + (multiplier * atr)
|
| 42 |
+
final_lowerband = hl2 - (multiplier * atr)
|
| 43 |
+
trend = pd.Series(index=close.index)
|
| 44 |
+
direction = True # True = uptrend
|
| 45 |
+
|
| 46 |
+
for i in range(len(close)):
|
| 47 |
+
if i == 0:
|
| 48 |
+
trend.iloc[i] = final_upperband.iloc[i]
|
| 49 |
+
else:
|
| 50 |
+
if close.iloc[i] > final_upperband.iloc[i-1]:
|
| 51 |
+
direction = True
|
| 52 |
+
elif close.iloc[i] < final_lowerband.iloc[i-1]:
|
| 53 |
+
direction = False
|
| 54 |
+
if direction:
|
| 55 |
+
trend.iloc[i] = final_lowerband.iloc[i]
|
| 56 |
+
else:
|
| 57 |
+
trend.iloc[i] = final_upperband.iloc[i]
|
| 58 |
+
|
| 59 |
+
return trend
|