def label_func(data, snp_vector, vector, complete_datum, index, window=14): if len(snp_vector) <= index + window: return None windowed_index = index + window roc_vector = tl.ROC(vector, timeperiod=window) snp_roc = tl.ROC(snp_vector, timeperiod=window) beta = tl.BETA(roc_vector, snp_roc, timeperiod=window) expected_return = beta[windowed_index] * (snp_roc[windowed_index] - RISK_FREE) excess = beta[windowed_index] * (snp_roc[windowed_index] - RISK_FREE) + RISK_FREE label = 'buy' if data[windowed_index]['Open'] < data[windowed_index]['Close'] else 'stay' #print 'stock roc: %f, SnP roc: %f, beta: %f, exp-ret.: %f' %\ #(roc_vector[index], snp_roc[index], beta[index], RISK_FREE + beta[index] * (snp_roc[index] - RISK_FREE))) if expected_return > 0: bx = 1 else: bx = -1 complete_datum['bx'] = bx complete_datum['excess'] = excess complete_datum['label'] = label return complete_datum
def process_raw(self, csv_file): df = self._file df['dt'] = df['Date'].astype(str) + " " + df['Time'].astype(str) df['DateTime'] = pd.to_datetime(df['dt'], format=' % Y % m % d % H: % M: % S', errors='ignore') df = df.set_index(['DateTime']) df = df.drop(['General', 'Date', 'Time', 'dt'], axis=1) df['EMA10'] = ta.EMA(df['Close'], timeperiod=10) df['EMA30'] = ta.EMA(df['Close'], timeperiod=30) df['EMA60'] = ta.EMA(df['Close'], timeperiod=60) df['SMA10'] = ta.SMA(df['Close'], timeperiod=10) df['SMA30'] = ta.SMA(df['Close'], timeperiod=30) df['SMA60'] = ta.SMA(df['Close'], timeperiod=60) df['WMA10'] = ta.WMA(df['Close'], timeperiod=10) df['WMA30'] = ta.WMA(df['Close'], timeperiod=30) df['WMA60'] = ta.WMA(df['Close'], timeperiod=60) df['RSI10'] = ta.RSI(df['Close'], timeperiod=10) df['RSI30'] = ta.RSI(df['Close'], timeperiod=30) df['RSI60'] = ta.RSI(df['Close'], timeperiod=60) df['MOM10'] = ta.MOM(df['Close'], timeperiod=10) df['MOM30'] = ta.MOM(df['Close'], timeperiod=30) df['MOM60'] = ta.MOM(df['Close'], timeperiod=60) df['ROC10'] = ta.ROC(df['Close'], timeperiod=10) df['ROC50'] = ta.ROC(df['Close'], timeperiod=30) df['ROC60'] = ta.ROC(df['Close'], timeperiod=60) df.to_csv(csv_file) return df
def roc(apply_price, long_period, short_period, u_border, l_border, u_band, l_band): """ Parameters ---------- apply_price:价格序列 long_period:长期roc的周期 short_period:短期roc的周期 u_border:长期roc的上界 l_border:长期roc的下界 u_band:均衡线上轨的宽度 l_band:均衡线下轨的宽度 """ long_roc = talib.ROC(apply_price, timeperiod=long_period) short_roc = talib.ROC(apply_price, timeperiod=short_period) _signal_pos = [] u_equilibrium = 100 + u_band l_equilibrium = 100 - l_band for index in range(len(apply_price)): # 卖出点:当长期ROC大于上界且短期ROC在均衡线附近 if long_roc[index] > u_border and (l_equilibrium <= short_roc[index] <= u_equilibrium): _signal_pos.append((index, 1)) continue # 买入点:当长期ROC小于下界且短期ROC在均衡线附近 if long_roc[index] < l_border and (l_equilibrium <= short_roc[index] <= u_equilibrium): _signal_pos.append((index, -1)) continue return _signal_pos
def cc(candles: np.ndarray, wma_period: int = 10, roc_short_period: int = 11, roc_long_period: int = 14, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ CC - Coppock Curve :param candles: np.ndarray :param wma_period: int - default: 10 :param roc_short_period: int - default: 11 :param roc_long_period: int - default: 14 :param source_type: str - default: "close" :param sequential: bool - default=False :return: float | np.ndarray """ if not sequential and len(candles) > 240: candles = candles[-240:] source = get_candle_source(candles, source_type=source_type) res = talib.WMA(talib.ROC(source, timeperiod=roc_long_period) + talib.ROC(source, timeperiod=roc_short_period), timeperiod=wma_period) return res if sequential else res[-1]
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 computeDROC(self): sroc = talib.ROC(self.prices, timeperiod=self.slowPeriod) froc = talib.ROC(self.prices, timeperiod=self.fastPeriod) droc = froc - sroc droc = sroc self.removeNullID(droc) self.rawFeatures['ROC'] = droc return
def cor(self, sym1, sym2, frequency, period=10): if not self.kbars_ready(sym1, frequency) or not self.kbars_ready( sym2, frequency): return [] close1 = self.close(sym1, frequency) close2 = self.close(sym2, frequency) roc1 = ta.ROC(close1, timeperiod=period) roc2 = ta.ROC(close2, timeperiod=period) return ta.CORREL(roc1, roc2, timeperiod=period)
def kst(candles: np.ndarray, sma_period1: int = 10, sma_period2: int = 10, sma_period3: int = 10, sma_period4: int = 15, roc_period1: int = 10, roc_period2: int = 15, roc_period3: int = 20, roc_period4: int = 30, signal_period: int = 9, source_type: str = "close", sequential: bool = False) -> KST: """ Know Sure Thing (KST) :param candles: np.ndarray :param sma_period1: int - default=10 :param sma_period2: int - default=10 :param sma_period3: int - default=10 :param sma_period4: int - default=15 :param roc_period1: int - default=10 :param roc_period2: int - default=15 :param roc_period3: int - default=20 :param roc_period4: int - default=30 :param signal_period: int - default=9 :param source_type: str - default: "close" :param sequential: bool - default=False :return: KST(line, signal) """ 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) aroc1 = talib.SMA(talib.ROC(source, timeperiod=roc_period1), sma_period1) aroc2 = talib.SMA(talib.ROC(source, timeperiod=roc_period2), sma_period2) aroc3 = talib.SMA(talib.ROC(source, timeperiod=roc_period3), sma_period3) aroc4 = talib.SMA(talib.ROC(source, timeperiod=roc_period4), sma_period4) line = aroc1[len(aroc1) - len(aroc4):] + 2 * aroc2[len(aroc2) - len(aroc4):] + \ 3 * aroc3[len(aroc3) - len(aroc4):] + 4 * aroc4 signal = talib.SMA(line, signal_period) if sequential: return KST(line, signal) else: return KST(line[-1], signal[-1])
def addTechnicalFeatures(df): df['Data'] = pd.to_datetime(df['Data']) df.columns = ['Data', 'Open', 'High', 'Low', 'Close', 'Wol'] #Wskazniki analizy technicznej k, dfast = talib.STOCH(np.array(df['High']), np.array( df['Low']), np.array(df['Close'])) # uses high, low, close by default df['k'] = k df['dfast'] = dfast df['dslow'] = talib.SMA(dfast, timeperiod=5) df['momentum'] = talib.MOM(np.array(df['Close']), timeperiod=4) df['roc'] = talib.ROC(np.array(df['Close']), timeperiod=5) df['willR'] = talib.WILLR(np.array(df['High']), np.array(df['Low']), np.array(df['Close']), timeperiod=5) #ad = talib.ADOSC(np.array(df['High']), np.array(df['Low']), # np.array(df['Close']), np.array(df['Wol'])) df['disp5'] = df['Close'] / talib.SMA(np.array(df['Close']), 5) * 100 df['disp10'] = df['Close'] / talib.SMA(np.array(df['Close']), 10) * 100 df['oscp'] = ((talib.SMA(np.array(df['Close']), 5) - talib.SMA(np.array(df['Close']), 10)) / talib.SMA(np.array(df['Close']), 5)) df['rsi'] = talib.RSI(np.array(df['Close'])) df['CCI'] = talib.CCI(np.array(df['High']), np.array(df['Low']), np.array(df['Close'])) #Tworzenie zmiennej celu df['target1'] = df['Close'].shift(-1) - df['Open'] df['target5'] = df['Close'].shift(-5) - df['Open'] df['target3'] = df['Close'].shift(-3) - df['Open'] #zostawienie tylko zmiennej celu i wskaznikow technicznych df.drop(['Data', 'Open', 'High', 'Low', 'Close', 'Wol'], axis=1, inplace=True) return df
def hsmadata(self, llen, slen): #code 只能是商品 hsmadata = pd.DataFrame() for code in self.hsmaall.code.unique(): hsma0 = self.hsmaall[self.hsmaall.code == code].copy() hsma0['ratio'] = 0 hsma0['roc'] = talib.ROC(hsma0.close.values, timeperiod=slen) hsma0['dayr'] = hsma0.close / hsma0.open - 1 hsma0['dayh'] = hsma0.close / hsma0.high - 1 hsma0['dayl'] = hsma0.close / hsma0.low - 1 hsma0['dayhl'] = hsma0.high / hsma0.low - 1 hsma0['smar'] = hsma0['close'] / talib.SMA(hsma0.close.values, timeperiod=llen) - 1 hsma0['VAR'] = talib.STDDEV(hsma0.close.values, timeperiod=llen) / hsma0['close'] - 1 hsma0['timepos'] = 0 for d in hsma0['date'].unique(): hsmad = hsma0[hsma0.date == d] hsma0.loc[hsma0.date == d, 'ratio'] = ( hsmad.close.iloc[hsmad.shape[0] - 1] / hsmad.close - 1).values hsma0.loc[hsma0.date == d, 'timepos'] = ( (pd.Series(range(hsmad.shape[0])) + 1) / hsmad.shape[0]).values hsma0 = hsma0.dropna() hsmadata = pd.concat([hsmadata, hsma0], ignore_index=True) return hsmadata
def kst(prices, roc1=10, roc2=15, roc3=20, roc4=30, sma1=10, sma2=10, sma3=10, sma4=15, smasig=9): rcma1 = ta.SMA(ta.ROC(prices, timeperiod=roc1), timeperiod=sma1) rcma2 = ta.SMA(ta.ROC(prices, timeperiod=roc2), timeperiod=sma2) rcma3 = ta.SMA(ta.ROC(prices, timeperiod=roc3), timeperiod=sma3) rcma4 = ta.SMA(ta.ROC(prices, timeperiod=roc4), timeperiod=sma4) kst = 1.0 * rcma1 + 2.0 * rcma2 + 3.0 * rcma3 + 4.0 * rcma4 return ta.SMA(kst, timeperiod=smasig)
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 get_feature_df(df, company_symbol=None, feature="Trend"): if (company_symbol != None): df_comp = df[df["symbol"] == company_symbol] else: df_comp = df df_comp = df_comp.sort_values(by=['time']) if (feature == "Trend"): df_comp[feature] = ta.EMA(df_comp.close, timeperiod=90) elif (feature == "Volatility"): df_comp[feature] = ta.ATR(df_comp.high, df_comp.low, df_comp.close, timeperiod=90) elif (feature == "Volume"): df_comp[feature] = ta.ADOSC(df_comp.high, df_comp.low, df_comp.close, df_comp.volume, fastperiod=30, slowperiod=90) elif (feature == "Momemtum"): df_comp[feature + "_1"] = ta.RSI(df_comp.close, timeperiod=90) df_comp[feature + "_2"] = ta.ROC(df_comp.close, timeperiod=90) elif (feature == "Open_Close"): df_comp[feature + "_1"] = df_comp["open"] df_comp[feature + "_2"] = df_comp["close"] return df_comp
def compROC(self): roc = talib.ROC(self.close,timeperiod=self.lookback) self.removeNullID(roc) self.rawFeatures['ROC'] = roc FEATURE_SIZE_DICT['ROC'] = 1 return
def get_tis(df, dropnan=True, drop_vol=True): """ calculate technical indicators with ta-lib requires a df with a column named "Close" """ # to delete rows which have 0 volume / delete weekends print("dropping rows with zero volume from dataFrame {}".format(df.shape)) if drop_vol: df = df[(df[['Volume']] != 0).all(axis=1)] print("creating technical indicators") df['RSI'] = ta.RSI(df.Close.values, timeperiod=14) df['ROC'] = ta.ROC(df.Close.values, timeperiod=10) df['SMA'] = ta.SMA(df.Close.values, timeperiod=30) df['EMA'] = ta.EMA(df.Close.values, timeperiod=30) df['WMA'] = ta.WMA(df.Close.values, timeperiod=30) df['MACD'], df['macdSignal'], df['macdHist'] = ta.MACD(df.Close.values, fastperiod=12, slowperiod=26, signalperiod=9) print("done {}".format(df.shape)) print("dropping NaN values") if dropnan: df.dropna(inplace=True) print("returning dataFrame: {} from get_tis func".format(df.shape)) return df
def roc(candles: np.ndarray, period: int = 10, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ ROC - Rate of change : ((price/prevPrice)-1)*100 :param candles: np.ndarray :param period: int - default=10 :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.ROC(source, timeperiod=period) if sequential: return res else: return None if np.isnan(res[-1]) else res[-1]
def test_indicator_ROC(self): n = 3 price = 'Close' result = ROC(df, n) isinstance(result, pd.DataFrame) expected = talib.ROC(df[price].values, timeperiod=n) np.testing.assert_almost_equal(result.values, expected)
def calc_rps_returns_roc_func(data, *args, **kwargs): """ 计算收益率 实现算法2,经测试和算法1无速度差别,结果一致 w:周5;月20;半年:120; 一年250 """ w = 63 indices = None # 针对多标的,拆分 indices 数据再自动合并 code = data.index.get_level_values(level=1)[0] if (len(kwargs.keys()) > 0): w = kwargs['w'] if ('w' in kwargs.keys()) else w indices = kwargs['indices'].loc[(slice(None), code), :] if ( 'indices' in kwargs.keys()) else None if (len(args) > 0): w = args[0] if (len(args) > 0) else w indices = args[1].loc[(slice(None), code), :] if (len(args) >= 2) else None rps_reutrns = talib.ROC(data.close, w) / 100 if (indices is None): return pd.DataFrame(rps_reutrns, columns=[FLD.RPS_RETURNS], index=data.index).fillna(0) else: return pd.concat([ indices, pd.Series(rps_reutrns, name=FLD.RPS_RETURNS, index=data.index).fillna(0) ], axis=1)
def try_bottom_strategy(df): df['upperband'], df['middleband'], df['lowerband'] = talib.BBANDS( df['close'], timeperiod=30, nbdevup=3, nbdevdn=3, matype=0) df['std_var'] = (df['upperband'] - df['middleband']) / 3 df['close_off'] = (df['close'] - df['middleband']) / df['std_var'] df['rsi_14'] = talib.RSI(df['close'], timeperiod=14) df['vol_diff'] = talib.ROC(df['volume'], timeperiod=1) df['true_range'] = (df['high'] - df['low']) / df['low'] * 100 df['true_bar'] = (df['close'] - df['open']) / df['open'] * 100 df = df.round(2) df = df.dropna(how='any') df = df.reset_index(drop=True) score_ls = [] for i in range(len(df)): score = 0 if df['close_off'][i] < -1.6: score = score + 1 if df['rsi_14'][i] < 50: score = score + 1 if df['vol_diff'][i] > 80: score = score + 1 if df['true_range'][i] < 5: score = score + 1 if abs(df['true_bar'][i]) < 3: score = score + 1 score_ls.append(score) df['score'] = score_ls return df
def get_features(data): tech_data = pd.DataFrame(index=data.index); for t in periods_list: tech_data[f'SMA_{t}'] = talib.SMA(data.close,timeperiod=t) tech_data[f'MOM_{t}'] = talib.MOM(data.close, timeperiod=t) tech_data[f'RSI_{t}'] = talib.RSI(data.close, timeperiod=t) tech_data[f'MA_{t}'] = talib.MA(data.close, timeperiod=t) tech_data[f'DX_{t}'] = talib.DX(data.high, data.low, data.close, timeperiod=t) tech_data[f'volume_change_{t}'] = data.volume.pct_change(periods=t) tech_data[f'volatility_{t}'] = data.close.pct_change(periods=t).std() tech_data[f'ADX_{t}'] = talib.ADX(data.high, data.low, data.close, timeperiod=t) tech_data[f'ADXR_{t}'] = talib.ADXR(data.high, data.low, data.close, timeperiod=t) tech_data[f'AROONOSC_{t}'] = talib.AROONOSC(data.high, data.low, timeperiod=t) tech_data[f'ROC_{t}'] = talib.ROC(data.close, timeperiod=t) tech_data[f'BIAS_{t}'] = (data['close'] - data['close'].rolling(t, min_periods=1).mean())/ data['close'].rolling(t, min_periods=1).mean()*100 tech_data[f'BOLL_upper_{t}'], tech_data[f'BOLL_middle_{t}'], tech_data[f'BOLL_lower_{t}'] = talib.BBANDS( data.close, timeperiod=t, nbdevup=2, nbdevdn=2, matype=0) tech_data['SAR'] = talib.SAR(data.high, data.low) tech_data['AD'] = talib.AD(data.high, data.low, data.close, data.volume) tech_data['OBV'] = talib.OBV(data.close, data.volume) tech_data['target'] = data.close.pct_change().shift(-1).apply(lambda x: 1 if x > 0 else -1).fillna(0) tech_data['time'] = data.time tech_data = tech_data.set_index('time') reduce(lambda x, y: cross_over(x, y, tech_data), periods_list) features = list(set(tech_data.columns) - set(data.columns) - set(['target'])) return tech_data.dropna(), features
def rebalance(context, data): history = data.history(assets=context.asset, fields=['close'], bar_count=context.roc_window + 1, frequency='1d') date = history.index.values[-1] close = history['close'].values roc = ta.ROC(close, timeperiod=context.roc_window) current_price = data[context.asset].price record(price=current_price) buy_signal_triggered = False sell_signal_triggered = False if roc[-1] > 0: buy_signal_triggered = True elif roc[-1] < 0: 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 test_roc(): """test TA.ROC""" roc = TA.ROC(ohlc, 10) talib_roc = talib.ROC(ohlc["close"], 10) assert round(talib_roc[-1], 5) == round(roc.values[-1], 5)
def genTA(data, y, t): #t is timeperiod indicators = {} y_ind = copy.deepcopy(y) for ticker in data: ## Overlap indicators[ticker] = ta.SMA(data[ticker].iloc[:,3], timeperiod=t).to_frame() indicators[ticker]['EMA'] = ta.EMA(data[ticker].iloc[:,3], timeperiod=t) indicators[ticker]['BBAND_Upper'], indicators[ticker]['BBAND_Middle' ], indicators[ticker]['BBAND_Lower' ] = ta.BBANDS(data[ticker].iloc[:,3], timeperiod=t, nbdevup=2, nbdevdn=2, matype=0) indicators[ticker]['HT_TRENDLINE'] = ta.HT_TRENDLINE(data[ticker].iloc[:,3]) indicators[ticker]['SAR'] = ta.SAR(data[ticker].iloc[:,1], data[ticker].iloc[:,2], acceleration=0, maximum=0) #rename SMA column indicators[ticker].rename(columns={indicators[ticker].columns[0]: "SMA"}, inplace=True) ## Momentum indicators[ticker]['RSI'] = ta.RSI(data[ticker].iloc[:,3], timeperiod=(t-1)) indicators[ticker]['MOM'] = ta.MOM(data[ticker].iloc[:,3], timeperiod=(t-1)) indicators[ticker]['ROC'] = ta.ROC(data[ticker].iloc[:,3], timeperiod=(t-1)) indicators[ticker]['ROCP']= ta.ROCP(data[ticker].iloc[:,3],timeperiod=(t-1)) indicators[ticker]['STOCH_SLOWK'], indicators[ticker]['STOCH_SLOWD'] = ta.STOCH(data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3], fastk_period=t, slowk_period=int(.6*t), slowk_matype=0, slowd_period=int(.6*t), slowd_matype=0) indicators[ticker]['MACD'], indicators[ticker]['MACDSIGNAL'], indicators[ticker]['MACDHIST'] = ta.MACD(data[ticker].iloc[:,3], fastperiod=t,slowperiod=2*t,signalperiod=int(.7*t)) ## Volume indicators[ticker]['OBV'] = ta.OBV(data[ticker].iloc[:,3], data[ticker].iloc[:,4]) indicators[ticker]['AD'] = ta.AD(data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3], data[ticker].iloc[:,4]) indicators[ticker]['ADOSC'] = ta.ADOSC(data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3], data[ticker].iloc[:,4], fastperiod=int(.3*t), slowperiod=t) ## Cycle indicators[ticker]['HT_DCPERIOD'] = ta.HT_DCPERIOD(data[ticker].iloc[:,3]) indicators[ticker]['HT_TRENDMODE']= ta.HT_TRENDMODE(data[ticker].iloc[:,3]) ## Price indicators[ticker]['AVGPRICE'] = ta.AVGPRICE(data[ticker].iloc[:,0], data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3]) indicators[ticker]['TYPPRICE'] = ta.TYPPRICE(data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3]) ## Volatility indicators[ticker]['ATR'] = ta.ATR(data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3], timeperiod=(t-1)) ## Statistics indicators[ticker]['BETA'] = ta.BETA(data[ticker].iloc[:,1], data[ticker].iloc[:,2], timeperiod=(t-1)) indicators[ticker]['LINEARREG'] = ta.LINEARREG(data[ticker].iloc[:,3], timeperiod=t) indicators[ticker]['VAR'] = ta.VAR(data[ticker].iloc[:,3], timeperiod=t, nbdev=1) ## Math Transform indicators[ticker]['EXP'] = ta.EXP(data[ticker].iloc[:,3]) indicators[ticker]['LN'] = ta.LN(data[ticker].iloc[:,3]) ## Patterns (returns integers - but norming might not really do anything but wondering if they should be normed) indicators[ticker]['CDLENGULFING'] = ta.CDLENGULFING(data[ticker].iloc[:,0], data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3]) indicators[ticker]['CDLDOJI'] = ta.CDLDOJI(data[ticker].iloc[:,0], data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3]) indicators[ticker]['CDLHAMMER'] = ta.CDLHAMMER(data[ticker].iloc[:,0], data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3]) indicators[ticker]['CDLHANGINGMAN']= ta.CDLHANGINGMAN(data[ticker].iloc[:,0], data[ticker].iloc[:,1], data[ticker].iloc[:,2], data[ticker].iloc[:,3]) #drop 'nan' values indicators[ticker].drop(indicators[ticker].index[np.arange(0,63)], inplace=True) y_ind[ticker].drop(y_ind[ticker].index[np.arange(0,63)], inplace=True) #Normalize Features indicators_norm = normData(indicators) return indicators_norm, indicators, y_ind
def calculate_roc(self, period=1): """ Rate of change : ((price/prevPrice)-1)*100 :return: """ real = talib.ROC(self.close, timeperiod=period) return real
def technical_indicators(ohlcv_path="data/OHLC_NDX.csv"): """ Calculate Technical Indicators from the ti_dict dictionary and export to .csv :param ohlcv_path: OHLCV dataset path """ ohlcv = pd.read_csv(ohlcv_path) ti_dict = { 'SMA5': talib.SMA(ohlcv.Close, 5), 'SMA10': talib.SMA(ohlcv.Close, 10), 'SMA50': talib.SMA(ohlcv.Close, 50), 'EMA20': talib.EMA(ohlcv.Close, 20), 'stoch5': talib.STOCH(ohlcv.High, ohlcv.Low, ohlcv.Close, 5, 3, 0, 3, 0)[0], 'ADOSC': talib.ADOSC(ohlcv.High, ohlcv.Low, ohlcv.Close, ohlcv.Volume, fastperiod=3, slowperiod=10), 'MACDhist': talib.MACD(ohlcv.Close, fastperiod=12, slowperiod=26, signalperiod=9)[2], 'WILLR': talib.WILLR(ohlcv.High, ohlcv.Low, ohlcv.Close, timeperiod=14), 'RSI': talib.RSI(ohlcv.Close, timeperiod=14), 'MOM': talib.MOM(ohlcv.Close, timeperiod=10), 'ROC': talib.ROC(ohlcv.Close, timeperiod=10), 'OBV': talib.OBV(ohlcv.Close, ohlcv.Volume), 'CCI': talib.CCI(ohlcv.High, ohlcv.Low, ohlcv.Close, timeperiod=14) } # Create a dataframe from TI dictionary df_ti = pd.DataFrame(ti_dict, index=ohlcv.index) del ti_dict del ohlcv # Save Technical Indicators dataframe if not os.path.isdir('data_preprocessed/'): os.mkdir('data_preprocessed/') df_ti.to_csv('data_preprocessed/technical_indicators.csv', index=False) del df_ti return 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 getData(code,start,end): data = pandasData.DataReader(code,'yahoo',start,end) # print len(data) f_ema = ta.EMA(data['Close'].values, timeperiod=30).tolist() # print f_ema f_ma = ta.MA(data['Close'].values, timeperiod=30, matype=0).tolist() f_wma = ta.WMA(data['Close'].values, timeperiod=30).tolist() f_momentum = ta.MOM(data['Close'].values, timeperiod=10).tolist() f_roc = ta.ROC(data['Close'].values, timeperiod=10).tolist() # f_cycle = ta.HT_DCPERIOD(data['Close'].values).tolist() f_price = ta.WCLPRICE(data['High'].values, data['Low'].values, data['Close'].values).tolist() f_natr = ta.NATR(data['High'].values, data['Low'].values, data['Close'].values, timeperiod=14).tolist() f_stddev = ta.STDDEV(data['Close'].values, timeperiod=5, nbdev=1).tolist() X = pd.DataFrame( pd.np.array([f_ema, f_ma, f_wma, f_momentum, f_roc, f_price, f_natr, f_stddev]).T[32:] ,columns=['f_ema','f_ma','f_wma','f_momentum','f_roc','f_price','f_natr','f_stddev']) # print X['f_ema'].size # print X data = data['Close'].tolist() finaldata = [[] for i in range(2)] for i in range(0, len(data) - 1): temp = (data[i + 1] - data[i]) / data[i] finaldata[0].append(temp) if (temp > 0): finaldata[1].append(1) else: finaldata[1].append(0) # print len(data) data = data[31:len(data) - 1] # print data Y = pd.DataFrame(pd.np.array(finaldata).T, columns=['change', 'label']) X = X.join(Y) return X
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 rocStretagy(companyname, begin): end = datetime.date.today() #begin=end-pd.DateOffset(365*backtestyear) #timestamp format and get apple stock. st = dt.strptime(begin, '%Y-%m-%d') #st=begin.strftime('%Y-%m-%d') ed = end.strftime('%Y-%m-%d') symbol = companyname + ".NS" datanifty = nift50Indices.objects.filter(Ticker=symbol, Date__gte=st).values_list( 'Close', 'Open', 'High', 'Low') #data = pdr.get_data_yahoo(company,st,ed) dataframe = pd.DataFrame(list(datanifty), columns=['Close', 'Open', 'High', 'Low']) print(dataframe) data = dataframe.astype(float) #data = pdr.get_data_yahoo('RELIANCE.NS',st,ed) #Rate of change ROC data['ROC'] = talib.ROC(np.asarray(data['Close']), 20) graph = data.plot(y=['ROC'], title=companyname + ' ROC ') graph = plt.gcf() fig_html = mpld3.fig_to_html(graph) return fig_html
def backtest_market(data, buy_barrier, short_barrier): COL_TIME = 0 COL_CLOSE = 4 COL_VOLUME = 5 closes = [x[COL_CLOSE] for x in data] dates = [x[COL_TIME] for x in data] volumes = [x[COL_VOLUME] for x in data] candle_data = {'close': closes, 'volume': volumes} bars = pd.DataFrame(candle_data, index=dates, columns=['close', 'volume']) #print (bars.describe()) bars['Pct Change'] = bars['close'].astype('float').pct_change() bars['RSI'] = talib.RSI(bars['close']) bars['volumeROC'] = talib.ROC(bars['volume']) symbol = market rfs = RSIStrategy('close', bars, buy_barrier, short_barrier) signals = rfs.generate_signals() #print ("signal summary \n",signals.describe()) backtest = MarketOnOpenPortfolio(symbol, bars, signals) pf = backtest.backtest_portfolio() lastindex = pf['Strategy'][-1] total_return = lastindex / 100 - 1 print("total return ", total_return) plot_portfolio(pf)