class Indicators(): def __init__(self): self.DC=DataCollect() #Clean up the format of indicator to every indicator def Indic_gen(self,histprice,histindic): histindic=pd.DataFrame(histindic) histindic['date']=histprice['date'] histindic.columns=(['indic','date']) histindic=histindic[['date','indic']] return(histindic) def Onedayback(self,histindic): histindic['date']=histindic['date'].shift(-1) histindic=histindic.loc[histindic['date'].isnull()==False,:].copy() return(histindic) #Calculate the RSI of each index def RSI(self,ticker): histprice=self.DC.Hist_price(ticker) histindic=TA.momentum.rsi(histprice['close'],window=14) histindic=self.Indic_gen(histprice,histindic,ticker) histindic['indic']=np.nan histindic.loc[histindic['indic']>=70,['indic']]=0 histindic.loc[histindic['indic']<=50,['indic']]=1 histindic=histindic.fillna(method='ffill') return(histindic) def ADX(self,histprice): ADX=TA.trend.adx(histprice['high'], histprice['low'], histprice['close'],window=14,fillna=False) ADX_NEG=TA.trend.adx_neg(histprice['high'], histprice['low'], histprice['close'],window=14,fillna=False) ADX_POS=TA.trend.adx_pos(histprice['high'], histprice['low'], histprice['close'],window=14,fillna=False) adx=pd.DataFrame([ADX,ADX_NEG,ADX_POS]) adx=adx.transpose() adx['date']=histprice['date'] adx['adx_status']='NA' adx.loc[adx['adx_pos']>adx['adx_neg'],'adx_status']='bull' adx.loc[adx['adx_pos']<adx['adx_neg'],'adx_status']='bear' return(adx) def MACD(self,histprice): mdff=TA.trend.macd_diff(histprice['close']) masig=TA.trend.macd_signal(histprice['close']) macd=pd.DataFrame([mdff,masig]) macd=macd.transpose() macd['date']=histprice['date'] macd['diff']=macd['mdff']-macd['masig'] return(macd) def Kama(self,histprice): histindic=TA.momentum.kama(histprice['close']) return(histindic) def Ppo(self,histprice): histindic=TA.momentum.ppo(histprice['close']) return(histindic) def ROC(self,histprice): histindic=TA.momentum.ppo(histprice['close']) return(histindic)
class Indicators(): def __init__(self): self.DC = DataCollect() #Clean up the format of indicator to every indicator def Indic_gen(self, histprice, histindic): histindic = pd.DataFrame(histindic) histindic['date'] = histprice['date'] histindic.columns = (['indic', 'date']) histindic = histindic[['date', 'indic']] return (histindic) def Onedayback(self, histindic): histindic['date'] = histindic['date'].shift(-1) histindic = histindic.loc[histindic['date'].isnull() == False, :].copy() return (histindic) def MACD(self, ticker): histprice = self.DC.Hist_price(ticker) diff = TA.trend.macd_diff(histprice['close']) diff = self.Indic_gen(histprice, diff) diff['MACD_sig'] = np.nan diff.loc[diff['indic'] > 0, ['MACD_sig']] = 1 diff.loc[diff['indic'] <= 0, ['MACD_sig']] = 0 diff['MACD_sig'] = diff['MACD_sig'].fillna(method='ffill') diff = self.Onedayback(diff) return (diff) def SMA(self, ticker): histprice = self.DC.Hist_price(ticker) sma50 = TA.trend.sma_indicator(histprice['close'], window=50) sma50 = self.Indic_gen(histprice, sma50) sma50 = sma50.rename(columns={'indic': 'sma50'}) sma100 = TA.trend.sma_indicator(histprice['close'], window=100) sma100 = self.Indic_gen(histprice, sma100) sma100 = sma100.rename(columns={'indic': 'sma100'}) sma = pd.merge(sma50, sma100[['date', 'sma100']], on='date', how='left') sma = sma.loc[sma['sma100'].isnull() == False, :].copy() sma['diff'] = sma['sma50'] - sma['sma100'] sma['SMA_sig'] = np.nan sma.loc[sma['diff'] > 0, ['SMA_sig']] = 1 sma.loc[sma['diff'] <= 0, ['SMA_sig']] = 0 sma['diff'] = sma['diff'].fillna(method='ffill') sma = self.Onedayback(sma) return (sma) #Calculate the RSI of each index def RSI(self, ticker): histprice = self.DC.Hist_price(ticker) histindic = TA.momentum.rsi(histprice['close'], window=14) histindic = self.Indic_gen(histprice, histindic, ticker) histindic['indic'] = np.nan histindic.loc[histindic['indic'] >= 70, ['indic']] = 0 histindic.loc[histindic['indic'] <= 50, ['indic']] = 1 histindic = histindic.fillna(method='ffill') return (histindic) def ADX(self, histprice): histindic = TA.trend.adx(histprice['high'], histprice['low'], histprice['close'], window=14, fillna=False) return (histindic) def Kama(self, histprice): histindic = TA.momentum.kama(histprice['close']) return (histindic) def Ppo(self, histprice): histindic = TA.momentum.ppo(histprice['close']) return (histindic) def ROC(self, histprice): histindic = TA.momentum.ppo(histprice['close']) return (histindic)