Trading indicators with python code

The considered indicators are:

  • Momentum Strength
  • Exponential moving average (EMA)
  • Moving Average Convergence Divergence (MACD)
  • Aroon up and Aroon down
  • Relative Strength Index (RSI)
  • Accumulation/Distribution (AD)

Momentum Strength

def Momentum(self):
#Momentum Momentum=cl−clx
self.momentum=np.subtract(self.df_cl[10:], self.df_cl[:-10])

Exponential moving average (EMA)

def Exp_moving_average(self):
self.ema20=np.asarray(pd.DataFrame(
self.df_cl).ewm(span=20).mean()).flatte()
self.ema100=np.asarray(pd.DataFrame(
self.df_cl).ewm(span=100).mean()).flatten()

Moving Average Convergence Divergence (MACD)

def Macd(self):
#Moving Average Convergence Divergence (MACD)
#MACD=12-Period EMA − 26-Period EMA
self.ema12=np.asarray(pd.DataFrame(
self.df_cl).ewm(span=12).mean())
self.ema26=np.asarray(pd.DataFrame(
self.df_cl).ewm(span=26).mean())
self.macd=np.subtract(self.ema12, self.ema26).flatten()
self.macd_signal=np.asarray(pd.DataFrame(
self.macd).ewm(span=9).mean()).flatten()

Aroon indicator

def Aroon(self):
#aroon indicator
period=25
self.aroon_up=[(100/period)*
(period-np.argmax(self.df_cl[t-period:t]))
for t in range(period, len(self.df_cl))]
self.aroon_down=[(100/period)*
(period-np.argmin(self.df_cl[t-period:t]))
for t in range(period, len(self.df_cl))]

Relative Strength Index (RSI)

def Rsi(self):
#Relative Strength Index
period=14
up=np.subtract(self.df_cl[1:], self.df_cl[:-1])
up[up<0]=0
down=np.subtract(self.df_cl[1:], self.df_cl[:-1])
down[down>0]=0
down=np.absolute(down)
ema_up=np.asarray(pd.DataFrame(up).ewm(span=period).mean())
ema_down=np.asarray(pd.DataFrame(
down).ewm(span=period).mean())
rs=np.divide(ema_up, ema_down)
self.rsi=np.subtract(100, np.divide(100,
np.add(1, rs))).flatten()

Accumulation/Distribution (AD)

Code

References:

  1. https://www.investopedia.com/articles/technical/081501.asp
  2. https://www.investopedia.com/terms/r/rsi.asp
  3. https://www.investopedia.com/terms/m/macd.asp
  4. https://www.investopedia.com/ask/answers/112814/what-aroon-indicator-formula-and-how-indicator-calculated.asp
  5. https://www.investopedia.com/terms/a/adx.asp
  6. https://www.investopedia.com/terms/a/accumulationdistribution.asp

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
IperGiove

IperGiove

121 Followers

Data scientist at TheRockTrading to study artificial intelligence and cryptocurrency fields. https://github.com/ipergiove