Spaces:
Running
Running
Update indicator.py
Browse files- indicator.py +98 -129
indicator.py
CHANGED
|
@@ -1,139 +1,108 @@
|
|
| 1 |
# indicator.py
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
|
| 43 |
-
rs = gain / loss
|
| 44 |
-
rsi = 100 - (100 / (1 + rs))
|
| 45 |
-
|
| 46 |
-
return pd.DataFrame({"RSI": rsi})
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# ==============================
|
| 50 |
-
# Supertrend (Custom)
|
| 51 |
-
# ==============================
|
| 52 |
-
def calc_supertrend(df, period=10, multiplier=3):
|
| 53 |
-
hl2 = (df["High"] + df["Low"]) / 2
|
| 54 |
-
tr1 = df["High"] - df["Low"]
|
| 55 |
-
tr2 = abs(df["High"] - df["Close"].shift())
|
| 56 |
-
tr3 = abs(df["Low"] - df["Close"].shift())
|
| 57 |
-
tr = tr1.combine(tr2, max).combine(tr3, max)
|
| 58 |
-
|
| 59 |
atr = tr.rolling(period).mean()
|
|
|
|
|
|
|
|
|
|
| 60 |
upperband = hl2 + multiplier * atr
|
| 61 |
lowerband = hl2 - multiplier * atr
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
else:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
else:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
# ==============================
|
| 100 |
-
# Keltner Channel
|
| 101 |
-
# ==============================
|
| 102 |
-
def calc_keltner(df):
|
| 103 |
-
typical = (df["High"] + df["Low"] + df["Close"]) / 3
|
| 104 |
-
ema = typical.ewm(span=20).mean()
|
| 105 |
-
atr = (df["High"] - df["Low"]).rolling(20).mean()
|
| 106 |
-
|
| 107 |
-
upper = ema + 2 * atr
|
| 108 |
-
lower = ema - 2 * atr
|
| 109 |
-
|
| 110 |
-
return pd.DataFrame({"KC_UP": upper, "KC_MID": ema, "KC_LOW": lower})
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# ==============================
|
| 114 |
-
# ZigZag (simplified)
|
| 115 |
-
# ==============================
|
| 116 |
-
def calc_zigzag(df, pct=3):
|
| 117 |
-
zigzag = [np.nan] * len(df)
|
| 118 |
-
last_pivot = df["Close"].iloc[0]
|
| 119 |
-
|
| 120 |
for i in range(1, len(df)):
|
| 121 |
-
change = (df[
|
| 122 |
-
if
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# indicator.py
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
+
import talib
|
| 5 |
+
|
| 6 |
+
# ============================================================
|
| 7 |
+
# TRENDING INDICATOR HELPERS
|
| 8 |
+
# ============================================================
|
| 9 |
+
|
| 10 |
+
def macd(df, fast=12, slow=26, signal=9):
|
| 11 |
+
"""Compute MACD and signal line."""
|
| 12 |
+
try:
|
| 13 |
+
macd_line, signal_line, hist = talib.MACD(df['Close'], fastperiod=fast, slowperiod=slow, signalperiod=signal)
|
| 14 |
+
df['MACD'] = macd_line
|
| 15 |
+
df['MACD_Signal'] = signal_line
|
| 16 |
+
except Exception:
|
| 17 |
+
# fallback
|
| 18 |
+
df['MACD'] = df['Close'].ewm(span=fast).mean() - df['Close'].ewm(span=slow).mean()
|
| 19 |
+
df['MACD_Signal'] = df['MACD'].ewm(span=signal).mean()
|
| 20 |
+
return df
|
| 21 |
+
|
| 22 |
+
def rsi(df, period=14):
|
| 23 |
+
try:
|
| 24 |
+
df['RSI'] = talib.RSI(df['Close'], timeperiod=period)
|
| 25 |
+
except Exception:
|
| 26 |
+
delta = df['Close'].diff()
|
| 27 |
+
gain = delta.where(delta > 0, 0)
|
| 28 |
+
loss = -delta.where(delta < 0, 0)
|
| 29 |
+
avg_gain = gain.rolling(period).mean()
|
| 30 |
+
avg_loss = loss.rolling(period).mean()
|
| 31 |
+
rs = avg_gain / avg_loss
|
| 32 |
+
df['RSI'] = 100 - (100 / (1 + rs))
|
| 33 |
+
return df
|
| 34 |
+
|
| 35 |
+
def supertrend(df, period=10, multiplier=3):
|
| 36 |
+
"""Compute SuperTrend indicator"""
|
| 37 |
+
# ATR calculation
|
| 38 |
+
high_low = df['High'] - df['Low']
|
| 39 |
+
high_close = (df['High'] - df['Close'].shift()).abs()
|
| 40 |
+
low_close = (df['Low'] - df['Close'].shift()).abs()
|
| 41 |
+
tr = pd.concat([high_low, high_close, low_close], axis=1).max(axis=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
atr = tr.rolling(period).mean()
|
| 43 |
+
|
| 44 |
+
# Basic Upper and Lower Bands
|
| 45 |
+
hl2 = (df['High'] + df['Low']) / 2
|
| 46 |
upperband = hl2 + multiplier * atr
|
| 47 |
lowerband = hl2 - multiplier * atr
|
| 48 |
|
| 49 |
+
# SuperTrend calculation
|
| 50 |
+
supertrend = [True] # True = uptrend, False = downtrend
|
| 51 |
+
final_upper = upperband.copy()
|
| 52 |
+
final_lower = lowerband.copy()
|
| 53 |
+
for i in range(1, len(df)):
|
| 54 |
+
if df['Close'][i] > final_upper[i-1]:
|
| 55 |
+
supertrend.append(True)
|
| 56 |
+
elif df['Close'][i] < final_lower[i-1]:
|
| 57 |
+
supertrend.append(False)
|
| 58 |
else:
|
| 59 |
+
supertrend.append(supertrend[i-1])
|
| 60 |
+
if supertrend[i-1]:
|
| 61 |
+
final_upper[i] = min(upperband[i], final_upper[i-1])
|
| 62 |
else:
|
| 63 |
+
final_lower[i] = max(lowerband[i], final_lower[i-1])
|
| 64 |
+
df['SuperTrend'] = supertrend
|
| 65 |
+
return df
|
| 66 |
+
|
| 67 |
+
def keltner_channel(df, period=20, atr_mult=2):
|
| 68 |
+
"""Compute Keltner Channels"""
|
| 69 |
+
try:
|
| 70 |
+
high_low = df['High'] - df['Low']
|
| 71 |
+
high_close = (df['High'] - df['Close'].shift()).abs()
|
| 72 |
+
low_close = (df['Low'] - df['Close'].shift()).abs()
|
| 73 |
+
tr = pd.concat([high_low, high_close, low_close], axis=1).max(axis=1)
|
| 74 |
+
atr = tr.rolling(period).mean()
|
| 75 |
+
ma = df['Close'].rolling(period).mean()
|
| 76 |
+
df['KC_Upper'] = ma + atr_mult * atr
|
| 77 |
+
df['KC_Lower'] = ma - atr_mult * atr
|
| 78 |
+
except Exception:
|
| 79 |
+
pass
|
| 80 |
+
return df
|
| 81 |
+
|
| 82 |
+
def zigzag(df, change_pct=5):
|
| 83 |
+
"""Simple ZigZag based on % change"""
|
| 84 |
+
zz = [df['Close'].iloc[0]]
|
| 85 |
+
last_dir = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
for i in range(1, len(df)):
|
| 87 |
+
change = (df['Close'].iloc[i] - zz[-1]) / zz[-1] * 100
|
| 88 |
+
if change > change_pct:
|
| 89 |
+
zz.append(df['Close'].iloc[i])
|
| 90 |
+
last_dir = 1
|
| 91 |
+
elif change < -change_pct:
|
| 92 |
+
zz.append(df['Close'].iloc[i])
|
| 93 |
+
last_dir = -1
|
| 94 |
+
else:
|
| 95 |
+
zz.append(np.nan)
|
| 96 |
+
df['ZigZag'] = zz
|
| 97 |
+
return df
|
| 98 |
+
|
| 99 |
+
def swing_high_low(df, period=5):
|
| 100 |
+
"""Mark swing highs and lows"""
|
| 101 |
+
df['Swing_High'] = df['High'][(df['High'].rolling(period*2+1, center=True).max() == df['High'])]
|
| 102 |
+
df['Swing_Low'] = df['Low'][(df['Low'].rolling(period*2+1, center=True).min() == df['Low'])]
|
| 103 |
+
return df
|
| 104 |
+
|
| 105 |
+
def stockstick(df):
|
| 106 |
+
"""Simple stick colors for up/down"""
|
| 107 |
+
df['StickColor'] = np.where(df['Close'] >= df['Open'], 'green', 'red')
|
| 108 |
+
return df
|