Create indicater.py
Browse files- indicater.py +60 -0
indicater.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# indicater.py
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import talib as ta
|
| 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 |
+
df: DataFrame with columns ['Open','High','Low','Close','Volume']
|
| 37 |
+
Returns dict of indicator DataFrames or Series
|
| 38 |
+
"""
|
| 39 |
+
indicators = {}
|
| 40 |
+
|
| 41 |
+
if 'Close' in df.columns:
|
| 42 |
+
# Moving averages on main chart
|
| 43 |
+
indicators['SMA20'] = ta.SMA(df['Close'], timeperiod=20)
|
| 44 |
+
indicators['SMA50'] = ta.SMA(df['Close'], timeperiod=50)
|
| 45 |
+
indicators['EMA20'] = ta.EMA(df['Close'], timeperiod=20)
|
| 46 |
+
indicators['EMA50'] = ta.EMA(df['Close'], timeperiod=50)
|
| 47 |
+
|
| 48 |
+
# MACD as sub-plot
|
| 49 |
+
macd, macdsignal, macdhist = ta.MACD(df['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
|
| 50 |
+
indicators['MACD'] = pd.DataFrame({'MACD': macd, 'Signal': macdsignal, 'Hist': macdhist}, index=df.index)
|
| 51 |
+
|
| 52 |
+
# Bollinger Bands
|
| 53 |
+
upper, middle, lower = ta.BBANDS(df['Close'], timeperiod=20)
|
| 54 |
+
indicators['Bollinger'] = pd.DataFrame({'Upper': upper, 'Middle': middle, 'Lower': lower}, index=df.index)
|
| 55 |
+
|
| 56 |
+
# SuperTrend requires High, Low, Close
|
| 57 |
+
if all(x in df.columns for x in ['High', 'Low', 'Close']):
|
| 58 |
+
indicators['SuperTrend'] = supertrend(df)
|
| 59 |
+
|
| 60 |
+
return indicators
|