eshan6704 commited on
Commit
109c8f6
·
verified ·
1 Parent(s): 8e6de5c

Update indicater.py

Browse files
Files changed (1) hide show
  1. indicater.py +57 -63
indicater.py CHANGED
@@ -3,76 +3,70 @@ import pandas as pd
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import numpy as np
4
  import talib
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def calculate_indicators(df):
7
  """
8
+ Calculate various indicators.
9
+ df: OHLCV dataframe with columns: Open, High, Low, Close, Volume
10
+ Returns dict of indicator name -> DataFrame
11
  """
12
  indicators = {}
13
 
14
+ close = df['Close']
15
+ high = df['High']
16
+ low = df['Low']
17
+ volume = df['Volume'] if 'Volume' in df else pd.Series([1]*len(df), index=df.index)
18
+
19
+ # --- TA-Lib indicators ---
20
+ try:
21
+ # Moving averages
22
+ indicators['SMA20'] = pd.DataFrame({'SMA20': talib.SMA(close, timeperiod=20)}, index=df.index)
23
+ indicators['SMA50'] = pd.DataFrame({'SMA50': talib.SMA(close, timeperiod=50)}, index=df.index)
24
+ indicators['EMA20'] = pd.DataFrame({'EMA20': talib.EMA(close, timeperiod=20)}, index=df.index)
25
+ indicators['EMA50'] = pd.DataFrame({'EMA50': talib.EMA(close, timeperiod=50)}, index=df.index)
26
+
27
+ # MACD
28
+ macd, macdsignal, macdhist = talib.MACD(close)
29
+ indicators['MACD'] = pd.DataFrame({'MACD': macd, 'Signal': macdsignal, 'Hist': macdhist}, index=df.index)
30
 
31
+ # RSI
32
+ indicators['RSI14'] = pd.DataFrame({'RSI14': talib.RSI(close, timeperiod=14)}, index=df.index)
 
 
 
 
 
 
 
 
33
 
34
+ # Bollinger Bands
35
+ upper, middle, lower = talib.BBANDS(close)
36
+ indicators['BB_upper'] = pd.DataFrame({'BB_upper': upper}, index=df.index)
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
+ # ADX
41
+ indicators['ADX14'] = pd.DataFrame({'ADX14': talib.ADX(high, low, close, timeperiod=14)}, index=df.index)
42
+ except Exception as e:
43
+ print("TA-Lib indicators error:", e)
44
+
45
+ # --- Custom indicators if not in TA-Lib ---
46
+ # SuperTrend
47
+ indicators['SuperTrend'] = calculate_supertrend(df)
48
 
49
  return indicators
50
+
51
+ def calculate_supertrend(df, period=10, multiplier=3):
52
+ """
53
+ Basic SuperTrend calculation
54
+ """
55
+ hl2 = (df['High'] + df['Low']) / 2
56
+ atr = talib.ATR(df['High'], df['Low'], df['Close'], timeperiod=period)
57
+ st_upper = hl2 + multiplier * atr
58
+ st_lower = hl2 - multiplier * atr
59
+ supertrend = pd.Series(index=df.index, dtype=float)
60
+ trend = True # True = up, False = down
61
+
62
+ for i in range(len(df)):
63
+ if i == 0:
64
+ supertrend.iloc[i] = st_upper.iloc[i]
65
+ else:
66
+ if df['Close'].iloc[i] > supertrend.iloc[i-1]:
67
+ trend = True
68
+ supertrend.iloc[i] = st_lower.iloc[i]
69
+ else:
70
+ trend = False
71
+ supertrend.iloc[i] = st_upper.iloc[i]
72
+ return pd.DataFrame({'SuperTrend': supertrend}, index=df.index)