def generate_tech_data(p1_df, p2_df): sample = pd.DataFrame( { 'p1': p1_df.values.ravel(), 'p2': p2_df.values.ravel() }, index=p1_df.index) p1 = p1_df.values.ravel() p2 = p2_df.values.ravel() sample['p1' + '_mom'] = talib.MOM(p1) sample['p1' + '_macd'], sample['p1' + '_macd_sig'], sample['p1' + '_macd_hist'] = talib.MACD(p1) sample['p1' + '_rsi'] = talib.RSI(p1, timeperiod=10) sample['p1' + '_cmo'] = talib.CMO(p1) sample['p2' + '_mom'] = talib.MOM(p2) sample['p2' + '_macd'], sample['p2' + '_macd_sig'], sample['p2' + '_macd_hist'] = talib.MACD(p2) sample['p2' + '_rsi'] = talib.RSI(p2, timeperiod=10) sample['p2' + '_cmo'] = talib.CMO(p2) spread = p1 / p2 sample['spread'] = spread sample['diff'] = sample['spread'] / sample['spread'].shift(1) sample = sample.dropna() return sample
def computeCMO(self): scmo = talib.CMO(self.prices, timeperiod=self.slowPeriod) fcmo = talib.CMO(self.prices, timeperiod=self.fastPeriod) cmo = fcmo - scmo cmo = scmo self.removeNullID(cmo) self.rawFeatures['CMO'] = cmo return
def cmo(candles: np.ndarray, period: int = 14, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ CMO - Chande Momentum Oscillator :param candles: np.ndarray :param period: int - default=14 :param source_type: str - default: "close" :param sequential: bool - default=False :return: float | np.ndarray """ warmup_candles_num = get_config('env.data.warmup_candles_num', 240) if not sequential and len(candles) > warmup_candles_num: candles = candles[-warmup_candles_num:] source = get_candle_source(candles, source_type=source_type) res = talib.CMO(source, timeperiod=period) if sequential: return res else: return None if np.isnan(res[-1]) else res[-1]
def generate_feature(data): high = data.High.values low = data.Low.values close = data.Close.values feature_df = pd.DataFrame(index=data.index) feature_df["ADX"] = ADX = talib.ADX(high, low, close, timeperiod=14) feature_df["ADXR"] = ADXR = talib.ADXR(high, low, close, timeperiod=14) feature_df["APO"] = APO = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["AROONOSC"] = AROONOSC = talib.AROONOSC(high, low, timeperiod=14) feature_df["CCI"] = CCI = talib.CCI(high, low, close, timeperiod=14) feature_df["CMO"] = CMO = talib.CMO(close, timeperiod=14) feature_df["DX"] = DX = talib.DX(high, low, close, timeperiod=14) feature_df["MINUS_DI"] = MINUS_DI = talib.MINUS_DI(high, low, close, timeperiod=14) feature_df["MINUS_DM"] = MINUS_DM = talib.MINUS_DM(high, low, timeperiod=14) feature_df["MOM"] = MOM = talib.MOM(close, timeperiod=10) feature_df["PLUS_DI"] = PLUS_DI = talib.PLUS_DI(high, low, close, timeperiod=14) feature_df["PLUS_DM"] = PLUS_DM = talib.PLUS_DM(high, low, timeperiod=14) feature_df["PPO"] = PPO = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["ROC"] = ROC = talib.ROC(close, timeperiod=10) feature_df["ROCP"] = ROCP = talib.ROCP(close, timeperiod=10) feature_df["ROCR100"] = ROCR100 = talib.ROCR100(close, timeperiod=10) feature_df["RSI"] = RSI = talib.RSI(close, timeperiod=14) feature_df["ULTOSC"] = ULTOSC = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) feature_df["WILLR"] = WILLR = talib.WILLR(high, low, close, timeperiod=14) feature_df = feature_df.fillna(0.0) matrix = np.stack(( ADX, ADXR, APO, AROONOSC, CCI, CMO, DX, MINUS_DI, ROCR100, ROC, MINUS_DM, MOM, PLUS_DI, PLUS_DM, PPO, ROCP, WILLR, ULTOSC, RSI)) matrix = np.nan_to_num(matrix) matrix = matrix.transpose() return feature_df, matrix
def SourceData(SourceStart, SourceEnd): Data1 = { "close " : quandl.get("LBMA/GOLD", start_date=SourceStart, end_date=SourceEnd)["USD (PM)"].dropna(), } Data2 = { "MA " : talib.MA(Data1["close "], timeperiod=30, matype=0), "EMA " : talib.EMA(Data1["close "], timeperiod=30), "CMO " : talib.CMO(Data1["close "], timeperiod=14), "MACD1 " : talib.MACD(Data1["close "], fastperiod=12, slowperiod=26, signalperiod=9)[0], "MACD2 " : talib.MACD(Data1["close "], fastperiod=12, slowperiod=26, signalperiod=9)[1], "MACD3 " : talib.MACD(Data1["close "], fastperiod=12, slowperiod=26, signalperiod=9)[2], } Data3 = { "Crude " : quandl.get("EIA/PET_RWTC_D", start_date=SourceStart, end_date=SourceEnd), "CNY " : quandl.get("FED/RXI_N_B_CH", start_date=SourceStart, end_date=SourceEnd)["Value"], "Uncer " : quandl.get("FRED/WLEMUINDXD", start_date=SourceStart, end_date=SourceEnd), "Unccn " : quandl.get("FRED/CHIEPUINDXM", start_date=SourceStart, end_date=SourceEnd), "VIX " : quandl.get("CHRIS/CBOE_VX1", start_date=SourceStart, end_date=SourceEnd)["Open"], "AAII " : quandl.get("AAII/AAII_SENTIMENT", start_date=SourceStart, end_date=SourceEnd)["Bullish"], } DataDic = {**Data1, **Data2, **Data3} data_cont = list(DataDic.values()) data_name = list(DataDic.keys()) return (data_cont, data_name)
def compCMO(self): cmo = talib.CMO(self.close,timeperiod=self.lookback) self.removeNullID(cmo) self.rawFeatures['CMO'] = cmo FEATURE_SIZE_DICT['CMO'] = 1 return
def extract_features(data): high = data['High'] low = data['Low'] close = data['Close'] volume = data['Volume'] open_ = data['Open'] data['ADX'] = ta.ADX(high, low, close, timeperiod=19) data['CCI'] = ta.CCI(high, low, close, timeperiod=19) data['CMO'] = ta.CMO(close, timeperiod=14) data['MACD'], X, Y = ta.MACD(close, fastperiod=10, slowperiod=30, signalperiod=9) data['MFI'] = ta.MFI(high, low, close, volume, timeperiod=19) data['MOM'] = ta.MOM(close, timeperiod=9) data['ROCR'] = ta.ROCR(close, timeperiod=12) data['RSI'] = ta.RSI(close, timeperiod=19) data['STOCHSLOWK'], data['STOCHSLOWD'] = ta.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) data['TRIX'] = ta.TRIX(close, timeperiod=30) data['WILLR'] = ta.WILLR(high, low, close, timeperiod=14) data['OBV'] = ta.OBV(close, volume) data['TSF'] = ta.TSF(close, timeperiod=14) data['NATR'] = ta.NATR(high, low, close)#, timeperiod=14) data['ULTOSC'] = ta.ULTOSC(high, low, close) data['AROONOSC'] = ta.AROONOSC(high, low, timeperiod=14) data['BOP'] = ta.BOP(open_, high, low, close) data['LINEARREG'] = ta.LINEARREG(close) data['AP0'] = ta.APO(close, fastperiod=9, slowperiod=23, matype=1) data['TEMA'] = ta.TRIMA(close, 29) return data
def _compute_states(self): start = self.current_idx - self.lookback end = self.current_idx df = self._data.iloc[start:end, :] states = {} states['roc1'] = ta.ROC(df.close, timeperiod=5).iloc[-1] states['roc2'] = ta.ROC(df.close, timeperiod=20).iloc[-1] states['rsi1'] = ta.RSI(df.close, timeperiod=14).iloc[-1] states['rsi2'] = ta.RSI(df.close, timeperiod=28).iloc[-1] states['cci1'] = ta.CCI(df.high, df.low, df.close, timeperiod=14).iloc[-1] states['cci2'] = ta.CCI(df.high, df.low, df.close, timeperiod=28).iloc[-1] states['adx1'] = ta.ADX(df.high, df.low, df.close, timeperiod=14).iloc[-1] states['adx2'] = ta.ADX(df.high, df.low, df.close, timeperiod=28).iloc[-1] states['atr'] = ta.ATR(df.high, df.low, df.close, timeperiod=14).iloc[-1] states['aroon1'] = ta.AROONOSC(df.high, df.low, timeperiod=14).iloc[-1] states['aroon2'] = ta.AROONOSC(df.high, df.low, timeperiod=28).iloc[-1] states['bop'] = ta.BOP(df.open, df.high, df.low, df.close).iloc[-1] states['cmo'] = ta.CMO(df.close, timeperiod=14).iloc[-1] states['close'] = df.close.iloc[-1] states['date'] = df.index[-1] states['position'] = self.position states['entry_price'] = self.entry_price states['entry_date'] = self.entry_date return states
def calculations(self): '''calculations''' self.df['rsi'] = ta.RSI(self.df['close'], timeperiod=5) self.df['apo'] = ta.APO(self.df['close'], fastperiod=10, slowperiod=5, matype=0) self.df['upperband'], self.df['middleband'], self.df[ 'lowerband'] = ta.BBANDS(self.df['close'], timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) self.df['ema'] = ta.EMA(self.df['close'], timeperiod=5) self.df['ma'] = ta.MA(self.df['close'], timeperiod=5, matype=0) self.df['sma'] = ta.MA(self.df['close'], timeperiod=5) self.df['t3'] = ta.T3(self.df['close'], timeperiod=5, vfactor=0) self.df['wma'] = ta.WMA(self.df['close'], timeperiod=5) self.df['aroonosc'] = ta.AROONOSC(self.df['high'], self.df['low'], timeperiod=5) self.df['cci'] = ta.CCI(self.df['high'], self.df['low'], self.df['close'], timeperiod=5) self.df['cmo'] = ta.CMO(self.df['close'], timeperiod=14) self.df['macd'], self.df['macdsignal'], self.df[ 'macdhist'] = ta.MACDEXT(self.df['close'], fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) self.df['slowk'], self.df['slowd'] = ta.STOCH(self.df['high'], self.df['low'], self.df['close'], fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) self.df['fastk'], self.df['fastd'] = ta.STOCHRSI(self.df['close'], timeperiod=5, fastk_period=5, fastd_period=3, fastd_matype=0) self.df['ultosc'] = ta.ULTOSC(self.df['high'], self.df['low'], self.df['close'], timeperiod1=7, timeperiod2=14, timeperiod3=28) self.df['adosc'] = ta.ADOSC(self.df['high'], self.df['low'], self.df['close'], self.df['volume'], fastperiod=3, slowperiod=10) return self.df
def getMomentumIndicators(df): high = df['High'] low = df['Low'] close = df['Close'] open = df['Open'] volume = df['Volume'] df['ADX'] = ta.ADX(high, low, close, timeperiod=14) df['SMA'] = ta.ADXR(high, low, close, timeperiod=14) df['APO'] = ta.APO(close, fastperiod=12, slowperiod=26, matype=0) df['AROONDOWN'], df['AROOONUP'] = ta.AROON(high, low, timeperiod=14) df['AROONOSC'] = ta.AROONOSC(high, low, timeperiod=14) df['BOP'] = ta.BOP(open, high, low, close) df['CCI'] = ta.CCI(high, low, close, timeperiod=14) df['CMO'] = ta.CMO(close, timeperiod=14) df['DX'] = ta.DX(high, low, close, timeperiod=14) df['MACD'], df['MACDSIGNAL'], df['MACDHIST'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) df['MFI'] = ta.MFI(high, low, close, volume, timeperiod=14) df['MINUS_DI'] = ta.MINUS_DI(high, low, close, timeperiod=14) df['MINUS_DM']= ta.MINUS_DM(high, low, timeperiod=14) df['MOM'] = ta.MOM(close, timeperiod=10) df['PLUS_DM'] =ta.PLUS_DM(high, low, timeperiod=14) df['PPO'] = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0) df['ROC'] = ta.ROC(close, timeperiod=10) df['ROCP'] = ta.ROCP(close, timeperiod=10) df['ROCR'] = ta.ROCR(close, timeperiod=10) df['ROCR100'] = ta.ROCR100(close, timeperiod=10) df['RSI'] = ta.RSI(close, timeperiod=14) df['SLOWK'], df['SLOWD'] = ta.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df['FASTK'], df['FASTD'] = ta.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) df['FASTK2'], df['FASTD2'] = ta.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) df['TRIX'] = ta.TRIX(close, timeperiod=30) df['ULTOSC'] = ta.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) df['WILLR'] = ta.WILLR(high, low, close, timeperiod=14)
def rebalance(context, data): history = data.history(assets=context.asset, fields=['close'], bar_count=context.cmo_window + 1, frequency='1d') date = history.index.values[-1] close = history['close'].values # 计算CMO指标 cmo = ta.CMO(close, timeperiod=context.cmo_window) print(cmo[-1]) current_price = data[context.asset].price record(price=current_price) buy_signal_triggered = False sell_signal_triggered = False if cmo[-1] < context.over_sell: buy_signal_triggered = True elif cmo[-1] > context.over_buy: sell_signal_triggered = True current_position = context.portfolio.positions[context.asset].amount if buy_signal_triggered and current_position == 0: print(str(date) + '==>Buy') order_target_percent(context.asset, 1.0) elif sell_signal_triggered and current_position > 0: print(str(date) + '==>Sell') order_target_percent(context.asset, 0.0) else: print("No trading")
def momentum(self): adx = talib.ADX(self.high,self.low,self.close,self.period) adxr = talib.ADXR(self.high,self.low,self.close,self.period) apo = talib.APO(self.high,self.low,self.close,self.period) aroondown, aroonup = talib.AROON(self.high, self.low, period) aroonosc = talib.AROONOSC(self.high,self.low,self.period) bop = talib.BOP(self.opens,self.high,self.low,self.close) cci = talib.CCI(self.high,self.low,self.close,self.period) cmo = talib.CMO(self.close,self.period) dx = talib.DX(self.high,self.low,self.close,self.period) macd, macdsignal, macdhist = talib.MACD(self.close, fastperiod=period, slowperiod=period*5, signalperiod=period*2) macd1, macdsignal1, macdhist1 = talib.MACDEXT(self.close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) macd2, macdsignal2, macdhist2 = talib.MACDFIX(self.close, signalperiod=9) mfi = talib.MFI(self.high, self.low, self.close, self.volume, timeperiod=14) minus_di = talib.MINUS_DI(self.high, self.low, self.close, timeperiod=14) minus_dm = talib.MINUS_DM(self.high, self.low, timeperiod=14) mom = talib.MOM(self.close, timeperiod=10) plus_di = talib.PLUS_DI(self.high, self.low, self.close, timeperiod=14) plus_dm = talib.PLUS_DM(self.high, self.low, timeperiod=14) ppo = talib.PPO(self.close, fastperiod=12, slowperiod=26, matype=0) roc = talib.ROC(self.close, timeperiod=10) rocp = talib.ROCP(self.close, timeperiod=10) rocr = talib.ROCR(self.close, timeperiod=10) rocr100 = talib.ROCR100(self.close, timeperiod=10) rsi = talib.RSI(self.close, timeperiod=14) slowk, slowd = talib.STOCH(self.high, self.low, self.close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) fastk, fastd = talib.STOCHF(self.high, self.low, self.close, fastk_period=5, fastd_period=3, fastd_matype=0) fastk1, fastd1 = talib.STOCHRSI(self.close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) trix = talib.TRIX(self.close, timeperiod=30) ultosc = talib.ULTOSC(self.high, self.low, self.close, timeperiod1=7, timeperiod2=14, timeperiod3=28) willr = talib.WILLR(self.high, self.low, self.close, timeperiod=14)
def cmo(close_ts, timeperiod=14): import talib close_np = close_ts.cpu().detach().numpy() close_df = pd.DataFrame(close_np) cmo = close_df.apply(lambda x: talib.CMO(x, timeperiod=14)) CMO_ts = torch.tensor(cmo.values, dtype=close_ts.dtype, device=close_ts.device) return CMO_ts
def CMO(self, stockhist): cmo = stockhist.copy() cmo['cmo'] = tal.CMO(stockhist['close']) cmo['signal'] = 0 cmo.loc[cmo['cmo'] > 25, 'signal'] = 1 cmo['signal'] = cmo['signal'].fillna(0) cmo = cmo[['date', 'dailyreturn', 'signal']].copy() return (cmo)
def cmo(self, n: int, array: bool = False) -> Union[float, np.ndarray]: """ CMO. """ result = talib.CMO(self.close, n) if array: return result return result[-1]
def cmo(self, n, array=False): """ CMO. """ result = talib.CMO(self.close, n) if array: return result return result[-1]
def create_technical_indicators(df, indicators=['MA_4'], only_close_colum=False): if only_close_colum: if isinstance(df, pd.DataFrame): close = np.array(df['Close']).reshape(len(df), ) else: if isinstance(df, pd.DataFrame): close = np.array(df['Close']).reshape(len(df), ) # OPEN = np.array(df['Open']).reshape(len(df), ) high = np.array(df['High']).reshape(len(df), ) low = np.array(df['Low']).reshape(len(df), ) df_2_return = pd.DataFrame() for indicator in indicators: if indicator is None: pass else: if indicator.split('_')[0].strip() == 'MA': smavg = talib.MA(close, timeperiod=int(indicator.split('_')[1])) df_2_return[indicator] = smavg if indicator.split('_')[0].strip() == 'BBANDS': upperband, middleband, lowerband = talib.BBANDS(close, timeperiod=int(indicator.split('_')[1])) df_2_return['lowerband'] = lowerband df_2_return[indicator] = middleband df_2_return['upperband'] = upperband if indicator.split('_')[0].strip() == 'MACD': indicator = indicator + '_12' + '_26' + '_9' _, macdsignal, macdhist = talib.MACD(close, fastperiod=int(indicator.split('_')[1]), slowperiod=int(indicator.split('_')[2]), signalperiod=int(indicator.split('_')[3])) df_2_return['macdsignal'] = macdsignal df_2_return['macdhist'] = macdhist if indicator.split('_')[0].strip() == 'RSI': real_RSI = talib.RSI(close, timeperiod=int(indicator.split('_')[1])) df_2_return[indicator] = real_RSI if indicator.split('_')[0].strip() == 'CMO': real_CMO = talib.CMO(close, timeperiod=int(indicator.split('_')[1])) df_2_return[indicator] = real_CMO if indicator.split('_')[0].strip() == 'ADX': if only_close_colum: pass else: real_ADX = talib.ADX(high, low, close, timeperiod=int(indicator.split('_')[1])) df_2_return[indicator] = real_ADX if indicator.split('_')[0].strip() == 'STOCH': if only_close_colum: pass else: slowk, slowd = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df_2_return[indicator] = slowd - slowk return(df_2_return)
def CMO(self, timeperiod=14): real_data = np.array([self.df.close], dtype='f8') cmo = talib.CMO(real_data[0], timeperiod=timeperiod) # return go.Scatter( # x=self.df.index, # y=cmo, # name='CMO' # ) return cmo
def test_cmo(): """test TA.CMO""" cmo = TA.CMO(ohlc, period=9) talib_cmo = talib.CMO(ohlc["close"], timeperiod=9) # assert round(talib_cmo[-1], 2) == round(cmo.values[-1], 2) # assert -35.99 == -35.66 pass # close enough
def cmo(self, sym, frequency, period=14): if not self.kbars_ready(sym, frequency): return [] closes = self.close(sym, frequency) cmo = ta.CMO(closes, timeperiod=period) return cmo
def CMO(self, stk_no, start='2008/01/01', end='2016/12/31', timeperiod=14): realstart = day_back(start, timeperiod) close = self.get_stock_data(stk_no, 'ClosePrice', realstart, end) if np.isnan(close).all(): return [] cmo = talib.CMO(close, timeperiod) if np.isnan(cmo).all(): return [] else: return cmo[timeperiod:]
def get_mo(price_arr, cur_pos, period=20): if cur_pos <= (period + 1): return 0 else: s = cur_pos - (period + 1) tmp_arr = price_arr[s:cur_pos] tmp_arr.reverse() prices = np.array(tmp_arr, dtype=float) return ta.CMO(prices, timeperiod=period)[-1]
def generate_feature(data): high = data.High.values low = data.Low.values close = data.Close.values # feature_df = pd.DataFrame(index=data.index) feature_df = data.copy() feature_df["ADX"] = ADX = talib.ADX(high, low, close, timeperiod=14) feature_df["ADXR"] = ADXR = talib.ADXR(high, low, close, timeperiod=14) feature_df["APO"] = APO = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["AROONOSC"] = AROONOSC = talib.AROONOSC(high, low, timeperiod=14) feature_df["CCI"] = CCI = talib.CCI(high, low, close, timeperiod=14) feature_df["CMO"] = CMO = talib.CMO(close, timeperiod=14) feature_df["DX"] = DX = talib.DX(high, low, close, timeperiod=14) feature_df["MINUS_DI"] = MINUS_DI = talib.MINUS_DI(high, low, close, timeperiod=14) feature_df["MINUS_DM"] = MINUS_DM = talib.MINUS_DM(high, low, timeperiod=14) feature_df["MOM"] = MOM = talib.MOM(close, timeperiod=10) feature_df["PLUS_DI"] = PLUS_DI = talib.PLUS_DI(high, low, close, timeperiod=14) feature_df["PLUS_DM"] = PLUS_DM = talib.PLUS_DM(high, low, timeperiod=14) feature_df["PPO"] = PPO = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["ROC"] = ROC = talib.ROC(close, timeperiod=10) feature_df["ROCP"] = ROCP = talib.ROCP(close, timeperiod=10) feature_df["ROCR100"] = ROCR100 = talib.ROCR100(close, timeperiod=10) feature_df["RSI"] = RSI = talib.RSI(close, timeperiod=14) feature_df["ULTOSC"] = ULTOSC = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) feature_df["WILLR"] = WILLR = talib.WILLR(high, low, close, timeperiod=14) feature_df = feature_df.fillna(0.0) # Exclude columns you don't want feature_df = feature_df[feature_df.columns[ ~feature_df.columns.isin(['Open', 'High', 'Low', 'Close'])]] matrix = feature_df.values return feature_df, matrix
def _get_indicators(security, open_name, close_name, high_name, low_name, volume_name): """ expand the features of the data through technical analysis across 26 different signals :param security: data which features are going to be expanded :param open_name: open price column name :param close_name: close price column name :param high_name: high price column name :param low_name: low price column name :param volume_name: traded volumn column name :return: expanded and extracted data """ open_price = security[open_name].values close_price = security[close_name].values low_price = security[low_name].values high_price = security[high_name].values volume = security[volume_name].values if volume_name else None security['MOM'] = talib.MOM(close_price) security['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price) security['HT_DCPHASE'] = talib.HT_DCPHASE(close_price) security['SINE'], security['LEADSINE'] = talib.HT_SINE(close_price) security['INPHASE'], security['QUADRATURE'] = talib.HT_PHASOR( close_price) security['ADXR'] = talib.ADXR(high_price, low_price, close_price) security['APO'] = talib.APO(close_price) security['AROON_UP'], _ = talib.AROON(high_price, low_price) security['CCI'] = talib.CCI(high_price, low_price, close_price) security['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price) security['PPO'] = talib.PPO(close_price) security['MACD'], security['MACD_SIG'], security[ 'MACD_HIST'] = talib.MACD(close_price) security['CMO'] = talib.CMO(close_price) security['ROCP'] = talib.ROCP(close_price) security['FASTK'], security['FASTD'] = talib.STOCHF( high_price, low_price, close_price) security['TRIX'] = talib.TRIX(close_price) security['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price) security['WILLR'] = talib.WILLR(high_price, low_price, close_price) security['NATR'] = talib.NATR(high_price, low_price, close_price) security['RSI'] = talib.RSI(close_price) security['EMA'] = talib.EMA(close_price) security['SAREXT'] = talib.SAREXT(high_price, low_price) # security['TEMA'] = talib.EMA(close_price) security['RR'] = security[close_name] / security[close_name].shift( 1).fillna(1) security['LOG_RR'] = np.log(security['RR']) if volume_name: security['MFI'] = talib.MFI(high_price, low_price, close_price, volume) # security['AD'] = talib.AD(high_price, low_price, close_price, volume) # security['OBV'] = talib.OBV(close_price, volume) security[volume_name] = np.log(security[volume_name]) security.drop([open_name, close_name, high_name, low_name], axis=1) security = security.dropna().astype(np.float32) return security
def add_cmo(dataset,param, first_header): field_name = 'cmo' + str(param) df = dataset[first_header].copy() close = df['Close'] cmo = talib.CMO(close, timeperiod=14)/100 col = pd.DataFrame(cmo, index=df.index) dataset[first_header, field_name] = col return dataset
def indicators(data): """ :param data: data :return: np array of each indicator """ cmo = talib.CMO(np.array(data), timeperiod=10).reshape(-1, 1) roc = talib.ROC(np.array(data), timeperiod=5).reshape(-1, 1) rsi = talib.RSI(np.array(data), timeperiod=5).reshape(-1, 1) wma = talib.WMA(np.array(data), timeperiod=20).reshape(-1, 1) ppo = talib.PPO(np.array(data), fastperiod=10, slowperiod=20, matype=0).reshape(-1, 1) return cmo, roc, rsi, wma, ppo
def generate_tech_data(stock): price = stock.values name = stock.name data = pd.DataFrame(stock) data[name + '_mom'] = talib.MOM(price) data[name + '_macd'], data[name + '_macd_sig'], data[name + '_macd_hist'] = talib.MACD(price) data[name + '_rsi'] = talib.RSI(price, timeperiod=10) data[name + '_cmo'] = talib.CMO(price) data = data.drop(name, axis=1) data = data.dropna() return data
def get_momentum_studies(open, low, high, close, volume, df): # Momentum studies # https://mrjbq7.github.io/ta-lib/func_groups/momentum_indicators.html df['MACD'], df['MACD_SIGN'], df['MACD_HIST'] = talib.MACD( close, fastperiod=12, slowperiod=26, signalperiod=9) df['STOCH-SLOW-K'], df['STOCH-SLOW-D'] = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df['STOCH-FAST-K'], df['STOCH-FAST-D'] = talib.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) df['STOCH-RSI-K'], df['STOCH-RSI-D'] = talib.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) df['AROON-DOWN'], df['AROON-UP'] = talib.AROON(high, low, timeperiod=14) df["MINUS_DI"] = talib.MINUS_DI(high, low, close, timeperiod=14) df["MINUS_DM"] = talib.MINUS_DM(high, low, timeperiod=14) df["PLUS_DI"] = talib.PLUS_DI(high, low, close, timeperiod=14) df["PLUS_DM"] = talib.PLUS_DM(high, low, timeperiod=14) df["MOM"] = talib.MOM(close, timeperiod=10) df["MFI"] = talib.MFI(high, low, close, volume, timeperiod=14) df["ADX"] = talib.ADX(high, low, close, timeperiod=14) df["ADXR"] = talib.ADXR(high, low, close, timeperiod=14) df["APO"] = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) df["AROONOSC"] = talib.AROONOSC(high, low, timeperiod=14) df["BOP"] = talib.BOP(open, high, low, close) df["CCI"] = talib.CCI(high, low, close, timeperiod=14) df["CMO"] = talib.CMO(close, timeperiod=14) df["DX"] = talib.DX(high, low, close, timeperiod=14) df["PPO"] = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) df["ROC"] = talib.ROC(close, timeperiod=10) df["RSI"] = talib.RSI(close, timeperiod=14) df["TRIX"] = talib.TRIX(close, timeperiod=30) df["ULT"] = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) df["WILLR"] = talib.WILLR(high, low, close, timeperiod=14)
def add_CMO(self, timeperiod=14, type='line', color='secondary', **kwargs): """Chande Momentum Indicator.""" if not self.has_close: raise Exception() utils.kwargs_check(kwargs, VALID_TA_KWARGS) if 'kind' in kwargs: type = kwargs['kind'] name = 'CMO({})'.format(str(timeperiod)) self.sec[name] = dict(type=type, color=color) self.ind[name] = talib.CMO(self.df[self.cl].values, timeperiod)
def test_CMO(self): class MyCMO(OperatorCMO): def __init__(self, name, **kwargs): super(MyCMO, self).__init__(100, name, **kwargs) self.env.add_operator('cmo', { 'operator': MyCMO, }) string = 'cmo(14, open)' gene = self.env.parse_string(string) self.assertRaises(IndexError, gene.eval, self.env, self.dates[98], self.dates[-1]) df = gene.eval(self.env, self.dates[99], self.dates[100]) ser0, ser1 = df.iloc[0], df.iloc[1] o = self.env.get_data_value('open').values res0, res1, res = [], [], [] for i, val in ser0.iteritems(): res0.append(talib.CMO(o[:100, i], timeperiod=14)[-1] == val) for i, val in ser1.iteritems(): res1.append(talib.CMO(o[1:100 + 1, i], timeperiod=14)[-1] == val) res.append(talib.CMO(o[:100 + 1, i], timeperiod=14)[-1] != val) self.assertTrue(all(res0) and all(res1) and any(res))