eshan6704 commited on
Commit
a86b9f2
·
verified ·
1 Parent(s): 6c0386c

Update indicater.py

Browse files
Files changed (1) hide show
  1. indicater.py +61 -46
indicater.py CHANGED
@@ -1,72 +1,87 @@
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 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)
 
1
  # indicater.py
2
  import pandas as pd
 
3
  import talib
4
+ import numpy as np
5
 
6
  def calculate_indicators(df):
7
  """
8
+ Calculate all possible indicators from TA-Lib for a given OHLCV DataFrame.
9
+ df: DataFrame with columns ['Open','High','Low','Close','Volume']
10
+ Returns: dict of {indicator_name: Series or DataFrame}
11
  """
12
  indicators = {}
13
+
14
  close = df['Close']
15
  high = df['High']
16
  low = df['Low']
17
+ open_ = df['Open']
18
+ volume = df['Volume']
19
 
20
+ # --- Moving Averages ---
21
+ indicators['SMA_5'] = talib.SMA(close, timeperiod=5)
22
+ indicators['SMA_10'] = talib.SMA(close, timeperiod=10)
23
+ indicators['SMA_20'] = talib.SMA(close, timeperiod=20)
24
+ indicators['SMA_50'] = talib.SMA(close, timeperiod=50)
25
+ indicators['EMA_5'] = talib.EMA(close, timeperiod=5)
26
+ indicators['EMA_10'] = talib.EMA(close, timeperiod=10)
27
+ indicators['EMA_20'] = talib.EMA(close, timeperiod=20)
28
+ indicators['EMA_50'] = talib.EMA(close, timeperiod=50)
29
 
30
+ # --- Trend Indicators ---
31
+ indicators['ADX'] = talib.ADX(high, low, close, timeperiod=14)
32
+ indicators['CCI'] = talib.CCI(high, low, close, timeperiod=14)
33
+ indicators['AROON_UP'], indicators['AROON_DOWN'] = talib.AROON(high, low, timeperiod=14)
34
+ indicators['MACD'], indicators['MACD_signal'], indicators['MACD_hist'] = talib.MACD(close)
35
+ indicators['ATR'] = talib.ATR(high, low, close, timeperiod=14)
36
 
37
+ # --- Oscillators ---
38
+ indicators['RSI'] = talib.RSI(close, timeperiod=14)
39
+ indicators['STOCH_slowk'], indicators['STOCH_slowd'] = talib.STOCH(high, low, close)
40
+ indicators['STOCHF_fastk'], indicators['STOCHF_fastd'] = talib.STOCHF(high, low, close)
41
+ indicators['WILLR'] = talib.WILLR(high, low, close, timeperiod=14)
42
 
43
+ # --- Volatility ---
44
+ indicators['BB_upper'], indicators['BB_middle'], indicators['BB_lower'] = talib.BBANDS(close)
45
+ indicators['ATR'] = talib.ATR(high, low, close, timeperiod=14)
 
 
46
 
47
+ # --- Fallback for SuperTrend ---
48
+ try:
49
+ indicators['SuperTrend'] = supertrend(df)
50
  except Exception as e:
51
+ indicators['SuperTrend'] = pd.Series([np.nan]*len(df), index=df.index)
 
 
 
 
52
 
53
  return indicators
54
 
55
+
56
+ def supertrend(df, period=10, multiplier=3):
57
  """
58
+ Compute SuperTrend indicator.
59
+ Returns a Series same length as df with SuperTrend values.
60
  """
61
+ high = df['High']
62
+ low = df['Low']
63
+ close = df['Close']
64
+
65
+ atr = talib.ATR(high, low, close, timeperiod=period)
66
+ hl2 = (high + low) / 2
67
+ upperband = hl2 + multiplier * atr
68
+ lowerband = hl2 - multiplier * atr
69
+
70
+ supertrend = pd.Series(index=df.index)
71
+ direction = True # True = bullish
72
 
73
  for i in range(len(df)):
74
  if i == 0:
75
+ supertrend.iloc[i] = hl2.iloc[i]
76
+ continue
77
+ if close.iloc[i] > supertrend.iloc[i-1]:
78
+ direction = True
79
+ elif close.iloc[i] < supertrend.iloc[i-1]:
80
+ direction = False
81
+
82
+ if direction:
83
+ supertrend.iloc[i] = lowerband.iloc[i] if lowerband.iloc[i] > supertrend.iloc[i-1] else supertrend.iloc[i-1]
84
  else:
85
+ supertrend.iloc[i] = upperband.iloc[i] if upperband.iloc[i] < supertrend.iloc[i-1] else supertrend.iloc[i-1]
86
+
87
+ return supertrend