eshan6704 commited on
Commit
402949b
·
verified ·
1 Parent(s): 0b8eecf

Update indicater.py

Browse files
Files changed (1) hide show
  1. indicater.py +63 -44
indicater.py CHANGED
@@ -3,57 +3,76 @@ 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
 
3
  import numpy as np
4
  import talib
5
 
6
+ # -------------------------------
7
+ # Custom SuperTrend implementation
8
+ # -------------------------------
9
+ def supertrend(df, period=10, multiplier=3):
10
+ """
11
+ Compute SuperTrend
12
+ """
13
+ hl2 = (df['High'] + df['Low']) / 2
14
+ atr = talib.ATR(df['High'], df['Low'], df['Close'], timeperiod=period)
15
+
16
+ upperband = hl2 + (multiplier * atr)
17
+ lowerband = hl2 - (multiplier * atr)
18
+ supertrend = pd.Series(index=df.index, dtype=float)
19
+ direction = pd.Series(index=df.index, dtype=int)
20
+
21
+ for i in range(len(df)):
22
+ if i == 0:
23
+ supertrend.iloc[i] = upperband.iloc[i]
24
+ direction.iloc[i] = 1
25
+ continue
26
+ if df['Close'].iloc[i] > supertrend.iloc[i-1]:
27
+ direction.iloc[i] = 1
28
+ supertrend.iloc[i] = lowerband.iloc[i]
29
+ elif df['Close'].iloc[i] < supertrend.iloc[i-1]:
30
+ direction.iloc[i] = -1
31
+ supertrend.iloc[i] = upperband.iloc[i]
32
+ else:
33
+ direction.iloc[i] = direction.iloc[i-1]
34
+ supertrend.iloc[i] = supertrend.iloc[i-1]
35
+ return supertrend
36
+
37
+ # -------------------------------
38
+ # Main indicator calculation
39
+ # -------------------------------
40
  def calculate_indicators(df):
41
  """
42
+ Compute major TA-Lib indicators and custom indicators.
43
+ Return dictionary {indicator_name: series_or_df}
44
  """
45
  indicators = {}
46
 
47
+ # --- Price-based moving averages ---
48
+ indicators['SMA5'] = talib.SMA(df['Close'], timeperiod=5)
49
+ indicators['SMA20'] = talib.SMA(df['Close'], timeperiod=20)
50
+ indicators['SMA50'] = talib.SMA(df['Close'], timeperiod=50)
51
+ indicators['SMA200'] = talib.SMA(df['Close'], timeperiod=200)
52
+
53
+ indicators['EMA5'] = talib.EMA(df['Close'], timeperiod=5)
54
+ indicators['EMA20'] = talib.EMA(df['Close'], timeperiod=20)
55
+ indicators['EMA50'] = talib.EMA(df['Close'], timeperiod=50)
56
+ indicators['EMA200'] = talib.EMA(df['Close'], timeperiod=200)
57
 
58
+ # --- Momentum indicators ---
59
+ indicators['RSI'] = talib.RSI(df['Close'], timeperiod=14)
60
+ indicators['STOCH'], indicators['STOCH_signal'] = talib.STOCH(
61
+ df['High'], df['Low'], df['Close'], fastk_period=14, slowk_period=3, slowk_matype=0,
62
+ slowd_period=3, slowd_matype=0
63
+ )
64
+ indicators['MACD'], indicators['MACD_signal'], indicators['MACD_hist'] = talib.MACD(df['Close'])
65
+ indicators['ADX'] = talib.ADX(df['High'], df['Low'], df['Close'], timeperiod=14)
66
+ indicators['CCI'] = talib.CCI(df['High'], df['Low'], df['Close'], timeperiod=14)
67
+ indicators['OBV'] = talib.OBV(df['Close'], df['Volume'])
68
 
69
+ # --- Volatility indicators ---
70
+ indicators['ATR'] = talib.ATR(df['High'], df['Low'], df['Close'], timeperiod=14)
71
+ indicators['BB_upper'], indicators['BB_middle'], indicators['BB_lower'] = talib.BBANDS(
72
+ df['Close'], timeperiod=20, nbdevup=2, nbdevdn=2, matype=0
73
+ )
74
 
75
+ # --- Custom indicators ---
76
+ indicators['SuperTrend'] = supertrend(df)
77
 
78
  return indicators