def correl(client, symbol, range="6m", highcol="high", lowcol="low", period=14): """This will return a dataframe of Pearson's Correlation Coefficient(r) for the given symbol across the given range Args: client (pyEX.Client): Client symbol (string): Ticker range (string): range to use, for pyEX.chart highcol (string): column to use to calculate lowcol (string): column to use to calculate period (int): period to calculate adx across Returns: DataFrame: result """ df = client.chartDF(symbol, range) correl = t.CORREL(df[highcol].values.astype(float), df[lowcol].values.astype(float), period) return pd.DataFrame({ highcol: df[highcol].values, lowcol: df[lowcol].values, "correl": correl })
def CORREL(ds1, ds2, count, timeperiod=-2**31): """Pearson's Correlation Coefficient (r)""" data1 = value_ds_to_numpy(ds1, count) if data1 is None: return None data2 = value_ds_to_numpy(ds2, count) if data2 is None: return None return talib.CORREL(data1, data2, timeperiod)
def barAlpha61(bars, fast=False): """根据基因工程生成 (PH5*PH5*FRV80*FRV80)""" n = 80 n0 = 5 high = bars['high'].rolling(n0).max() close = bars['close'] volume = bars['volume'].diff().values.astype(np.float64) d = (high - close) / close x = talib.CORREL(close.values, volume, n) return d * d * x * x
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 CORREL(high, low, timeperiod=30): ''' Pearson's Correlation Coefficient (r) 皮尔逊相关系数 分组: Statistic Functions 统计函数 简介: 用于度量两个变量X和Y之间的相关(线性相关),其值介于-1与1之间 皮尔逊相关系数是一种度量两个变量间相关程度的方法。它是一个介于 1 和 -1 之间的值, 其中,1 表示变量完全正相关, 0 表示无关,-1 表示完全负相关。 real = CORREL(high, low, timeperiod=30) ''' return talib.CORREL(high, low, timeperiod)
def correl(candles: np.ndarray, period: int = 5, sequential: bool = False) -> Union[float, np.ndarray]: """ CORREL - Pearson's Correlation Coefficient (r) :param candles: np.ndarray :param period: int - default: 5 :param sequential: bool - default=False :return: float | np.ndarray """ candles = slice_candles(candles, sequential) res = talib.CORREL(candles[:, 3], candles[:, 4], timeperiod=period) return res if sequential else res[-1]
def getStatFunctions(df): high = df['High'] low = df['Low'] close = df['Close'] open = df['Open'] volume = df['Volume'] df['BETA'] = ta.BETA(high, low, timeperiod=5) df['CORREL'] = ta.CORREL(high, low, timeperiod=30) df['LINREG'] = ta.LINEARREG(close, timeperiod=14) df['LINREGANGLE'] = ta.LINEARREG_ANGLE(close, timeperiod=14) df['LINREGINTERCEPT'] = ta.LINEARREG_INTERCEPT(close, timeperiod=14) df['LINREGSLOPE'] = ta.LINEARREG_SLOPE(close, timeperiod=14) df['STDDEV'] = ta.STDDEV(close, timeperiod=5, nbdev=1) df['TSF'] = ta.TSF(close, timeperiod=14) df['VAR'] = ta.VAR(close, timeperiod=5, nbdev=1)
def Correlation(price1, price2, time=None, period=30): if len(price1) != len(price2): print('price1 and price2 are not in the same length') return 0 corr = talib.CORREL(numpy.array(price1), numpy.array(price2), timeperiod=period) data = pandas.DataFrame({'time': time, 'corr': corr}).dropna() data.insert(0, 'time', data.pop('time')) # if time is not None and len(time)==len(price1): # data=pandas.DataFrame(corr,index=time).dropna() # # return data
def correl(candles: np.ndarray, period=5, sequential=False) -> Union[float, np.ndarray]: """ CORREL - Pearson's Correlation Coefficient (r) :param candles: np.ndarray :param period: int - default: 5 :param sequential: bool - default=False :return: float | np.ndarray """ if not sequential and len(candles) > 240: candles = candles[-240:] res = talib.CORREL(candles[:, 3], candles[:, 4], timeperiod=period) if sequential: return res else: return None if np.isnan(res[-1]) else res[-1]
def correl(candles: np.ndarray, period: int = 5, sequential: bool = False) -> Union[float, np.ndarray]: """ CORREL - Pearson's Correlation Coefficient (r) :param candles: np.ndarray :param period: int - default: 5 :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:] res = talib.CORREL(candles[:, 3], candles[:, 4], timeperiod=period) if sequential: return res else: return None if np.isnan(res[-1]) else res[-1]
def Stat_Function(dataframe): #Statistic Functions #BETA - Beta df[f'{ratio}_BETA'] = talib.BETA(High, Low, timeperiod=5) #CORREL - Pearson's Correlation Coefficient (r) df[f'{ratio}_CORREL'] = talib.CORREL(High, Low, timeperiod=30) #LINEARREG - Linear Regression df[f'{ratio}_LINEARREG'] = talib.LINEARREG(Close, timeperiod=14) #LINEARREG_ANGLE - Linear Regression Angle df[f'{ratio}_LINEARREG_ANGLE'] = talib.LINEARREG_ANGLE(Close, timeperiod=14) #LINEARREG_INTERCEPT - Linear Regression Intercept df[f'{ratio}_LINEARREG_INTERCEPT'] = talib.LINEARREG_INTERCEPT(Close, timeperiod=14) #LINEARREG_SLOPE - Linear Regression Slope df[f'{ratio}_LINEARREG_SLOPE'] = talib.LINEARREG_SLOPE(Close, timeperiod=14) #STDDEV - Standard Deviation df[f'{ratio}_STDDEV'] = talib.STDDEV(Close, timeperiod=5, nbdev=1) #TSF - Time Series Forecast df[f'{ratio}_TSF'] = talib.TSF(Close, timeperiod=14) #VAR - Variance df[f'{ratio}_VAR'] = talib.VAR(Close, timeperiod=5, nbdev=1) return
def main(): ohlcv = api_ohlcv('20191017') open, high, low, close, volume, timestamp = [], [], [], [], [], [] for i in ohlcv: open.append(int(i[0])) high.append(int(i[1])) low.append(int(i[2])) close.append(int(i[3])) volume.append(float(i[4])) time_str = str(i[5]) timestamp.append( datetime.fromtimestamp(int( time_str[:10])).strftime('%Y/%m/%d %H:%M:%M')) date_time_index = pd.to_datetime( timestamp) # convert to DateTimeIndex type df = pd.DataFrame( { 'open': open, 'high': high, 'low': low, 'close': close, 'volume': volume }, index=date_time_index) # df.index += pd.offsets.Hour(9) # adjustment for JST if required print(df.shape) print(df.columns) # pct_change f = lambda x: 1 if x > 0.0001 else -1 if x < -0.0001 else 0 if -0.0001 <= x <= 0.0001 else np.nan y = df.rename(columns={ 'close': 'y' }).loc[:, 'y'].pct_change(1).shift(-1).fillna(0) X = df.copy() y_ = pd.DataFrame(y.map(f), columns=['y']) y = df.rename(columns={'close': 'y'}).loc[:, 'y'].pct_change(1).fillna(0) df_ = pd.concat([X, y_], axis=1) # check the shape print( '----------------------------------------------------------------------------------------' ) print('X shape: (%i,%i)' % X.shape) print('y shape: (%i,%i)' % y_.shape) print( '----------------------------------------------------------------------------------------' ) print(y_.groupby('y').size()) print('y=1 up, y=0 stay, y=-1 down') print( '----------------------------------------------------------------------------------------' ) # feature calculation open = pd.Series(df['open']) high = pd.Series(df['high']) low = pd.Series(df['low']) close = pd.Series(df['close']) volume = pd.Series(df['volume']) # pct_change for new column X['diff'] = y # Exponential Moving Average ema = talib.EMA(close, timeperiod=3) ema = ema.fillna(ema.mean()) # Momentum momentum = talib.MOM(close, timeperiod=5) momentum = momentum.fillna(momentum.mean()) # RSI rsi = talib.RSI(close, timeperiod=14) rsi = rsi.fillna(rsi.mean()) # ADX adx = talib.ADX(high, low, close, timeperiod=14) adx = adx.fillna(adx.mean()) # ADX change adx_change = adx.pct_change(1).shift(-1) adx_change = adx_change.fillna(adx_change.mean()) # AD ad = talib.AD(high, low, close, volume) ad = ad.fillna(ad.mean()) X_ = pd.concat([X, ema, momentum, rsi, adx_change, ad], axis=1).drop(['open', 'high', 'low', 'close'], axis=1) X_.columns = ['volume', 'diff', 'ema', 'momentum', 'rsi', 'adx', 'ad'] X_.join(y_).head(10) # default parameter models X_train, X_test, y_train, y_test = train_test_split(X_, y_, test_size=0.33, random_state=42) print('X_train shape: {}'.format(X_train.shape)) print('X_test shape: {}'.format(X_test.shape)) print('y_train shape: {}'.format(y_train.shape)) print('y_test shape: {}'.format(y_test.shape)) pipe_knn = Pipeline([('scl', StandardScaler()), ('est', KNeighborsClassifier(n_neighbors=3))]) pipe_logistic = Pipeline([('scl', StandardScaler()), ('est', LogisticRegression(solver='lbfgs', multi_class='multinomial', random_state=39))]) pipe_rf = Pipeline([('scl', StandardScaler()), ('est', RandomForestClassifier(random_state=39))]) pipe_gb = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) pipe_names = ['KNN', 'Logistic', 'RandomForest', 'GradientBoosting'] pipe_lines = [pipe_knn, pipe_logistic, pipe_rf, pipe_gb] for (i, pipe) in enumerate(pipe_lines): pipe.fit(X_train, y_train.values.ravel()) print(pipe) print('%s: %.3f' % (pipe_names[i] + ' Train Accuracy', accuracy_score(y_train.values.ravel(), pipe.predict(X_train)))) print('%s: %.3f' % (pipe_names[i] + ' Test Accuracy', accuracy_score(y_test.values.ravel(), pipe.predict(X_test)))) print('%s: %.3f' % (pipe_names[i] + ' Train F1 Score', f1_score(y_train.values.ravel(), pipe.predict(X_train), average='micro'))) print('%s: %.3f' % (pipe_names[i] + ' Test F1 Score', f1_score(y_test.values.ravel(), pipe.predict(X_test), average='micro'))) for (i, pipe) in enumerate(pipe_lines): predict = pipe.predict(X_test) cm = confusion_matrix(y_test.values.ravel(), predict, labels=[-1, 0, 1]) print('{} Confusion Matrix'.format(pipe_names[i])) print(cm) ## Overlap Studies Functions # DEMA - Double Exponential Moving Average dema = talib.DEMA(close, timeperiod=3) dema = dema.fillna(dema.mean()) print('DEMA - Double Exponential Moving Average shape: {}'.format( dema.shape)) # EMA - Exponential Moving Average ema = talib.EMA(close, timeperiod=3) ema = ema.fillna(ema.mean()) print('EMA - Exponential Moving Average shape: {}'.format(ema.shape)) # HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline hilbert = talib.HT_TRENDLINE(close) hilbert = hilbert.fillna(hilbert.mean()) print( 'HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline shape: {}'. format(hilbert.shape)) # KAMA - Kaufman Adaptive Moving Average kama = talib.KAMA(close, timeperiod=3) kama = kama.fillna(kama.mean()) print('KAMA - Kaufman Adaptive Moving Average shape: {}'.format( kama.shape)) # MA - Moving average ma = talib.MA(close, timeperiod=3, matype=0) ma = ma.fillna(ma.mean()) print('MA - Moving average shape: {}'.format(kama.shape)) # MIDPOINT - MidPoint over period midpoint = talib.MIDPOINT(close, timeperiod=7) midpoint = midpoint.fillna(midpoint.mean()) print('MIDPOINT - MidPoint over period shape: {}'.format(midpoint.shape)) # MIDPRICE - Midpoint Price over period midprice = talib.MIDPRICE(high, low, timeperiod=7) midprice = midprice.fillna(midprice.mean()) print('MIDPRICE - Midpoint Price over period shape: {}'.format( midprice.shape)) # SAR - Parabolic SAR sar = talib.SAR(high, low, acceleration=0, maximum=0) sar = sar.fillna(sar.mean()) print('SAR - Parabolic SAR shape: {}'.format(sar.shape)) # SAREXT - Parabolic SAR - Extended sarext = talib.SAREXT(high, low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0) sarext = sarext.fillna(sarext.mean()) print('SAREXT - Parabolic SAR - Extended shape: {}'.format(sarext.shape)) # SMA - Simple Moving Average sma = talib.SMA(close, timeperiod=3) sma = sma.fillna(sma.mean()) print('SMA - Simple Moving Average shape: {}'.format(sma.shape)) # T3 - Triple Exponential Moving Average (T3) t3 = talib.T3(close, timeperiod=5, vfactor=0) t3 = t3.fillna(t3.mean()) print('T3 - Triple Exponential Moving Average shape: {}'.format(t3.shape)) # TEMA - Triple Exponential Moving Average tema = talib.TEMA(close, timeperiod=3) tema = tema.fillna(tema.mean()) print('TEMA - Triple Exponential Moving Average shape: {}'.format( tema.shape)) # TRIMA - Triangular Moving Average trima = talib.TRIMA(close, timeperiod=3) trima = trima.fillna(trima.mean()) print('TRIMA - Triangular Moving Average shape: {}'.format(trima.shape)) # WMA - Weighted Moving Average wma = talib.WMA(close, timeperiod=3) wma = wma.fillna(wma.mean()) print('WMA - Weighted Moving Average shape: {}'.format(wma.shape)) ## Momentum Indicator Functions # ADX - Average Directional Movement Index adx = talib.ADX(high, low, close, timeperiod=14) adx = adx.fillna(adx.mean()) print('ADX - Average Directional Movement Index shape: {}'.format( adx.shape)) # ADXR - Average Directional Movement Index Rating adxr = talib.ADXR(high, low, close, timeperiod=7) adxr = adxr.fillna(adxr.mean()) print('ADXR - Average Directional Movement Index Rating shape: {}'.format( adxr.shape)) # APO - Absolute Price Oscillator apo = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) apo = apo.fillna(apo.mean()) print('APO - Absolute Price Oscillator shape: {}'.format(apo.shape)) # AROONOSC - Aroon Oscillator aroon = talib.AROONOSC(high, low, timeperiod=14) aroon = aroon.fillna(aroon.mean()) print('AROONOSC - Aroon Oscillator shape: {}'.format(apo.shape)) # BOP - Balance Of Power bop = talib.BOP(open, high, low, close) bop = bop.fillna(bop.mean()) print('BOP - Balance Of Power shape: {}'.format(apo.shape)) # CCI - Commodity Channel Index cci = talib.CCI(high, low, close, timeperiod=7) cci = cci.fillna(cci.mean()) print('CCI - Commodity Channel Index shape: {}'.format(cci.shape)) # CMO - Chande Momentum Oscillator cmo = talib.CMO(close, timeperiod=7) cmo = cmo.fillna(cmo.mean()) print('CMO - Chande Momentum Oscillator shape: {}'.format(cmo.shape)) # DX - Directional Movement Index dx = talib.DX(high, low, close, timeperiod=7) dx = dx.fillna(dx.mean()) print('DX - Directional Movement Index shape: {}'.format(dx.shape)) # MFI - Money Flow Index mfi = talib.MFI(high, low, close, volume, timeperiod=7) mfi = mfi.fillna(mfi.mean()) print('MFI - Money Flow Index shape: {}'.format(mfi.shape)) # MINUS_DI - Minus Directional Indicator minusdi = talib.MINUS_DI(high, low, close, timeperiod=14) minusdi = minusdi.fillna(minusdi.mean()) print('MINUS_DI - Minus Directional Indicator shape: {}'.format( minusdi.shape)) # MINUS_DM - Minus Directional Movement minusdm = talib.MINUS_DM(high, low, timeperiod=14) minusdm = minusdm.fillna(minusdm.mean()) print('MINUS_DM - Minus Directional Movement shape: {}'.format( minusdm.shape)) # MOM - Momentum mom = talib.MOM(close, timeperiod=5) mom = mom.fillna(mom.mean()) print('MOM - Momentum shape: {}'.format(mom.shape)) # PLUS_DI - Plus Directional Indicator plusdi = talib.PLUS_DI(high, low, close, timeperiod=14) plusdi = plusdi.fillna(plusdi.mean()) print('PLUS_DI - Plus Directional Indicator shape: {}'.format( plusdi.shape)) # PLUS_DM - Plus Directional Movement plusdm = talib.PLUS_DM(high, low, timeperiod=14) plusdm = plusdm.fillna(plusdm.mean()) print('PLUS_DM - Plus Directional Movement shape: {}'.format(plusdi.shape)) # PPO - Percentage Price Oscillator ppo = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) ppo = ppo.fillna(ppo.mean()) print('PPO - Percentage Price Oscillator shape: {}'.format(ppo.shape)) # ROC - Rate of change:((price/prevPrice)-1)*100 roc = talib.ROC(close, timeperiod=10) roc = roc.fillna(roc.mean()) print('ROC - Rate of change : ((price/prevPrice)-1)*100 shape: {}'.format( roc.shape)) # RSI - Relative Strength Index rsi = talib.RSI(close, timeperiod=14) rsi = rsi.fillna(rsi.mean()) print('RSI - Relative Strength Index shape: {}'.format(rsi.shape)) # TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA trix = talib.TRIX(close, timeperiod=30) trix = trix.fillna(trix.mean()) print('TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA shape: {}'. format(trix.shape)) # ULTOSC - Ultimate Oscillator ultosc = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) ultosc = ultosc.fillna(ultosc.mean()) print('ULTOSC - Ultimate Oscillator shape: {}'.format(ultosc.shape)) # WILLR - Williams'%R willr = talib.WILLR(high, low, close, timeperiod=7) willr = willr.fillna(willr.mean()) print("WILLR - Williams'%R shape: {}".format(willr.shape)) ## Volume Indicator Functions # AD - Chaikin A/D Line ad = talib.AD(high, low, close, volume) ad = ad.fillna(ad.mean()) print('AD - Chaikin A/D Line shape: {}'.format(ad.shape)) # ADOSC - Chaikin A/D Oscillator adosc = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10) adosc = adosc.fillna(adosc.mean()) print('ADOSC - Chaikin A/D Oscillator shape: {}'.format(adosc.shape)) # OBV - On Balance Volume obv = talib.OBV(close, volume) obv = obv.fillna(obv.mean()) print('OBV - On Balance Volume shape: {}'.format(obv.shape)) ## Volatility Indicator Functions # ATR - Average True Range atr = talib.ATR(high, low, close, timeperiod=7) atr = atr.fillna(atr.mean()) print('ATR - Average True Range shape: {}'.format(atr.shape)) # NATR - Normalized Average True Range natr = talib.NATR(high, low, close, timeperiod=7) natr = natr.fillna(natr.mean()) print('NATR - Normalized Average True Range shape: {}'.format(natr.shape)) # TRANGE - True Range trange = talib.TRANGE(high, low, close) trange = trange.fillna(trange.mean()) print('TRANGE - True Range shape: {}'.format(natr.shape)) ## Price Transform Functions # AVGPRICE - Average Price avg = talib.AVGPRICE(open, high, low, close) avg = avg.fillna(avg.mean()) print('AVGPRICE - Average Price shape: {}'.format(natr.shape)) # MEDPRICE - Median Price medprice = talib.MEDPRICE(high, low) medprice = medprice.fillna(medprice.mean()) print('MEDPRICE - Median Price shape: {}'.format(medprice.shape)) # TYPPRICE - Typical Price typ = talib.TYPPRICE(high, low, close) typ = typ.fillna(typ.mean()) print('TYPPRICE - Typical Price shape: {}'.format(typ.shape)) # WCLPRICE - Weighted Close Price wcl = talib.WCLPRICE(high, low, close) wcl = wcl.fillna(wcl.mean()) print('WCLPRICE - Weighted Close Price shape: {}'.format(wcl.shape)) ## Cycle Indicator Functions # HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period dcperiod = talib.HT_DCPERIOD(close) dcperiod = dcperiod.fillna(dcperiod.mean()) print('HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period shape: {}'. format(dcperiod.shape)) # HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase dcphase = talib.HT_DCPHASE(close) dcphase = dcphase.fillna(dcphase.mean()) print('HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase shape: {}'. format(dcperiod.shape)) ## Statistic Functions # BETA - Beta beta = talib.BETA(high, low, timeperiod=3) beta = beta.fillna(beta.mean()) print('BETA - Beta shape: {}'.format(beta.shape)) # CORREL - Pearson's Correlation Coefficient(r) correl = talib.CORREL(high, low, timeperiod=30) correl = correl.fillna(correl.mean()) print("CORREL - Pearson's Correlation Coefficient(r) shape: {}".format( beta.shape)) # LINEARREG - Linear Regression linreg = talib.LINEARREG(close, timeperiod=7) linreg = linreg.fillna(linreg.mean()) print("LINEARREG - Linear Regression shape: {}".format(linreg.shape)) # STDDEV - Standard Deviation stddev = talib.STDDEV(close, timeperiod=5, nbdev=1) stddev = stddev.fillna(stddev.mean()) print("STDDEV - Standard Deviation shape: {}".format(stddev.shape)) # TSF - Time Series Forecast tsf = talib.TSF(close, timeperiod=7) tsf = tsf.fillna(tsf.mean()) print("TSF - Time Series Forecast shape: {}".format(tsf.shape)) # VAR - Variance var = talib.VAR(close, timeperiod=5, nbdev=1) var = var.fillna(var.mean()) print("VAR - Variance shape: {}".format(var.shape)) ## Feature DataFrame X_full = pd.concat([ X, dema, ema, hilbert, kama, ma, midpoint, midprice, sar, sarext, sma, t3, tema, trima, wma, adx, adxr, apo, aroon, bop, cci, cmo, mfi, minusdi, minusdm, mom, plusdi, plusdm, ppo, roc, rsi, trix, ultosc, willr, ad, adosc, obv, atr, natr, trange, avg, medprice, typ, wcl, dcperiod, dcphase, beta, correl, linreg, stddev, tsf, var ], axis=1).drop(['open', 'high', 'low', 'close'], axis=1) X_full.columns = [ 'volume', 'diff', 'dema', 'ema', 'hilbert', 'kama', 'ma', 'midpoint', 'midprice', 'sar', 'sarext', 'sma', 't3', 'tema', 'trima', 'wma', 'adx', 'adxr', 'apo', 'aroon', 'bop', 'cci', 'cmo', 'mfi', 'minusdi', 'minusdm', 'mom', 'plusdi', 'plusdm', 'ppo', 'roc', 'rsi', 'trix', 'ultosc', 'willr', 'ad', 'adosc', 'obv', 'atr', 'natr', 'trange', 'avg', 'medprice', 'typ', 'wcl', 'dcperiod', 'dcphase', 'beta', 'correl', 'linreg', 'stddev', 'tsf', 'var' ] X_full.join(y_).head(10) # full feature models X_train_full, X_test_full, y_train_full, y_test_full = train_test_split( X_full, y_, test_size=0.33, random_state=42) print('X_train shape: {}'.format(X_train_full.shape)) print('X_test shape: {}'.format(X_test_full.shape)) print('y_train shape: {}'.format(y_train_full.shape)) print('y_test shape: {}'.format(y_test_full.shape)) pipe_knn_full = Pipeline([('scl', StandardScaler()), ('est', KNeighborsClassifier(n_neighbors=3))]) pipe_logistic_full = Pipeline([ ('scl', StandardScaler()), ('est', LogisticRegression(solver='lbfgs', multi_class='multinomial', random_state=39)) ]) pipe_rf_full = Pipeline([('scl', StandardScaler()), ('est', RandomForestClassifier(random_state=39))]) pipe_gb_full = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) pipe_names = ['KNN', 'Logistic', 'RandomForest', 'GradientBoosting'] pipe_lines_full = [ pipe_knn_full, pipe_logistic_full, pipe_rf_full, pipe_gb_full ] for (i, pipe) in enumerate(pipe_lines_full): pipe.fit(X_train_full, y_train_full.values.ravel()) print(pipe) print('%s: %.3f' % (pipe_names[i] + ' Train Accuracy', accuracy_score(y_train_full.values.ravel(), pipe.predict(X_train_full)))) print('%s: %.3f' % (pipe_names[i] + ' Test Accuracy', accuracy_score(y_test_full.values.ravel(), pipe.predict(X_test_full)))) print('%s: %.3f' % (pipe_names[i] + ' Train F1 Score', f1_score(y_train_full.values.ravel(), pipe.predict(X_train_full), average='micro'))) print('%s: %.3f' % (pipe_names[i] + ' Test F1 Score', f1_score(y_test_full.values.ravel(), pipe.predict(X_test_full), average='micro'))) # Univariate Statistics select = SelectPercentile(percentile=25) select.fit(X_train_full, y_train_full.values.ravel()) X_train_selected = select.transform(X_train_full) X_test_selected = select.transform(X_test_full) # GradientBoost Classifier print( '--------------------------Without Univariate Statistics-------------------------------------' ) pipe_gb = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) pipe_gb.fit(X_train_full, y_train_full.values.ravel()) print('Train Accuracy: {:.3f}'.format( accuracy_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full)))) print('Test Accuracy: {:.3f}'.format( accuracy_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full)))) print('Train F1 Score: {:.3f}'.format( f1_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full), average='micro'))) print('Test F1 Score: {:.3f}'.format( f1_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full), average='micro'))) # GradientBoost Cllassifier with Univariate Statistics print( '---------------------------With Univariate Statistics--------------------------------------' ) pipe_gb_percentile = Pipeline([ ('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39)) ]) pipe_gb_percentile.fit(X_train_selected, y_train_full.values.ravel()) print('Train Accuracy: {:.3f}'.format( accuracy_score(y_train_full.values.ravel(), pipe_gb_percentile.predict(X_train_selected)))) print('Test Accuracy: {:.3f}'.format( accuracy_score(y_test_full.values.ravel(), pipe_gb_percentile.predict(X_test_selected)))) print('Train F1 Score: {:.3f}'.format( f1_score(y_train_full.values.ravel(), pipe_gb_percentile.predict(X_train_selected), average='micro'))) print('Test F1 Score: {:.3f}'.format( f1_score(y_test_full.values.ravel(), pipe_gb_percentile.predict(X_test_selected), average='micro'))) # Model-based Selection select = SelectFromModel(RandomForestClassifier(n_estimators=100, random_state=42), threshold="1.25*mean") select.fit(X_train_full, y_train_full.values.ravel()) X_train_model = select.transform(X_train_full) X_test_model = select.transform(X_test_full) # GradientBoost Classifier print( '--------------------------Without Model-based Selection--------------------------------------' ) pipe_gb = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) pipe_gb.fit(X_train_full, y_train_full.values.ravel()) print('Train Accuracy: {:.3f}'.format( accuracy_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full)))) print('Test Accuracy: {:.3f}'.format( accuracy_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full)))) print('Train F1 Score: {:.3f}'.format( f1_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full), average='micro'))) print('Test F1 Score: {:.3f}'.format( f1_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full), average='micro'))) # GradientBoost Classifier with Model-based Selection print( '----------------------------With Model-based Selection--------------------------------------' ) pipe_gb_model = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) pipe_gb_model.fit(X_train_model, y_train_full.values.ravel()) print('Train Accuracy: {:.3f}'.format( accuracy_score(y_train_full.values.ravel(), pipe_gb_model.predict(X_train_model)))) print('Test Accuracy: {:.3f}'.format( accuracy_score(y_test_full.values.ravel(), pipe_gb_model.predict(X_test_model)))) print('Train F1 Score: {:.3f}'.format( f1_score(y_train_full.values.ravel(), pipe_gb_model.predict(X_train_model), average='micro'))) print('Test F1 Score: {:.3f}'.format( f1_score(y_test_full.values.ravel(), pipe_gb_model.predict(X_test_model), average='micro'))) # Recursive Feature Elimination select = RFE(RandomForestClassifier(n_estimators=100, random_state=42), n_features_to_select=15) select.fit(X_train_full, y_train_full.values.ravel()) X_train_rfe = select.transform(X_train_full) X_test_rfe = select.transform(X_test_full) # GradientBoost Classifier print( '--------------------------Without Recursive Feature Elimination-------------------------------------' ) pipe_gb = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) pipe_gb.fit(X_train_full, y_train_full.values.ravel()) print('Train Accuracy: {:.3f}'.format( accuracy_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full)))) print('Test Accuracy: {:.3f}'.format( accuracy_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full)))) print('Train F1 Score: {:.3f}'.format( f1_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full), average='micro'))) print('Test F1 Score: {:.3f}'.format( f1_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full), average='micro'))) # GradientBoost Classifier with Recursive Feature Elimination print( '----------------------------With Recursive Feature Elimination--------------------------------------' ) pipe_gb_rfe = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) pipe_gb_rfe.fit(X_train_rfe, y_train_full.values.ravel()) print('Train Accuracy: {:.3f}'.format( accuracy_score(y_train_full.values.ravel(), pipe_gb_rfe.predict(X_train_rfe)))) print('Test Accuracy: {:.3f}'.format( accuracy_score(y_test_full.values.ravel(), pipe_gb_rfe.predict(X_test_rfe)))) print('Train F1 Score: {:.3f}'.format( f1_score(y_train_full.values.ravel(), pipe_gb_rfe.predict(X_train_rfe), average='micro'))) print('Test F1 Score: {:.3f}'.format( f1_score(y_test_full.values.ravel(), pipe_gb_rfe.predict(X_test_rfe), average='micro'))) cv = cross_val_score(pipe_gb, X_, y_.values.ravel(), cv=StratifiedKFold(n_splits=10, shuffle=True, random_state=39)) print('Cross validation with StratifiedKFold scores: {}'.format(cv)) print('Cross Validation with StatifiedKFold mean: {}'.format(cv.mean())) # GridSearch n_features = len(df.columns) param_grid = { 'learning_rate': [0.01, 0.1, 1, 10], 'n_estimators': [1, 10, 100, 200, 300], 'max_depth': [1, 2, 3, 4, 5] } stratifiedcv = StratifiedKFold(n_splits=10, shuffle=True, random_state=39) X_train, X_test, y_train, y_test = train_test_split(X_, y_, test_size=0.33, random_state=42) grid_search = GridSearchCV(GradientBoostingClassifier(), param_grid, cv=stratifiedcv) grid_search.fit(X_train, y_train.values.ravel()) print('GridSearch Train Accuracy: {:.3f}'.format( accuracy_score(y_train.values.ravel(), grid_search.predict(X_train)))) print('GridSearch Test Accuracy: {:.3f}'.format( accuracy_score(y_test.values.ravel(), grid_search.predict(X_test)))) print('GridSearch Train F1 Score: {:.3f}'.format( f1_score(y_train.values.ravel(), grid_search.predict(X_train), average='micro'))) print('GridSearch Test F1 Score: {:.3f}'.format( f1_score(y_test.values.ravel(), grid_search.predict(X_test), average='micro'))) # GridSearch results print("Best params:\n{}".format(grid_search.best_params_)) print("Best cross-validation score: {:.2f}".format( grid_search.best_score_)) results = pd.DataFrame(grid_search.cv_results_) corr_params = results.drop(results.columns[[ 0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20 ]], axis=1) corr_params.head() # GridSearch in nested cv_gb = cross_val_score(grid_search, X_, y_.values.ravel(), cv=StratifiedKFold(n_splits=3, shuffle=True, random_state=39)) print('Grid Search with nested cross validation scores: {}'.format(cv_gb)) print('Grid Search with nested cross validation mean: {}'.format( cv_gb.mean()))
def get_datasets(asset, currency, granularity, datapoints): """Fetch the API and precess the desired pair Arguments: asset {str} -- First pair currency {str} -- Second pair granularity {str ['day', 'hour']} -- Granularity datapoints {int [100 - 2000]} -- [description] Returns: pandas.Dataframe -- The OHLCV and indicators dataframe """ df_train_path = 'datasets/bot_train_{}_{}_{}.csv'.format( asset + currency, datapoints, granularity) df_rollout_path = 'datasets/bot_rollout_{}_{}_{}.csv'.format( asset + currency, datapoints, granularity) emojis = [ ':moneybag:', ':yen:', ':dollar:', ':pound:', ':euro:', ':credit_card:', ':money_with_wings:', ':gem:' ] if not os.path.exists(df_rollout_path): headers = { 'User-Agent': 'Mozilla/5.0', 'authorization': 'Apikey 3d7d3e9e6006669ac00584978342451c95c3c78421268ff7aeef69995f9a09ce' } # OHLC # url = 'https://min-api.cryptocompare.com/data/histo{}?fsym={}&tsym={}&e=Binance&limit={}'.format(granularity, asset, currency, datapoints) url = 'https://min-api.cryptocompare.com/data/histo{}?fsym={}&tsym={}&limit={}'.format( granularity, asset, currency, datapoints) # print(emoji.emojize(':dizzy: :large_blue_diamond: :gem: :bar_chart: :crystal_ball: :chart_with_downwards_trend: :chart_with_upwards_trend: :large_orange_diamond: loading...', use_aliases=True)) print( colored( emoji.emojize('> ' + random.choice(emojis) + ' downloading ' + asset + '/' + currency, use_aliases=True), 'green')) # print(colored('> downloading ' + asset + '/' + currency, 'green')) response = requests.get(url, headers=headers) json_response = response.json() status = json_response['Response'] if status == "Error": print(colored('=== {} ==='.format(json_response['Message']), 'red')) raise AssertionError() result = json_response['Data'] df = pd.DataFrame(result) print(df.tail()) df['Date'] = pd.to_datetime(df['time'], utc=True, unit='s') df.drop('time', axis=1, inplace=True) # indicators # https://github.com/mrjbq7/ta-lib/blob/master/docs/func.md open_price, high, low, close = np.array(df['open']), np.array( df['high']), np.array(df['low']), np.array(df['close']) volume = np.array(df['volumefrom']) # cycle indicators df.loc[:, 'HT_DCPERIOD'] = talib.HT_DCPERIOD(close) df.loc[:, 'HT_DCPHASE'] = talib.HT_DCPHASE(close) df.loc[:, 'HT_PHASOR_inphase'], df.loc[:, 'HT_PHASOR_quadrature'] = talib.HT_PHASOR( close) df.loc[:, 'HT_SINE_sine'], df.loc[:, 'HT_SINE_leadsine'] = talib.HT_SINE( close) df.loc[:, 'HT_TRENDMODE'] = talib.HT_TRENDMODE(close) # momemtum indicators df.loc[:, 'ADX'] = talib.ADX(high, low, close, timeperiod=12) df.loc[:, 'ADXR'] = talib.ADXR(high, low, close, timeperiod=13) df.loc[:, 'APO'] = talib.APO(close, fastperiod=5, slowperiod=10, matype=0) df.loc[:, 'AROON_down'], df.loc[:, 'AROON_up'] = talib.AROON(high, low, timeperiod=15) df.loc[:, 'AROONOSC'] = talib.AROONOSC(high, low, timeperiod=13) df.loc[:, 'BOP'] = talib.BOP(open_price, high, low, close) df.loc[:, 'CCI'] = talib.CCI(high, low, close, timeperiod=13) df.loc[:, 'CMO'] = talib.CMO(close, timeperiod=14) df.loc[:, 'DX'] = talib.DX(high, low, close, timeperiod=10) df['MACD'], df['MACD_signal'], df['MACD_hist'] = talib.MACD( close, fastperiod=5, slowperiod=10, signalperiod=20) df.loc[:, 'MFI'] = talib.MFI(high, low, close, volume, timeperiod=12) df.loc[:, 'MINUS_DI'] = talib.MINUS_DI(high, low, close, timeperiod=10) df.loc[:, 'MINUS_DM'] = talib.MINUS_DM(high, low, timeperiod=14) df.loc[:, 'MOM'] = talib.MOM(close, timeperiod=20) df.loc[:, 'PPO'] = talib.PPO(close, fastperiod=17, slowperiod=35, matype=2) df.loc[:, 'ROC'] = talib.ROC(close, timeperiod=12) df.loc[:, 'RSI'] = talib.RSI(close, timeperiod=25) df.loc[:, 'STOCH_k'], df.loc[:, 'STOCH_d'] = talib.STOCH(high, low, close, fastk_period=35, slowk_period=12, slowk_matype=0, slowd_period=7, slowd_matype=0) df.loc[:, 'STOCHF_k'], df.loc[:, 'STOCHF_d'] = talib.STOCHF(high, low, close, fastk_period=28, fastd_period=14, fastd_matype=0) df.loc[:, 'STOCHRSI_K'], df.loc[:, 'STOCHRSI_D'] = talib.STOCHRSI( close, timeperiod=35, fastk_period=12, fastd_period=10, fastd_matype=1) df.loc[:, 'TRIX'] = talib.TRIX(close, timeperiod=30) df.loc[:, 'ULTOSC'] = talib.ULTOSC(high, low, close, timeperiod1=14, timeperiod2=28, timeperiod3=35) df.loc[:, 'WILLR'] = talib.WILLR(high, low, close, timeperiod=35) # overlap studies df.loc[:, 'BBANDS_upper'], df.loc[:, 'BBANDS_middle'], df.loc[:, 'BBANDS_lower'] = talib.BBANDS( close, timeperiod= 12, nbdevup=2, nbdevdn=2, matype=0) df.loc[:, 'DEMA'] = talib.DEMA(close, timeperiod=30) df.loc[:, 'EMA'] = talib.EMA(close, timeperiod=7) df.loc[:, 'HT_TRENDLINE'] = talib.HT_TRENDLINE(close) df.loc[:, 'KAMA'] = talib.KAMA(close, timeperiod=5) df.loc[:, 'MA'] = talib.MA(close, timeperiod=5, matype=0) df.loc[:, 'MIDPOINT'] = talib.MIDPOINT(close, timeperiod=20) df.loc[:, 'WMA'] = talib.WMA(close, timeperiod=15) df.loc[:, 'SMA'] = talib.SMA(close) # pattern recoginition df.loc[:, 'CDL2CROWS'] = talib.CDL2CROWS(open_price, high, low, close) df.loc[:, 'CDL3BLACKCROWS'] = talib.CDL3BLACKCROWS( open_price, high, low, close) df.loc[:, 'CDL3INSIDE'] = talib.CDL3INSIDE(open_price, high, low, close) df.loc[:, 'CDL3LINESTRIKE'] = talib.CDL3LINESTRIKE( open_price, high, low, close) # price transform df.loc[:, 'WCLPRICE'] = talib.WCLPRICE(high, low, close) # statistic funcitons df.loc[:, 'BETA'] = talib.BETA(high, low, timeperiod=20) df.loc[:, 'CORREL'] = talib.CORREL(high, low, timeperiod=20) df.loc[:, 'STDDEV'] = talib.STDDEV(close, timeperiod=20, nbdev=1) df.loc[:, 'TSF'] = talib.TSF(close, timeperiod=20) df.loc[:, 'VAR'] = talib.VAR(close, timeperiod=20, nbdev=1) # volatility indicators df.loc[:, 'ATR'] = talib.ATR(high, low, close, timeperiod=7) df.loc[:, 'NATR'] = talib.NATR(high, low, close, timeperiod=20) df.loc[:, 'TRANGE'] = talib.TRANGE(high, low, close) # volume indicators df.loc[:, 'AD'] = talib.AD(high, low, close, volume) df.loc[:, 'ADOSC'] = talib.ADOSC(high, low, close, volume, fastperiod=10, slowperiod=20) df.loc[:, 'OBV'] = talib.OBV(close, volume) # df.fillna(df.mean(), inplace=True) df.dropna(inplace=True) df.set_index('Date', inplace=True) print(colored('> caching' + asset + '/' + currency + ':)', 'cyan')) train_size = round( len(df) * DF_TRAIN_SIZE) # 75% to train -> test with different value df_train = df[:train_size] df_rollout = df[train_size:] df_train.to_csv(df_train_path) df_rollout.to_csv(df_rollout_path) df_train = pd.read_csv( df_train_path) # re-read to avoid indexing issue w/ Ray df_rollout = pd.read_csv(df_rollout_path) else: print( colored( emoji.emojize('> ' + random.choice(emojis) + ' feching ' + asset + '/' + currency + ' from cache', use_aliases=True), 'magenta')) # print(colored('> feching ' + asset + '/' + currency + ' from cache :)', 'magenta')) df_train = pd.read_csv(df_train_path) df_rollout = pd.read_csv(df_rollout_path) # df_train.set_index('Date', inplace=True) # df_rollout.set_index('Date', inplace=True) return df_train, df_rollout
import tushare as ts def get_data(code,start='2015-01-01'): df=ts.get_k_data(code,start) df.index=pd.to_datetime(df.date) df=df.sort_index() return df #获取上证指数收盘价、最高、最低价格 df=get_data('sh')[['open','close','high','low']] #收盘价对时间t的线性回归预测值 df['linearreg']=ta.LINEARREG(df.close, timeperiod=14) #时间序列预测值 df['tsf']=ta.TSF(df.close, timeperiod=14) #画图 df.loc['2018-08-01':,['close','linearreg','tsf']].plot(figsize=(12,6)) plt.show() df['beta']=ta.BETA(df.high,df.low,timeperiod=5) df['correl']=ta.CORREL(df.high, df.low, timeperiod=30) df['stdev']=ta.STDDEV(df.close, timeperiod=5, nbdev=1) #将上述函数计算得到的结果进行可视化 df[['close','beta','correl','stdev']].plot(figsize=(12,8), subplots = True,layout=(2, 2)) plt.subplots_adjust(wspace=0,hspace=0.2) plt.show()
def CORREL(self, window=30): real = talib.CORREL(self.high, self.low, timeperiod=window) return real
real = talib.NATR(df.high, df.low, df.close, timeperiod=14) real = talib.TRANGE(df.high, df.low, df.close) #周期 real = talib.HT_DCPERIOD(df.close) real = talib.HT_DCPHASE(df.close) inphase, quadrature = talib.HT_PHASOR(df.close) sine, leadsine = talib.HT_SINE(df.close) #integer = talib.HT_TRENDMODE(df.close) #integer = talib.CDL2CROWS(df.open, df.high, df.low, df.close) #统计学指标 real = talib.BETA(df.high, df.low, timeperiod=5) real = talib.CORREL(df.high, df.low, timeperiod=30) #************************ #timeperiod=3,5,14,30, real = talib.LINEARREG_SLOPE(df.close, timeperiod=3) real = talib.STDDEV(df.close, timeperiod=5, nbdev=1) real = talib.TSF(df.close, timeperiod=14) #real = talib.ACOS(df.close)
def add_ta_features(df, ta_settings): """Add technial analysis features from typical financial dataset that typically include columns such as "open", "high", "low", "price" and "volume". http://mrjbq7.github.io/ta-lib/ Args: df(pandas.DataFrame): original DataFrame. ta_settings(dict): configuration. Returns: pandas.DataFrame: DataFrame with new features included. """ open = df['open'] high = df['high'] low = df['low'] close = df['price'] volume = df['volume'] if ta_settings['overlap']: df['ta_overlap_bbands_upper'], df['ta_overlap_bbands_middle'], df[ 'ta_overlap_bbands_lower'] = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) df['ta_overlap_dema'] = ta.DEMA( close, timeperiod=15) # NOTE: Changed to avoid a lot of Nan values df['ta_overlap_ema'] = ta.EMA(close, timeperiod=30) df['ta_overlap_kama'] = ta.KAMA(close, timeperiod=30) df['ta_overlap_ma'] = ta.MA(close, timeperiod=30, matype=0) df['ta_overlap_mama_mama'], df['ta_overlap_mama_fama'] = ta.MAMA(close) period = np.random.randint(10, 20, size=len(close)).astype(float) df['ta_overlap_mavp'] = ta.MAVP(close, period, minperiod=2, maxperiod=30, matype=0) df['ta_overlap_midpoint'] = ta.MIDPOINT(close, timeperiod=14) df['ta_overlap_midprice'] = ta.MIDPRICE(high, low, timeperiod=14) df['ta_overlap_sar'] = ta.SAR(high, low, acceleration=0, maximum=0) df['ta_overlap_sarext'] = ta.SAREXT(high, low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0) df['ta_overlap_sma'] = ta.SMA(close, timeperiod=30) df['ta_overlap_t3'] = ta.T3(close, timeperiod=5, vfactor=0) df['ta_overlap_tema'] = ta.TEMA( close, timeperiod=12) # NOTE: Changed to avoid a lot of Nan values df['ta_overlap_trima'] = ta.TRIMA(close, timeperiod=30) df['ta_overlap_wma'] = ta.WMA(close, timeperiod=30) # NOTE: Commented to avoid a lot of Nan values # df['ta_overlap_ht_trendline'] = ta.HT_TRENDLINE(close) if ta_settings['momentum']: df['ta_momentum_adx'] = ta.ADX(high, low, close, timeperiod=14) df['ta_momentum_adxr'] = ta.ADXR(high, low, close, timeperiod=14) df['ta_momentum_apo'] = ta.APO(close, fastperiod=12, slowperiod=26, matype=0) df['ta_momentum_aroondown'], df['ta_momentum_aroonup'] = ta.AROON( high, low, timeperiod=14) df['ta_momentum_aroonosc'] = ta.AROONOSC(high, low, timeperiod=14) df['ta_momentum_bop'] = ta.BOP(open, high, low, close) df['ta_momentum_cci'] = ta.CCI(high, low, close, timeperiod=14) df['ta_momentum_cmo'] = ta.CMO(close, timeperiod=14) df['ta_momentum_dx'] = ta.DX(high, low, close, timeperiod=14) df['ta_momentum_macd_macd'], df['ta_momentum_macd_signal'], df[ 'ta_momentum_macd_hist'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) df['ta_momentum_macdext_macd'], df['ta_momentum_macdext_signal'], df[ 'ta_momentum_macdext_hist'] = ta.MACDEXT(close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) df['ta_momentum_macdfix_macd'], df['ta_momentum_macdfix_signal'], df[ 'ta_momentum_macdfix_hist'] = ta.MACDFIX(close, signalperiod=9) df['ta_momentum_mfi'] = ta.MFI(high, low, close, volume, timeperiod=14) df['ta_momentum_minus_di'] = ta.MINUS_DI(high, low, close, timeperiod=14) df['ta_momentum_minus_dm'] = ta.MINUS_DM(high, low, timeperiod=14) df['ta_momentum_mom'] = ta.MOM(close, timeperiod=10) df['ta_momentum_plus_di'] = ta.PLUS_DI(high, low, close, timeperiod=14) df['ta_momentum_plus_dm'] = ta.PLUS_DM(high, low, timeperiod=14) df['ta_momentum_ppo'] = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0) df['ta_momentum_roc'] = ta.ROC(close, timeperiod=10) df['ta_momentum_rocp'] = ta.ROCP(close, timeperiod=10) df['ta_momentum_rocr'] = ta.ROCR(close, timeperiod=10) df['ta_momentum_rocr100'] = ta.ROCR100(close, timeperiod=10) df['ta_momentum_rsi'] = ta.RSI(close, timeperiod=14) df['ta_momentum_slowk'], df['ta_momentum_slowd'] = ta.STOCH( high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df['ta_momentum_fastk'], df['ta_momentum_fastd'] = ta.STOCHF( high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) df['ta_momentum_fastk'], df['ta_momentum_fastd'] = ta.STOCHRSI( close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) df['ta_momentum_trix'] = ta.TRIX( close, timeperiod=12) # NOTE: Changed to avoid a lot of Nan values df['ta_momentum_ultosc'] = ta.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) df['ta_momentum_willr'] = ta.WILLR(high, low, close, timeperiod=14) if ta_settings['volume']: df['ta_volume_ad'] = ta.AD(high, low, close, volume) df['ta_volume_adosc'] = ta.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10) df['ta_volume_obv'] = ta.OBV(close, volume) if ta_settings['volatility']: df['ta_volatility_atr'] = ta.ATR(high, low, close, timeperiod=14) df['ta_volatility_natr'] = ta.NATR(high, low, close, timeperiod=14) df['ta_volatility_trange'] = ta.TRANGE(high, low, close) if ta_settings['price']: df['ta_price_avgprice'] = ta.AVGPRICE(open, high, low, close) df['ta_price_medprice'] = ta.MEDPRICE(high, low) df['ta_price_typprice'] = ta.TYPPRICE(high, low, close) df['ta_price_wclprice'] = ta.WCLPRICE(high, low, close) if ta_settings['cycle']: df['ta_cycle_ht_dcperiod'] = ta.HT_DCPERIOD(close) df['ta_cycle_ht_phasor_inphase'], df[ 'ta_cycle_ht_phasor_quadrature'] = ta.HT_PHASOR(close) df['ta_cycle_ht_trendmode'] = ta.HT_TRENDMODE(close) # NOTE: Commented to avoid a lot of Nan values # df['ta_cycle_ht_dcphase'] = ta.HT_DCPHASE(close) # df['ta_cycle_ht_sine_sine'], df['ta_cycle_ht_sine_leadsine'] = ta.HT_SINE(close) if ta_settings['pattern']: df['ta_pattern_cdl2crows'] = ta.CDL2CROWS(open, high, low, close) df['ta_pattern_cdl3blackrows'] = ta.CDL3BLACKCROWS( open, high, low, close) df['ta_pattern_cdl3inside'] = ta.CDL3INSIDE(open, high, low, close) df['ta_pattern_cdl3linestrike'] = ta.CDL3LINESTRIKE( open, high, low, close) df['ta_pattern_cdl3outside'] = ta.CDL3OUTSIDE(open, high, low, close) df['ta_pattern_cdl3starsinsouth'] = ta.CDL3STARSINSOUTH( open, high, low, close) df['ta_pattern_cdl3whitesoldiers'] = ta.CDL3WHITESOLDIERS( open, high, low, close) df['ta_pattern_cdlabandonedbaby'] = ta.CDLABANDONEDBABY(open, high, low, close, penetration=0) df['ta_pattern_cdladvanceblock'] = ta.CDLADVANCEBLOCK( open, high, low, close) df['ta_pattern_cdlbelthold'] = ta.CDLBELTHOLD(open, high, low, close) df['ta_pattern_cdlbreakaway'] = ta.CDLBREAKAWAY(open, high, low, close) df['ta_pattern_cdlclosingmarubozu'] = ta.CDLCLOSINGMARUBOZU( open, high, low, close) df['ta_pattern_cdlconcealbabyswall'] = ta.CDLCONCEALBABYSWALL( open, high, low, close) df['ta_pattern_cdlcounterattack'] = ta.CDLCOUNTERATTACK( open, high, low, close) df['ta_pattern_cdldarkcloudcover'] = ta.CDLDARKCLOUDCOVER( open, high, low, close, penetration=0) df['ta_pattern_cdldoji'] = ta.CDLDOJI(open, high, low, close) df['ta_pattern_cdldojistar'] = ta.CDLDOJISTAR(open, high, low, close) df['ta_pattern_cdldragonflydoji'] = ta.CDLDRAGONFLYDOJI( open, high, low, close) df['ta_pattern_cdlengulfing'] = ta.CDLENGULFING(open, high, low, close) df['ta_pattern_cdleveningdojistar'] = ta.CDLEVENINGDOJISTAR( open, high, low, close, penetration=0) df['ta_pattern_cdleveningstar'] = ta.CDLEVENINGSTAR(open, high, low, close, penetration=0) df['ta_pattern_cdlgapsidesidewhite'] = ta.CDLGAPSIDESIDEWHITE( open, high, low, close) df['ta_pattern_cdlgravestonedoji'] = ta.CDLGRAVESTONEDOJI( open, high, low, close) df['ta_pattern_cdlhammer'] = ta.CDLHAMMER(open, high, low, close) df['ta_pattern_cdlhangingman'] = ta.CDLHANGINGMAN( open, high, low, close) df['ta_pattern_cdlharami'] = ta.CDLHARAMI(open, high, low, close) df['ta_pattern_cdlharamicross'] = ta.CDLHARAMICROSS( open, high, low, close) df['ta_pattern_cdlhighwave'] = ta.CDLHIGHWAVE(open, high, low, close) df['ta_pattern_cdlhikkake'] = ta.CDLHIKKAKE(open, high, low, close) df['ta_pattern_cdlhikkakemod'] = ta.CDLHIKKAKEMOD( open, high, low, close) df['ta_pattern_cdlhomingpigeon'] = ta.CDLHOMINGPIGEON( open, high, low, close) df['ta_pattern_cdlidentical3crows'] = ta.CDLIDENTICAL3CROWS( open, high, low, close) df['ta_pattern_cdlinneck'] = ta.CDLINNECK(open, high, low, close) df['ta_pattern_cdlinvertedhammer'] = ta.CDLINVERTEDHAMMER( open, high, low, close) df['ta_pattern_cdlkicking'] = ta.CDLKICKING(open, high, low, close) df['ta_pattern_cdlkickingbylength'] = ta.CDLKICKINGBYLENGTH( open, high, low, close) df['ta_pattern_cdlladderbottom'] = ta.CDLLADDERBOTTOM( open, high, low, close) df['ta_pattern_cdllongleggeddoji'] = ta.CDLLONGLEGGEDDOJI( open, high, low, close) df['ta_pattern_cdllongline'] = ta.CDLLONGLINE(open, high, low, close) df['ta_pattern_cdlmarubozu'] = ta.CDLMARUBOZU(open, high, low, close) df['ta_pattern_cdlmatchinglow'] = ta.CDLMATCHINGLOW( open, high, low, close) df['ta_pattern_cdlmathold'] = ta.CDLMATHOLD(open, high, low, close, penetration=0) df['ta_pattern_cdlmorningdojistar'] = ta.CDLMORNINGDOJISTAR( open, high, low, close, penetration=0) df['ta_pattern_cdlmorningstar'] = ta.CDLMORNINGSTAR(open, high, low, close, penetration=0) df['ta_pattern_cdllonneck'] = ta.CDLONNECK(open, high, low, close) df['ta_pattern_cdlpiercing'] = ta.CDLPIERCING(open, high, low, close) df['ta_pattern_cdlrickshawman'] = ta.CDLRICKSHAWMAN( open, high, low, close) df['ta_pattern_cdlrisefall3methods'] = ta.CDLRISEFALL3METHODS( open, high, low, close) df['ta_pattern_cdlseparatinglines'] = ta.CDLSEPARATINGLINES( open, high, low, close) df['ta_pattern_cdlshootingstar'] = ta.CDLSHOOTINGSTAR( open, high, low, close) df['ta_pattern_cdlshortline'] = ta.CDLSHORTLINE(open, high, low, close) df['ta_pattern_cdlspinningtop'] = ta.CDLSPINNINGTOP( open, high, low, close) df['ta_pattern_cdlstalledpattern'] = ta.CDLSTALLEDPATTERN( open, high, low, close) df['ta_pattern_cdlsticksandwich'] = ta.CDLSTICKSANDWICH( open, high, low, close) df['ta_pattern_cdltakuri'] = ta.CDLTAKURI(open, high, low, close) df['ta_pattern_cdltasukigap'] = ta.CDLTASUKIGAP(open, high, low, close) df['ta_pattern_cdlthrusting'] = ta.CDLTHRUSTING(open, high, low, close) df['ta_pattern_cdltristar'] = ta.CDLTRISTAR(open, high, low, close) df['ta_pattern_cdlunique3river'] = ta.CDLUNIQUE3RIVER( open, high, low, close) df['ta_pattern_cdlupsidegap2crows'] = ta.CDLUPSIDEGAP2CROWS( open, high, low, close) df['ta_pattern_cdlxsidegap3methods'] = ta.CDLXSIDEGAP3METHODS( open, high, low, close) if ta_settings['statistic']: df['ta_statistic_beta'] = ta.BETA(high, low, timeperiod=5) df['ta_statistic_correl'] = ta.CORREL(high, low, timeperiod=30) df['ta_statistic_linearreg'] = ta.LINEARREG(close, timeperiod=14) df['ta_statistic_linearreg_angle'] = ta.LINEARREG_ANGLE(close, timeperiod=14) df['ta_statistic_linearreg_intercept'] = ta.LINEARREG_INTERCEPT( close, timeperiod=14) df['ta_statistic_linearreg_slope'] = ta.LINEARREG_SLOPE(close, timeperiod=14) df['ta_statistic_stddev'] = ta.STDDEV(close, timeperiod=5, nbdev=1) df['ta_statistic_tsf'] = ta.TSF(close, timeperiod=14) df['ta_statistic_var'] = ta.VAR(close, timeperiod=5, nbdev=1) if ta_settings['math_transforms']: df['ta_math_transforms_atan'] = ta.ATAN(close) df['ta_math_transforms_ceil'] = ta.CEIL(close) df['ta_math_transforms_cos'] = ta.COS(close) df['ta_math_transforms_floor'] = ta.FLOOR(close) df['ta_math_transforms_ln'] = ta.LN(close) df['ta_math_transforms_log10'] = ta.LOG10(close) df['ta_math_transforms_sin'] = ta.SIN(close) df['ta_math_transforms_sqrt'] = ta.SQRT(close) df['ta_math_transforms_tan'] = ta.TAN(close) if ta_settings['math_operators']: df['ta_math_operators_add'] = ta.ADD(high, low) df['ta_math_operators_div'] = ta.DIV(high, low) df['ta_math_operators_min'], df['ta_math_operators_max'] = ta.MINMAX( close, timeperiod=30) df['ta_math_operators_minidx'], df[ 'ta_math_operators_maxidx'] = ta.MINMAXINDEX(close, timeperiod=30) df['ta_math_operators_mult'] = ta.MULT(high, low) df['ta_math_operators_sub'] = ta.SUB(high, low) df['ta_math_operators_sum'] = ta.SUM(close, timeperiod=30) return df
def build_talib_factors(df_ftr, tp=10): tporg = tp mul = 2 rtn_divisor = [1, 1 / 8] # momentum df_ftr['BOP'] = ta.BOP(df_ftr.open.values, df_ftr.high.values, df_ftr.low.values, df_ftr.close.values) # volatility df_ftr['TRANGE'] = ta.TRANGE(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values) # volume df_ftr['AD'] = ta.AD(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, df_ftr.volume.values) # df_ftr['AD_ANGLE']=ta.LINEARREG_ANGLE(df_ftr['AD'].values, timeperiod=tp) too little variation # overlap df_ftr['OBV'] = ta.OBV(df_ftr.close.values, df_ftr.volume.values) for i in range(len(rtn_divisor)): tp = int(tporg // rtn_divisor[i]) if tp <= 3: continue x = str(i) ######## self defined df_ftr['rtn_disper'] = df_ftr['high'] - df_ftr['low'] df_ftr['rtn_disper_rolling' + x] = df_ftr['rtn_disper'].rolling( int(tp)).mean() ######## momentum indicators # see descriptoin for the values df_ftr['close_slope' + x] and others see the freq it falls into the ranges df_ftr['close_slope' + x] = ta.LINEARREG_SLOPE(df_ftr['close'].values, timeperiod=tp) df_ftr['close_slope_std' + x] = df_ftr['close_slope' + x].rolling( int(tp)).std() # rsi df_ftr['rsi' + x] = ta.RSI(df_ftr.close.values, timeperiod=tp) df_ftr['rsi_mean' + x] = ta.SUM(df_ftr['rsi' + x].values, timeperiod=tp) / tp df_ftr['storsi' + x] = (df_ftr['rsi' + x] - df_ftr['rsi' + x].rolling(tp).min() ) / (df_ftr['rsi' + x].rolling(tp).max() - df_ftr['rsi' + x].rolling(tp).min()) # stochastic df_ftr['slowk' + x], df_ftr['slowd' + x] = ta.STOCH( df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, fastk_period=round(tp * mul), slowk_period=tp, slowk_matype=0, slowd_period=tp, slowd_matype=0) # slowd is slow sto, slowk is fast sto df_ftr['slowj'] = (3 * df_ftr['slowd' + x]) - (2 * df_ftr['slowk' + x]) df_ftr['fastk' + x], df_ftr['fastd' + x] = ta.STOCHF( df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, fastk_period=tp, fastd_period=tp // mul, fastd_matype=0) df_ftr['mom' + x] = ta.MOM(df_ftr.close.values, timeperiod=tp) # directional change df_ftr['plus_di' + x] = ta.PLUS_DI(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, timeperiod=tp) df_ftr['plus_dm' + x] = ta.PLUS_DM(df_ftr.high.values, df_ftr.low.values, timeperiod=tp) df_ftr['MINUS_DI' + x] = ta.MINUS_DI(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, timeperiod=tp) df_ftr['MINUS_DM' + x] = ta.MINUS_DM(df_ftr.high.values, df_ftr.low.values, timeperiod=tp) df_ftr['plus_minus_di' + x] = df_ftr['plus_di' + x] - df_ftr['MINUS_DI' + x] df_ftr['DX' + x] = ta.DX(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, timeperiod=tp) df_ftr['ADX' + x] = ta.ADX(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, timeperiod=tp) df_ftr['ADXR' + x] = ta.ADXR(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, timeperiod=tp) # MACD df_ftr['MACD' + x], df_ftr['macdsignal' + x], df_ftr['macdhist' + x] = ta.MACD( df_ftr.close.values, fastperiod=tp, slowperiod=round(tp * 2), signalperiod=tp // mul) # http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_average_convergence_divergence_macd # Aroon # https://tradingsim.com/blog/aroon-indicator/ df_ftr['aroondown' + x], df_ftr['aroonup' + x] = ta.AROON( df_ftr.high.values, df_ftr.low.values, timeperiod=tp) df_ftr['AROONOSC' + x] = ta.AROONOSC(df_ftr.high.values, df_ftr.low.values, timeperiod=tp) # Chande Momentum Oscillator # https://www.investopedia.com/terms/c/chandemomentumoscillator.asp df_ftr['CMO' + x] = ta.CMO(df_ftr.close.values, timeperiod=tp) # Money Flow Index # http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:money_flow_index_mfi df_ftr['MFI' + x] = ta.MFI(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, df_ftr.volume.values, timeperiod=tp) df_ftr['MFI_slope' + x] = ta.LINEARREG_SLOPE(df_ftr['MFI' + x].values, timeperiod=tp) # MACD with controllable MA type df_ftr['macdEXT' + x], df_ftr['macdEXTsignal' + x], df_ftr['macdEXThist' + x] = ta.MACDEXT( df_ftr.close.values, fastperiod=tp // mul, fastmatype=0, slowperiod=tp, slowmatype=0, signalperiod=9, signalmatype=0) df_ftr['macdEXT_slope' + x] = ta.LINEARREG_SLOPE(df_ftr['macdEXT' + x].values, timeperiod=tp) df_ftr['macdFIX' + x], df_ftr['macdFIXsignal' + x], df_ftr['macdFIXhist' + x] = ta.MACDFIX( df_ftr.close.values, signalperiod=9) # Stochastic Relative Strength Index df_ftr['fastkRSI' + x], df_ftr['fastdRSI' + x] = ta.STOCHRSI( df_ftr.close.values, timeperiod=tp, fastk_period=tp // 2, fastd_period=tp // (mul * 2), fastd_matype=0) # volatility df_ftr['ATR' + x] = ta.ATR(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, timeperiod=tp) df_ftr['NATR' + x] = ta.NATR(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, timeperiod=tp) # volume df_ftr['ADOSC' + x] = ta.ADOSC(df_ftr.high.values, df_ftr.low.values, df_ftr.close.values, df_ftr.volume.values, fastperiod=tp, slowperiod=tp * mul) df_ftr['AD_SLOPE' + x] = ta.LINEARREG_SLOPE(df_ftr['AD'].values, timeperiod=tp) df_ftr['AD_SLOPE_std' + x] = df_ftr['AD_SLOPE' + x].rolling( int(tp * 20)).std() df_ftr['OBV_slope' + x] = ta.LINEARREG_SLOPE(df_ftr['OBV'].values, timeperiod=tp) # cycle df_ftr['HT_DCPERIOD' + x] = df_ftr['HT_DCPERIOD'].pct_change(periods=int(tp)).values df_ftr['HT_TRENDLINE' + x] = pd.DataFrame( ta.HT_TRENDLINE( df_ftr.close.values)).pct_change(periods=int(tp)).values # statistics df_ftr['STDDEV' + x] = ta.STDDEV(df_ftr.close.values, timeperiod=tp, nbdev=1) # NbDev = How may deviations you want this function to return (normally = 1). df_ftr[ 'TSF' + x] = ta.TSF(df_ftr.close.values, timeperiod=tp) / df_ftr.close - 1 df_ftr['BETA' + x] = ta.BETA(df_ftr.high.values, df_ftr.low.values, timeperiod=tp) df_ftr['LINEARREG_SLOPE' + x] = ta.LINEARREG_SLOPE(df_ftr.close.values, timeperiod=tp) df_ftr['CORREL' + x] = ta.CORREL(df_ftr.high.values, df_ftr.low.values, timeperiod=tp) # candle indicators - pattern recognition - unused features df_ftr = df_ftr.replace([np.inf, -np.inf], np.nan) col = list(df_ftr.columns.values) df_ftr.to_pickle(r'../data/ftr_ta.pkl')
def get_talib_stock_daily( stock_code, s, e, append_ori_close=False, norms=['volume', 'amount', 'ht_dcphase', 'obv', 'adosc', 'ad', 'cci']): """获取经过talib处理后的股票日线数据""" stock_data = QA.QA_fetch_stock_day_adv(stock_code, s, e) stock_df = stock_data.to_qfq().data if append_ori_close: stock_df['o_close'] = stock_data.data['close'] # stock_df['high_qfq'] = stock_data.to_qfq().data['high'] # stock_df['low_hfq'] = stock_data.to_hfq().data['low'] close = np.array(stock_df['close']) high = np.array(stock_df['high']) low = np.array(stock_df['low']) _open = np.array(stock_df['open']) _volume = np.array(stock_df['volume']) stock_df['dema'] = talib.DEMA(close) stock_df['ema'] = talib.EMA(close) stock_df['ht_tradeline'] = talib.HT_TRENDLINE(close) stock_df['kama'] = talib.KAMA(close) stock_df['ma'] = talib.MA(close) stock_df['mama'], stock_df['fama'] = talib.MAMA(close) # MAVP stock_df['midpoint'] = talib.MIDPOINT(close) stock_df['midprice'] = talib.MIDPRICE(high, low) stock_df['sar'] = talib.SAR(high, low) stock_df['sarext'] = talib.SAREXT(high, low) stock_df['sma'] = talib.SMA(close) stock_df['t3'] = talib.T3(close) stock_df['tema'] = talib.TEMA(close) stock_df['trima'] = talib.TRIMA(close) stock_df['wma'] = talib.WMA(close) stock_df['adx'] = talib.ADX(high, low, close) stock_df['adxr'] = talib.ADXR(high, low, close) stock_df['apo'] = talib.APO(close) stock_df['aroondown'], stock_df['aroonup'] = talib.AROON(high, low) stock_df['aroonosc'] = talib.AROONOSC(high, low) stock_df['bop'] = talib.BOP(_open, high, low, close) stock_df['cci'] = talib.CCI(high, low, close) stock_df['cmo'] = talib.CMO(close) stock_df['dx'] = talib.DX(high, low, close) # MACD stock_df['macd'], stock_df['macdsignal'], stock_df[ 'macdhist'] = talib.MACDEXT(close) # MACDFIX stock_df['mfi'] = talib.MFI(high, low, close, _volume) stock_df['minus_di'] = talib.MINUS_DI(high, low, close) stock_df['minus_dm'] = talib.MINUS_DM(high, low) stock_df['mom'] = talib.MOM(close) stock_df['plus_di'] = talib.PLUS_DI(high, low, close) stock_df['plus_dm'] = talib.PLUS_DM(high, low) stock_df['ppo'] = talib.PPO(close) stock_df['roc'] = talib.ROC(close) stock_df['rocp'] = talib.ROCP(close) stock_df['rocr'] = talib.ROCR(close) stock_df['rocr100'] = talib.ROCR100(close) stock_df['rsi'] = talib.RSI(close) stock_df['slowk'], stock_df['slowd'] = talib.STOCH(high, low, close) stock_df['fastk'], stock_df['fastd'] = talib.STOCHF(high, low, close) # STOCHRSI - Stochastic Relative Strength Index stock_df['trix'] = talib.TRIX(close) stock_df['ultosc'] = talib.ULTOSC(high, low, close) stock_df['willr'] = talib.WILLR(high, low, close) stock_df['ad'] = talib.AD(high, low, close, _volume) stock_df['adosc'] = talib.ADOSC(high, low, close, _volume) stock_df['obv'] = talib.OBV(close, _volume) stock_df['ht_dcperiod'] = talib.HT_DCPERIOD(close) stock_df['ht_dcphase'] = talib.HT_DCPHASE(close) stock_df['inphase'], stock_df['quadrature'] = talib.HT_PHASOR(close) stock_df['sine'], stock_df['leadsine'] = talib.HT_PHASOR(close) stock_df['ht_trendmode'] = talib.HT_TRENDMODE(close) stock_df['avgprice'] = talib.AVGPRICE(_open, high, low, close) stock_df['medprice'] = talib.MEDPRICE(high, low) stock_df['typprice'] = talib.TYPPRICE(high, low, close) stock_df['wclprice'] = talib.WCLPRICE(high, low, close) stock_df['atr'] = talib.ATR(high, low, close) stock_df['natr'] = talib.NATR(high, low, close) stock_df['trange'] = talib.TRANGE(high, low, close) stock_df['beta'] = talib.BETA(high, low) stock_df['correl'] = talib.CORREL(high, low) stock_df['linearreg'] = talib.LINEARREG(close) stock_df['linearreg_angle'] = talib.LINEARREG_ANGLE(close) stock_df['linearreg_intercept'] = talib.LINEARREG_INTERCEPT(close) stock_df['linearreg_slope'] = talib.LINEARREG_SLOPE(close) stock_df['stddev'] = talib.STDDEV(close) stock_df['tsf'] = talib.TSF(close) stock_df['var'] = talib.VAR(close) stock_df = stock_df.reset_index().set_index('date') if norms: x = stock_df[norms].values # returns a numpy array x_scaled = MinMaxScaler().fit_transform(x) stock_df = stock_df.drop(columns=norms).join( pd.DataFrame(x_scaled, columns=norms, index=stock_df.index)) # stock_df = stock_df.drop(columns=['code', 'open', 'high', 'low']) stock_df = stock_df.dropna() stock_df = stock_df.drop(columns=['code']) return stock_df
def get_datasets(symbol, to_symbol, histo, limit): """Fetch the API and precess the desired pair Arguments: symbol {str} -- First pair to_symbol {str} -- Second pair histo {str ['day', 'hour']} -- Granularity limit {int [100 - 2000]} -- [description] Returns: pandas.Dataframe -- The OHLCV and indicators dataframe """ headers = { 'User-Agent': 'Mozilla/5.0', 'authorization': 'Apikey 3d7d3e9e6006669ac00584978342451c95c3c78421268ff7aeef69995f9a09ce' } # OHLC url = 'https://min-api.cryptocompare.com/data/histo{}?fsym={}&tsym={}&e=Binance&limit={}'.format( histo, symbol, to_symbol, limit) print(colored('> downloading ' + symbol + ' OHLCV', 'green')) response = requests.get(url, headers=headers) json_response = response.json() status = json_response['Response'] if status == "Error": print(colored('=== {} ==='.format(json_response['Message']), 'red')) raise AssertionError() result = json_response['Data'] df = pd.DataFrame(result) df['Date'] = pd.to_datetime(df['time'], utc=True, unit='s') df.drop('time', axis=1, inplace=True) # indicators # https://github.com/mrjbq7/ta-lib/blob/master/docs/func.md open_price, high, low, close = np.array(df['open']), np.array( df['high']), np.array(df['low']), np.array(df['close']) volume = np.array(df['volumefrom']) # cycle indicators df.loc[:, 'HT_DCPERIOD'] = talib.HT_DCPERIOD(close) df.loc[:, 'HT_DCPHASE'] = talib.HT_DCPHASE(close) df.loc[:, 'HT_PHASOR_inphase'], df.loc[:, 'HT_PHASOR_quadrature'] = talib.HT_PHASOR( close) df.loc[:, 'HT_SINE_sine'], df.loc[:, 'HT_SINE_leadsine'] = talib.HT_SINE(close) df.loc[:, 'HT_TRENDMODE'] = talib.HT_TRENDMODE(close) # momemtum indicators df.loc[:, 'ADX'] = talib.ADX(high, low, close, timeperiod=14) df.loc[:, 'ADXR'] = talib.ADXR(high, low, close, timeperiod=14) df.loc[:, 'APO'] = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) df.loc[:, 'AROON_down'], df.loc[:, 'AROON_up'] = talib.AROON(high, low, timeperiod=14) df.loc[:, 'AROONOSC'] = talib.AROONOSC(high, low, timeperiod=14) df.loc[:, 'BOP'] = talib.BOP(open_price, high, low, close) df.loc[:, 'CCI'] = talib.CCI(high, low, close, timeperiod=14) df.loc[:, 'CMO'] = talib.CMO(close, timeperiod=14) df.loc[:, 'DX'] = talib.DX(high, low, close, timeperiod=14) df['MACD'], df['MACD_signal'], df['MACD_hist'] = talib.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) df.loc[:, 'MFI'] = talib.MFI(high, low, close, volume, timeperiod=14) df.loc[:, 'MINUS_DI'] = talib.MINUS_DI(high, low, close, timeperiod=14) df.loc[:, 'MINUS_DM'] = talib.MINUS_DM(high, low, timeperiod=14) df.loc[:, 'MOM'] = talib.MOM(close, timeperiod=10) df.loc[:, 'PPO'] = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) df.loc[:, 'ROC'] = talib.ROC(close, timeperiod=10) df.loc[:, 'RSI'] = talib.RSI(close, timeperiod=14) df.loc[:, 'STOCH_k'], df.loc[:, 'STOCH_d'] = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df.loc[:, 'STOCHF_k'], df.loc[:, 'STOCHF_d'] = talib.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) df.loc[:, 'STOCHRSI_K'], df.loc[:, 'STOCHRSI_D'] = talib.STOCHRSI( close, timeperiod=30, fastk_period=14, fastd_period=10, fastd_matype=1) df.loc[:, 'TRIX'] = talib.TRIX(close, timeperiod=30) df.loc[:, 'ULTOSC'] = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) df.loc[:, 'WILLR'] = talib.WILLR(high, low, close, timeperiod=14) # overlap studies df.loc[:, 'BBANDS_upper'], df.loc[:, 'BBANDS_middle'], df.loc[:, 'BBANDS_lower'] = talib.BBANDS( close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) df.loc[:, 'DEMA'] = talib.DEMA(close, timeperiod=30) df.loc[:, 'EMA'] = talib.EMA(close, timeperiod=30) df.loc[:, 'HT_TRENDLINE'] = talib.HT_TRENDLINE(close) df.loc[:, 'KAMA'] = talib.KAMA(close, timeperiod=30) df.loc[:, 'MA'] = talib.MA(close, timeperiod=30, matype=0) df.loc[:, 'MIDPOINT'] = talib.MIDPOINT(close, timeperiod=14) df.loc[:, 'WMA'] = talib.WMA(close, timeperiod=30) df.loc[:, 'SMA'] = talib.SMA(close) # pattern recoginition df.loc[:, 'CDL2CROWS'] = talib.CDL2CROWS(open_price, high, low, close) df.loc[:, 'CDL3BLACKCROWS'] = talib.CDL3BLACKCROWS(open_price, high, low, close) df.loc[:, 'CDL3INSIDE'] = talib.CDL3INSIDE(open_price, high, low, close) df.loc[:, 'CDL3LINESTRIKE'] = talib.CDL3LINESTRIKE(open_price, high, low, close) # price transform df.loc[:, 'WCLPRICE'] = talib.WCLPRICE(high, low, close) # statistic funcitons df.loc[:, 'BETA'] = talib.BETA(high, low, timeperiod=5) df.loc[:, 'CORREL'] = talib.CORREL(high, low, timeperiod=30) df.loc[:, 'STDDEV'] = talib.STDDEV(close, timeperiod=5, nbdev=1) df.loc[:, 'TSF'] = talib.TSF(close, timeperiod=14) df.loc[:, 'VAR'] = talib.VAR(close, timeperiod=5, nbdev=1) # volatility indicators df.loc[:, 'ATR'] = talib.ATR(high, low, close, timeperiod=14) df.loc[:, 'NATR'] = talib.NATR(high, low, close, timeperiod=14) df.loc[:, 'TRANGE'] = talib.TRANGE(high, low, close) # volume indicators df.loc[:, 'AD'] = talib.AD(high, low, close, volume) df.loc[:, 'ADOSC'] = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10) df.loc[:, 'OBV'] = talib.OBV(close, volume) # wallet indicator to trading bot # df.loc[:, 'wallet_{}'.format(symbol)] = 1.0 df.loc[:, 'wallet_first_symbol'] = WALLET_FIRST_SYMBOL df.loc[:, 'wallet_second_symbol'] = WALLET_SECOND_SYMBOL # df.loc[:, 'wallet_{}'.format(to_symbol)] = 0.0 # df.fillna(df.mean(), inplace=True) df.dropna(inplace=True) df.set_index('Date', inplace=True) train_size = round(len(df) * 0.5) # 50% to train -> test with different value df_train = df[:train_size] df_rollout = df[train_size:] df_train.to_csv('datasets/bot_train_{}_{}_{}.csv'.format( symbol + to_symbol, limit, histo)) df_rollout.to_csv('datasets/bot_rollout_{}_{}_{}.csv'.format( symbol + to_symbol, limit, histo)) return df_train, df_rollout
def CORREL(data, **kwargs): _check_talib_presence() popen, phigh, plow, pclose, pvolume = _extract_ohlc(data) return talib.CORREL(phigh, plow, **kwargs)
resorted['high'], resorted['low'], resorted['close']) CDLUNIQUE3RIVER_real = talib.CDLUNIQUE3RIVER( resorted['open'], resorted['high'], resorted['low'], resorted['close']) CDLUPSIDEGAP2CROWS_real = talib.CDLUPSIDEGAP2CROWS( resorted['open'], resorted['high'], resorted['low'], resorted['close']) CDLXSIDEGAP3METHODS_real = talib.CDLXSIDEGAP3METHODS( resorted['open'], resorted['high'], resorted['low'], resorted['close']) #stats beta_real = talib.BETA(resorted['high'], resorted['low']) correl_real = talib.CORREL(resorted['high'], resorted['low']) linearreg_real = talib.LINEARREG(resorted['close']) linearregang_real = talib.LINEARREG_ANGLE(resorted['close']) linearreginter_real = talib.LINEARREG_INTERCEPT( resorted['close']) linearregslope_real = talib.LINEARREG_SLOPE(resorted['close']) stdev_real = talib.STDDEV(resorted['close']) tsf_real = talib.TSF(resorted['close']) var_real = talib.VAR(resorted['close']) fileoutput = 'indicators-' + key1 + "-" + key2 + ".csv" try: os.remove(fileoutput) except: print('clean ' + fileoutput) with open(fileoutput, 'a', newline='') as csvfile:
df['ATR1'] = abs(np.array(df['High'].shift(1)) - np.array(df['Low'].shift(1))) df['ATR2'] = abs( np.array(df['High'].shift(1)) - np.array(df['Adj Close'].shift(1))) df['ATR3'] = abs( np.array(df['Low'].shift(1)) - np.array(df['Adj Close'].shift(1))) df['AverageTrueRange'] = df[['ATR1', 'ATR2', 'ATR3']].max(axis=1) # df['EMA']=pd.Series(pd.ewma(df['Adj Close'], span = n, min_periods = n - 1)) # Statistic Functions df['Beta'] = ta.BETA(np.array(df['High'].shift(1)), np.array(df['Low'].shift(1)), timeperiod=n) df['CORREL'] = ta.CORREL(np.array(df['High'].shift(1)), np.array(df['Low'].shift(1)), timeperiod=n) df['LINEARREG'] = ta.LINEARREG(np.array(df['Adj Close'].shift(1)), timeperiod=n) df['LINEARREG_ANGLE'] = ta.LINEARREG_ANGLE(np.array(df['Adj Close'].shift(1)), timeperiod=n) df['LINEARREG_INTERCEPT'] = ta.LINEARREG_INTERCEPT(np.array( df['Adj Close'].shift(1)), timeperiod=n) df['LINEARREG_SLOPE'] = ta.LINEARREG_SLOPE(np.array(df['Adj Close'].shift(1)), timeperiod=n) df['STDDEV'] = ta.STDDEV(np.array(df['Adj Close'].shift(1)), timeperiod=n, nbdev=1) df['Time Series Forecast'] = ta.TSF(np.array(df['Adj Close'].shift(1)), timeperiod=n)
def handle_statistic_functions(args, axes, i, klines_df, close_times, display_count): # talib if args.BETA: name = 'BETA' real = talib.BETA(klines_df["high"], klines_df["low"], timeperiod=5) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name) if args.CORREL: name = 'CORREL' real = talib.CORREL(klines_df["high"], klines_df["low"], timeperiod=30) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name) if args.LINEARREG: name = 'LINEARREG' real = talib.LINEARREG(klines_df["close"], timeperiod=14) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name) if args.LINEARREG_ANGLE: name = 'LINEARREG_ANGLE' real = talib.LINEARREG_ANGLE(klines_df["close"], timeperiod=14) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name) if args.LINEARREG_INTERCEPT: name = 'LINEARREG_INTERCEPT' real = talib.LINEARREG_INTERCEPT(klines_df["close"], timeperiod=14) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name) if args.LINEARREG_SLOPE: name = 'LINEARREG_SLOPE' real = talib.LINEARREG_SLOPE(klines_df["close"], timeperiod=14) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name) if args.STDDEV: name = 'STDDEV' real = talib.STDDEV(klines_df["close"], timeperiod=5, nbdev=1) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name) if args.VAR: name = 'VAR' real = talib.VAR(klines_df["close"], timeperiod=5, nbdev=1) i += 1 axes[i].set_ylabel(name) axes[i].grid(True) axes[i].plot(close_times, real[-display_count:], "y:", label=name)
def TALIB_CORREL(close, timeperiod=30): '''00460,2,1''' return talib.CORREL(close, timeperiod)
def plot(self, is_oos_arr=None): #alpha_cpnl plt.figure(figsize=(15, 7)) for i in self.merge_dict: alpha_cpnl = np.zeros(len(self.merge_dict[i].alpha_cpnl) + 1) alpha_cpnl[1:] = self.merge_dict[i].alpha_cpnl alpha_cpnl_line = plt.plot(alpha_cpnl, linewidth=2, label=i) if not isinstance(self.resample_dates[0], str): dates = [str(i) for i in self.resample_dates] dates = self.resample_dates step = len(dates) / 8 space = [i for i in np.arange(len(dates)) if i % step == 0] dates_str = [i.split(' ')[0] for i in dates[space]] if len(np.unique(dates_str)) <= 3: step = len(dates) / 5 space = [i for i in np.arange(len(dates)) if i % step == 0] dates_str = [i for i in dates[space]] if is_oos_arr is not None: start = np.where(is_oos_arr == 1)[0][0] for i in np.unique(is_oos_arr)[1:]: end = np.where(is_oos_arr == i)[0][-1] p = plt.axvspan(start, end, edgecolor='red', facecolor='grey', linewidth=1, alpha=0.2) start = end else: plt.grid() plt.xticks(space, dates_str) plt.legend(loc=2) plt.xlabel('Date') plt.ylabel('PNL') plt.title('Accumulated profits & loss') plt.show() time.sleep(0.5) gf = GridFigure(4, 1) # alpha_drawdown ax = gf.next_row() for i in self.merge_dict: alpha_drawdown = self.merge_dict[i].alpha_drawdown ax.fill_between(np.arange(len(alpha_drawdown)), np.zeros_like(alpha_drawdown), alpha_drawdown, alpha=0.5, label=i) ax.set(ylabel='Alpha Drawdown') ax.legend(loc='lower left') #plt.show() # 两两提升对比图 combination_list = combination_2(self.merge_dict.keys()) ax = gf.next_row() for i in range(len(combination_list)): alpha_cpnl_1 = self.merge_dict[combination_list[i][0]].alpha_cpnl alpha_cpnl_2 = self.merge_dict[combination_list[i][1]].alpha_cpnl diff = alpha_cpnl_1 - alpha_cpnl_2 ax.fill_between(range(len(diff)), np.zeros_like(diff), diff, alpha=0.5, label="%s - %s" % (combination_list[i][0], combination_list[i][1])) ax.set(ylabel='cpnl_diff') ax.legend(loc='lower left') #plt.show() # 两两correlation rolling_period_length = 100 combination_list = combination_2(self.merge_dict.keys()) ax = gf.next_row() for i in range(len(combination_list)): alpha_pnl_1 = np.nan_to_num( self.merge_dict[combination_list[i][0]].alpha_pnl) alpha_pnl_2 = np.nan_to_num( self.merge_dict[combination_list[i][1]].alpha_pnl) rolling_corr = talib.CORREL(alpha_pnl_1, alpha_pnl_2, rolling_period_length) #rolling_corr = pd.DataFrame(alpha_pnl_1).rolling(rolling_period_length).corr(pd.DataFrame(alpha_pnl_2)).values.ravel() ax.fill_between(range(len(diff)), np.zeros_like(rolling_corr), rolling_corr, alpha=0.5, label="corr_%s(%s, %s)" % (rolling_period_length, combination_list[i][0], combination_list[i][1])) ax.set(ylabel='pnl correlation') ax.legend(loc='lower left') #plt.show() #截面correlation rolling_period_length = 100 combination_list = combination_2(self.merge_dict.keys()) ax = gf.next_row() for i in range(len(combination_list)): rolling_corr = np.zeros( len(self.factor_wgts_df_dict[combination_list[i][0]])) for j in range( len(self.factor_wgts_df_dict[combination_list[i][0]])): rolling_corr[j] = np.corrcoef( self.factor_wgts_df_dict[combination_list[i] [0]].iloc[j].values, self.factor_wgts_df_dict[combination_list[i] [1]].iloc[j].values)[0, 1] #rolling_corr = pd.DataFrame(alpha_pnl_1).rolling(rolling_period_length).corr(pd.DataFrame(alpha_pnl_2)).values.ravel() #ax.fill_between(range(len(rolling_corr)), np.zeros_like(rolling_corr), rolling_corr, alpha=0.5, label="corr_%s(%s, %s)" %(rolling_period_length, combination_list[i][0], combination_list[i][1])) #ax.fill_between(range(len(diff)), np.zeros_like(rolling_corr), rolling_corr, alpha=0.5, label="corr_%s(%s, %s)" %(rolling_period_length, combination_list[i][0], combination_list[i][1])) ax.fill_between(range(len(diff)), np.zeros_like(rolling_corr), rolling_corr, alpha=0.5, label="%s, %s" % (combination_list[i][0], combination_list[i][1])) ax.set(ylabel='cross section correlation') ax.legend(loc='lower left') gf.show() gf.close()
def calculate(self, para): self.t = self.inputdata[:, 0] self.op = self.inputdata[:, 1] self.high = self.inputdata[:, 2] self.low = self.inputdata[:, 3] self.close = self.inputdata[:, 4] #adjusted close self.close1 = self.inputdata[:, 5] self.volume = self.inputdata[:, 6] #Overlap study #Overlap Studies #Overlap Studies if para is 'BBANDS': #Bollinger Bands upperband, middleband, lowerband = ta.BBANDS(self.close, timeperiod=self.tp, nbdevup=2, nbdevdn=2, matype=0) self.output = [upperband, middleband, lowerband] elif para is 'DEMA': #Double Exponential Moving Average self.output = ta.DEMA(self.close, timeperiod=self.tp) elif para is 'EMA': #Exponential Moving Average self.output = ta.EMA(self.close, timeperiod=self.tp) elif para is 'HT_TRENDLINE': #Hilbert Transform - Instantaneous Trendline self.output = ta.HT_TRENDLINE(self.close) elif para is 'KAMA': #Kaufman Adaptive Moving Average self.output = ta.KAMA(self.close, timeperiod=self.tp) elif para is 'MA': #Moving average self.output = ta.MA(self.close, timeperiod=self.tp, matype=0) elif para is 'MAMA': #MESA Adaptive Moving Average mama, fama = ta.MAMA(self.close, fastlimit=0, slowlimit=0) elif para is 'MAVP': #Moving average with variable period self.output = ta.MAVP(self.close, periods=10, minperiod=self.tp, maxperiod=self.tp1, matype=0) elif para is 'MIDPOINT': #MidPoint over period self.output = ta.MIDPOINT(self.close, timeperiod=self.tp) elif para is 'MIDPRICE': #Midpoint Price over period self.output = ta.MIDPRICE(self.high, self.low, timeperiod=self.tp) elif para is 'SAR': #Parabolic SAR self.output = ta.SAR(self.high, self.low, acceleration=0, maximum=0) elif para is 'SAREXT': #Parabolic SAR - Extended self.output = ta.SAREXT(self.high, self.low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0) elif para is 'SMA': #Simple Moving Average self.output = ta.SMA(self.close, timeperiod=self.tp) elif para is 'T3': #Triple Exponential Moving Average (T3) self.output = ta.T3(self.close, timeperiod=self.tp, vfactor=0) elif para is 'TEMA': #Triple Exponential Moving Average self.output = ta.TEMA(self.close, timeperiod=self.tp) elif para is 'TRIMA': #Triangular Moving Average self.output = ta.TRIMA(self.close, timeperiod=self.tp) elif para is 'WMA': #Weighted Moving Average self.output = ta.WMA(self.close, timeperiod=self.tp) #Momentum Indicators elif para is 'ADX': #Average Directional Movement Index self.output = ta.ADX(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'ADXR': #Average Directional Movement Index Rating self.output = ta.ADXR(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'APO': #Absolute Price Oscillator self.output = ta.APO(self.close, fastperiod=12, slowperiod=26, matype=0) elif para is 'AROON': #Aroon aroondown, aroonup = ta.AROON(self.high, self.low, timeperiod=self.tp) self.output = [aroondown, aroonup] elif para is 'AROONOSC': #Aroon Oscillator self.output = ta.AROONOSC(self.high, self.low, timeperiod=self.tp) elif para is 'BOP': #Balance Of Power self.output = ta.BOP(self.op, self.high, self.low, self.close) elif para is 'CCI': #Commodity Channel Index self.output = ta.CCI(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'CMO': #Chande Momentum Oscillator self.output = ta.CMO(self.close, timeperiod=self.tp) elif para is 'DX': #Directional Movement Index self.output = ta.DX(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'MACD': #Moving Average Convergence/Divergence macd, macdsignal, macdhist = ta.MACD(self.close, fastperiod=12, slowperiod=26, signalperiod=9) self.output = [macd, macdsignal, macdhist] elif para is 'MACDEXT': #MACD with controllable MA type macd, macdsignal, macdhist = ta.MACDEXT(self.close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) self.output = [macd, macdsignal, macdhist] elif para is 'MACDFIX': #Moving Average Convergence/Divergence Fix 12/26 macd, macdsignal, macdhist = ta.MACDFIX(self.close, signalperiod=9) self.output = [macd, macdsignal, macdhist] elif para is 'MFI': #Money Flow Index self.output = ta.MFI(self.high, self.low, self.close, self.volume, timeperiod=self.tp) elif para is 'MINUS_DI': #Minus Directional Indicator self.output = ta.MINUS_DI(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'MINUS_DM': #Minus Directional Movement self.output = ta.MINUS_DM(self.high, self.low, timeperiod=self.tp) elif para is 'MOM': #Momentum self.output = ta.MOM(self.close, timeperiod=10) elif para is 'PLUS_DI': #Plus Directional Indicator self.output = ta.PLUS_DI(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'PLUS_DM': #Plus Directional Movement self.output = ta.PLUS_DM(self.high, self.low, timeperiod=self.tp) elif para is 'PPO': #Percentage Price Oscillator self.output = ta.PPO(self.close, fastperiod=12, slowperiod=26, matype=0) elif para is 'ROC': #Rate of change : ((price/prevPrice)-1)*100 self.output = ta.ROC(self.close, timeperiod=10) elif para is 'ROCP': #Rate of change Percentage: (price-prevPrice)/prevPrice self.output = ta.ROCP(self.close, timeperiod=10) elif para is 'ROCR': #Rate of change ratio: (price/prevPrice) self.output = ta.ROCR(self.close, timeperiod=10) elif para is 'ROCR100': #Rate of change ratio 100 scale: (price/prevPrice)*100 self.output = ta.ROCR100(self.close, timeperiod=10) elif para is 'RSI': #Relative Strength Index self.output = ta.RSI(self.close, timeperiod=self.tp) elif para is 'STOCH': #Stochastic slowk, slowd = ta.STOCH(self.high, self.low, self.close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) self.output = [slowk, slowd] elif para is 'STOCHF': #Stochastic Fast fastk, fastd = ta.STOCHF(self.high, self.low, self.close, fastk_period=5, fastd_period=3, fastd_matype=0) self.output = [fastk, fastd] elif para is 'STOCHRSI': #Stochastic Relative Strength Index fastk, fastd = ta.STOCHRSI(self.close, timeperiod=self.tp, fastk_period=5, fastd_period=3, fastd_matype=0) self.output = [fastk, fastd] elif para is 'TRIX': #1-day Rate-Of-Change (ROC) of a Triple Smooth EMA self.output = ta.TRIX(self.close, timeperiod=self.tp) elif para is 'ULTOSC': #Ultimate Oscillator self.output = ta.ULTOSC(self.high, self.low, self.close, timeperiod1=self.tp, timeperiod2=self.tp1, timeperiod3=self.tp2) elif para is 'WILLR': #Williams' %R self.output = ta.WILLR(self.high, self.low, self.close, timeperiod=self.tp) # Volume Indicators : # elif para is 'AD': #Chaikin A/D Line self.output = ta.AD(self.high, self.low, self.close, self.volume) elif para is 'ADOSC': #Chaikin A/D Oscillator self.output = ta.ADOSC(self.high, self.low, self.close, self.volume, fastperiod=3, slowperiod=10) elif para is 'OBV': #On Balance Volume self.output = ta.OBV(self.close, self.volume) # Volatility Indicators: # elif para is 'ATR': #Average True Range self.output = ta.ATR(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'NATR': #Normalized Average True Range self.output = ta.NATR(self.high, self.low, self.close, timeperiod=self.tp) elif para is 'TRANGE': #True Range self.output = ta.TRANGE(self.high, self.low, self.close) #Price Transform : # elif para is 'AVGPRICE': #Average Price self.output = ta.AVGPRICE(self.op, self.high, self.low, self.close) elif para is 'MEDPRICE': #Median Price self.output = ta.MEDPRICE(self.high, self.low) elif para is 'TYPPRICE': #Typical Price self.output = ta.TYPPRICE(self.high, self.low, self.close) elif para is 'WCLPRICE': #Weighted Close Price self.output = ta.WCLPRICE(self.high, self.low, self.close) #Cycle Indicators : # elif para is 'HT_DCPERIOD': #Hilbert Transform - Dominant Cycle Period self.output = ta.HT_DCPERIOD(self.close) elif para is 'HT_DCPHASE': #Hilbert Transform - Dominant Cycle Phase self.output = ta.HT_DCPHASE(self.close) elif para is 'HT_PHASOR': #Hilbert Transform - Phasor Components inphase, quadrature = ta.HT_PHASOR(self.close) self.output = [inphase, quadrature] elif para is 'HT_SINE': #Hilbert Transform - SineWave #2 sine, leadsine = ta.HT_SINE(self.close) self.output = [sine, leadsine] elif para is 'HT_TRENDMODE': #Hilbert Transform - Trend vs Cycle Mode self.integer = ta.HT_TRENDMODE(self.close) #Pattern Recognition : # elif para is 'CDL2CROWS': #Two Crows self.integer = ta.CDL2CROWS(self.op, self.high, self.low, self.close) elif para is 'CDL3BLACKCROWS': #Three Black Crows self.integer = ta.CDL3BLACKCROWS(self.op, self.high, self.low, self.close) elif para is 'CDL3INSIDE': #Three Inside Up/Down self.integer = ta.CDL3INSIDE(self.op, self.high, self.low, self.close) elif para is 'CDL3LINESTRIKE': #Three-Line Strike self.integer = ta.CDL3LINESTRIKE(self.op, self.high, self.low, self.close) elif para is 'CDL3OUTSIDE': #Three Outside Up/Down self.integer = ta.CDL3OUTSIDE(self.op, self.high, self.low, self.close) elif para is 'CDL3STARSINSOUTH': #Three Stars In The South self.integer = ta.CDL3STARSINSOUTH(self.op, self.high, self.low, self.close) elif para is 'CDL3WHITESOLDIERS': #Three Advancing White Soldiers self.integer = ta.CDL3WHITESOLDIERS(self.op, self.high, self.low, self.close) elif para is 'CDLABANDONEDBABY': #Abandoned Baby self.integer = ta.CDLABANDONEDBABY(self.op, self.high, self.low, self.close, penetration=0) elif para is 'CDLBELTHOLD': #Belt-hold self.integer = ta.CDLBELTHOLD(self.op, self.high, self.low, self.close) elif para is 'CDLBREAKAWAY': #Breakaway self.integer = ta.CDLBREAKAWAY(self.op, self.high, self.low, self.close) elif para is 'CDLCLOSINGMARUBOZU': #Closing Marubozu self.integer = ta.CDLCLOSINGMARUBOZU(self.op, self.high, self.low, self.close) elif para is 'CDLCONCEALBABYSWALL': #Concealing Baby Swallow self.integer = ta.CDLCONCEALBABYSWALL(self.op, self.high, self.low, self.close) elif para is 'CDLCOUNTERATTACK': #Counterattack self.integer = ta.CDLCOUNTERATTACK(self.op, self.high, self.low, self.close) elif para is 'CDLDARKCLOUDCOVER': #Dark Cloud Cover self.integer = ta.CDLDARKCLOUDCOVER(self.op, self.high, self.low, self.close, penetration=0) elif para is 'CDLDOJI': #Doji self.integer = ta.CDLDOJI(self.op, self.high, self.low, self.close) elif para is 'CDLDOJISTAR': #Doji Star self.integer = ta.CDLDOJISTAR(self.op, self.high, self.low, self.close) elif para is 'CDLDRAGONFLYDOJI': #Dragonfly Doji self.integer = ta.CDLDRAGONFLYDOJI(self.op, self.high, self.low, self.close) elif para is 'CDLENGULFING': #Engulfing Pattern self.integer = ta.CDLENGULFING(self.op, self.high, self.low, self.close) elif para is 'CDLEVENINGDOJISTAR': #Evening Doji Star self.integer = ta.CDLEVENINGDOJISTAR(self.op, self.high, self.low, self.close, penetration=0) elif para is 'CDLEVENINGSTAR': #Evening Star self.integer = ta.CDLEVENINGSTAR(self.op, self.high, self.low, self.close, penetration=0) elif para is 'CDLGAPSIDESIDEWHITE': #Up/Down-gap side-by-side white lines self.integer = ta.CDLGAPSIDESIDEWHITE(self.op, self.high, self.low, self.close) elif para is 'CDLGRAVESTONEDOJI': #Gravestone Doji self.integer = ta.CDLGRAVESTONEDOJI(self.op, self.high, self.low, self.close) elif para is 'CDLHAMMER': #Hammer self.integer = ta.CDLHAMMER(self.op, self.high, self.low, self.close) elif para is 'CDLHANGINGMAN': #Hanging Man self.integer = ta.CDLHANGINGMAN(self.op, self.high, self.low, self.close) elif para is 'CDLHARAMI': #Harami Pattern self.integer = ta.CDLHARAMI(self.op, self.high, self.low, self.close) elif para is 'CDLHARAMICROSS': #Harami Cross Pattern self.integer = ta.CDLHARAMICROSS(self.op, self.high, self.low, self.close) elif para is 'CDLHIGHWAVE': #High-Wave Candle self.integer = ta.CDLHIGHWAVE(self.op, self.high, self.low, self.close) elif para is 'CDLHIKKAKE': #Hikkake Pattern self.integer = ta.CDLHIKKAKE(self.op, self.high, self.low, self.close) elif para is 'CDLHIKKAKEMOD': #Modified Hikkake Pattern self.integer = ta.CDLHIKKAKEMOD(self.op, self.high, self.low, self.close) elif para is 'CDLHOMINGPIGEON': #Homing Pigeon self.integer = ta.CDLHOMINGPIGEON(self.op, self.high, self.low, self.close) elif para is 'CDLIDENTICAL3CROWS': #Identical Three Crows self.integer = ta.CDLIDENTICAL3CROWS(self.op, self.high, self.low, self.close) elif para is 'CDLINNECK': #In-Neck Pattern self.integer = ta.CDLINNECK(self.op, self.high, self.low, self.close) elif para is 'CDLINVERTEDHAMMER': #Inverted Hammer self.integer = ta.CDLINVERTEDHAMMER(self.op, self.high, self.low, self.close) elif para is 'CDLKICKING': #Kicking self.integer = ta.CDLKICKING(self.op, self.high, self.low, self.close) elif para is 'CDLKICKINGBYLENGTH': #Kicking - bull/bear determined by the longer marubozu self.integer = ta.CDLKICKINGBYLENGTH(self.op, self.high, self.low, self.close) elif para is 'CDLLADDERBOTTOM': #Ladder Bottom self.integer = ta.CDLLADDERBOTTOM(self.op, self.high, self.low, self.close) elif para is 'CDLLONGLEGGEDDOJI': #Long Legged Doji self.integer = ta.CDLLONGLEGGEDDOJI(self.op, self.high, self.low, self.close) elif para is 'CDLLONGLINE': #Long Line Candle self.integer = ta.CDLLONGLINE(self.op, self.high, self.low, self.close) elif para is 'CDLMARUBOZU': #Marubozu self.integer = ta.CDLMARUBOZU(self.op, self.high, self.low, self.close) elif para is 'CDLMATCHINGLOW': #Matching Low self.integer = ta.CDLMATCHINGLOW(self.op, self.high, self.low, self.close) elif para is 'CDLMATHOLD': #Mat Hold self.integer = ta.CDLMATHOLD(self.op, self.high, self.low, self.close, penetration=0) elif para is 'CDLMORNINGDOJISTAR': #Morning Doji Star self.integer = ta.CDLMORNINGDOJISTAR(self.op, self.high, self.low, self.close, penetration=0) elif para is 'CDLMORNINGSTAR': #Morning Star self.integer = ta.CDLMORNINGSTAR(self.op, self.high, self.low, self.close, penetration=0) elif para is 'CDLONNECK': #On-Neck Pattern self.integer = ta.CDLONNECK(self.op, self.high, self.low, self.close) elif para is 'CDLPIERCING': #Piercing Pattern self.integer = ta.CDLPIERCING(self.op, self.high, self.low, self.close) elif para is 'CDLRICKSHAWMAN': #Rickshaw Man self.integer = ta.CDLRICKSHAWMAN(self.op, self.high, self.low, self.close) elif para is 'CDLRISEFALL3METHODS': #Rising/Falling Three Methods self.integer = ta.CDLRISEFALL3METHODS(self.op, self.high, self.low, self.close) elif para is 'CDLSEPARATINGLINES': #Separating Lines self.integer = ta.CDLSEPARATINGLINES(self.op, self.high, self.low, self.close) elif para is 'CDLSHOOTINGSTAR': #Shooting Star self.integer = ta.CDLSHOOTINGSTAR(self.op, self.high, self.low, self.close) elif para is 'CDLSHORTLINE': #Short Line Candle self.integer = ta.CDLSHORTLINE(self.op, self.high, self.low, self.close) elif para is 'CDLSPINNINGTOP': #Spinning Top self.integer = ta.CDLSPINNINGTOP(self.op, self.high, self.low, self.close) elif para is 'CDLSTALLEDPATTERN': #Stalled Pattern self.integer = ta.CDLSTALLEDPATTERN(self.op, self.high, self.low, self.close) elif para is 'CDLSTICKSANDWICH': #Stick Sandwich self.integer = ta.CDLSTICKSANDWICH(self.op, self.high, self.low, self.close) elif para is 'CDLTAKURI': #Takuri (Dragonfly Doji with very long lower shadow) self.integer = ta.CDLTAKURI(self.op, self.high, self.low, self.close) elif para is 'CDLTASUKIGAP': #Tasuki Gap self.integer = ta.CDLTASUKIGAP(self.op, self.high, self.low, self.close) elif para is 'CDLTHRUSTING': #Thrusting Pattern self.integer = ta.CDLTHRUSTING(self.op, self.high, self.low, self.close) elif para is 'CDLTRISTAR': #Tristar Pattern self.integer = ta.CDLTRISTAR(self.op, self.high, self.low, self.close) elif para is 'CDLUNIQUE3RIVER': #Unique 3 River self.integer = ta.CDLUNIQUE3RIVER(self.op, self.high, self.low, self.close) elif para is 'CDLUPSIDEGAP2CROWS': #Upside Gap Two Crows self.integer = ta.CDLUPSIDEGAP2CROWS(self.op, self.high, self.low, self.close) elif para is 'CDLXSIDEGAP3METHODS': #Upside/Downside Gap Three Methods self.integer = ta.CDLXSIDEGAP3METHODS(self.op, self.high, self.low, self.close) #Statistic Functions : # elif para is 'BETA': #Beta self.output = ta.BETA(self.high, self.low, timeperiod=5) elif para is 'CORREL': #Pearson's Correlation Coefficient (r) self.output = ta.CORREL(self.high, self.low, timeperiod=self.tp) elif para is 'LINEARREG': #Linear Regression self.output = ta.LINEARREG(self.close, timeperiod=self.tp) elif para is 'LINEARREG_ANGLE': #Linear Regression Angle self.output = ta.LINEARREG_ANGLE(self.close, timeperiod=self.tp) elif para is 'LINEARREG_INTERCEPT': #Linear Regression Intercept self.output = ta.LINEARREG_INTERCEPT(self.close, timeperiod=self.tp) elif para is 'LINEARREG_SLOPE': #Linear Regression Slope self.output = ta.LINEARREG_SLOPE(self.close, timeperiod=self.tp) elif para is 'STDDEV': #Standard Deviation self.output = ta.STDDEV(self.close, timeperiod=5, nbdev=1) elif para is 'TSF': #Time Series Forecast self.output = ta.TSF(self.close, timeperiod=self.tp) elif para is 'VAR': #Variance self.output = ta.VAR(self.close, timeperiod=5, nbdev=1) else: print('You issued command:' + para)
def CORREL(raw_df, timeperiod=30): # Pearson's Correlation Coefficient # extract necessary data from raw dataframe (high, low) return ta.CORREL(raw_df.High.values, raw_df.Low.values, timeperiod)
def calc_features(df): open = df['op'] high = df['hi'] low = df['lo'] close = df['cl'] volume = df['volume'] orig_columns = df.columns hilo = (df['hi'] + df['lo']) / 2 df['BBANDS_upperband'], df['BBANDS_middleband'], df[ 'BBANDS_lowerband'] = talib.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) df['BBANDS_upperband'] -= hilo df['BBANDS_middleband'] -= hilo df['BBANDS_lowerband'] -= hilo df['DEMA'] = talib.DEMA(close, timeperiod=30) - hilo df['EMA'] = talib.EMA(close, timeperiod=30) - hilo df['HT_TRENDLINE'] = talib.HT_TRENDLINE(close) - hilo df['KAMA'] = talib.KAMA(close, timeperiod=30) - hilo df['MA'] = talib.MA(close, timeperiod=30, matype=0) - hilo df['MIDPOINT'] = talib.MIDPOINT(close, timeperiod=14) - hilo df['SMA'] = talib.SMA(close, timeperiod=30) - hilo df['T3'] = talib.T3(close, timeperiod=5, vfactor=0) - hilo df['TEMA'] = talib.TEMA(close, timeperiod=30) - hilo df['TRIMA'] = talib.TRIMA(close, timeperiod=30) - hilo df['WMA'] = talib.WMA(close, timeperiod=30) - hilo 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['AROON_aroondown'], df['AROON_aroonup'] = talib.AROON(high, low, timeperiod=14) 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['DX'] = talib.DX(high, low, close, timeperiod=14) df['MACD_macd'], df['MACD_macdsignal'], df['MACD_macdhist'] = talib.MACD( close, fastperiod=12, slowperiod=26, signalperiod=9) # skip MACDEXT MACDFIX たぶん同じなので df['MFI'] = talib.MFI(high, low, close, volume, timeperiod=14) df['MINUS_DI'] = talib.MINUS_DI(high, low, close, timeperiod=14) df['MINUS_DM'] = talib.MINUS_DM(high, low, timeperiod=14) df['MOM'] = talib.MOM(close, timeperiod=10) df['PLUS_DI'] = talib.PLUS_DI(high, low, close, timeperiod=14) df['PLUS_DM'] = talib.PLUS_DM(high, low, timeperiod=14) df['RSI'] = talib.RSI(close, timeperiod=14) df['STOCH_slowk'], df['STOCH_slowd'] = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df['STOCHF_fastk'], df['STOCHF_fastd'] = talib.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) df['STOCHRSI_fastk'], df['STOCHRSI_fastd'] = talib.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) df['TRIX'] = talib.TRIX(close, timeperiod=30) df['ULTOSC'] = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) df['WILLR'] = talib.WILLR(high, low, close, timeperiod=14) df['AD'] = talib.AD(high, low, close, volume) df['ADOSC'] = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10) df['OBV'] = talib.OBV(close, volume) df['ATR'] = talib.ATR(high, low, close, timeperiod=14) df['NATR'] = talib.NATR(high, low, close, timeperiod=14) df['TRANGE'] = talib.TRANGE(high, low, close) df['HT_DCPERIOD'] = talib.HT_DCPERIOD(close) df['HT_DCPHASE'] = talib.HT_DCPHASE(close) df['HT_PHASOR_inphase'], df['HT_PHASOR_quadrature'] = talib.HT_PHASOR( close) df['HT_SINE_sine'], df['HT_SINE_leadsine'] = talib.HT_SINE(close) df['HT_TRENDMODE'] = talib.HT_TRENDMODE(close) df['BETA'] = talib.BETA(high, low, timeperiod=5) df['CORREL'] = talib.CORREL(high, low, timeperiod=30) df['LINEARREG'] = talib.LINEARREG(close, timeperiod=14) - close df['LINEARREG_ANGLE'] = talib.LINEARREG_ANGLE(close, timeperiod=14) df['LINEARREG_INTERCEPT'] = talib.LINEARREG_INTERCEPT( close, timeperiod=14) - close df['LINEARREG_SLOPE'] = talib.LINEARREG_SLOPE(close, timeperiod=14) df['STDDEV'] = talib.STDDEV(close, timeperiod=5, nbdev=1) return df
def ta(name, price_h, price_l, price_c, price_v, price_o): # function 'MAX'/'MAXINDEX'/'MIN'/'MININDEX'/'MINMAX'/'MINMAXINDEX'/'SUM' is missing if name == 'AD': return talib.AD(np.array(price_h), np.array(price_l), np.array(price_c), np.asarray(price_v, dtype='float')) if name == 'ADOSC': return talib.ADOSC(np.array(price_h), np.array(price_l), np.array(price_c), np.asarray(price_v, dtype='float'), fastperiod=2, slowperiod=10) if name == 'ADX': return talib.ADX(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'ADXR': return talib.ADXR(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'APO': return talib.APO(np.asarray(price_c, dtype='float'), fastperiod=12, slowperiod=26, matype=0) if name == 'AROON': AROON_DWON, AROON2_UP = talib.AROON(np.array(price_h), np.asarray(price_l, dtype='float'), timeperiod=90) return (AROON_DWON, AROON2_UP) if name == 'AROONOSC': return talib.AROONOSC(np.array(price_h), np.asarray(price_l, dtype='float'), timeperiod=14) if name == 'ATR': return talib.ATR(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'AVGPRICE': return talib.AVGPRICE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'BBANDS': BBANDS1, BBANDS2, BBANDS3 = talib.BBANDS(np.asarray(price_c, dtype='float'), matype=MA_Type.T3) return BBANDS1 if name == 'BETA': return talib.BETA(np.array(price_h), np.asarray(price_l, dtype='float'), timeperiod=5) if name == 'BOP': return talib.BOP(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CCI': return talib.CCI(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'CDL2CROWS': return talib.CDL2CROWS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDL3BLACKCROWS': return talib.CDL3BLACKCROWS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDL3INSIDE': return talib.CDL3INSIDE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDL3LINESTRIKE': return talib.CDL3LINESTRIKE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDL3OUTSIDE': return talib.CDL3OUTSIDE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDL3STARSINSOUTH': return talib.CDL3STARSINSOUTH(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDL3WHITESOLDIERS': return talib.CDL3WHITESOLDIERS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLABANDONEDBABY': return talib.CDLABANDONEDBABY(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), penetration=0) if name == 'CDLADVANCEBLOCK': return talib.CDLADVANCEBLOCK(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLBELTHOLD': return talib.CDLBELTHOLD(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLBREAKAWAY': return talib.CDLBREAKAWAY(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLCLOSINGMARUBOZU': return talib.CDLCLOSINGMARUBOZU(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLCONCEALBABYSWALL': return talib.CDLCONCEALBABYSWALL(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLCOUNTERATTACK': return talib.CDLCOUNTERATTACK(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLDARKCLOUDCOVER': return talib.CDLDARKCLOUDCOVER(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), penetration=0) if name == 'CDLDOJI': return talib.CDLDOJI(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLDOJISTAR': return talib.CDLDOJISTAR(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLDRAGONFLYDOJI': return talib.CDLDRAGONFLYDOJI(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLENGULFING': return talib.CDLENGULFING(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLEVENINGDOJISTAR': return talib.CDLEVENINGDOJISTAR(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), penetration=0) if name == 'CDLEVENINGSTAR': return talib.CDLEVENINGSTAR(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), penetration=0) if name == 'CDLGAPSIDESIDEWHITE': return talib.CDLGAPSIDESIDEWHITE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLGRAVESTONEDOJI': return talib.CDLGRAVESTONEDOJI(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHAMMER': return talib.CDLHAMMER(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHANGINGMAN': return talib.CDLHANGINGMAN(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHARAMI': return talib.CDLHARAMI(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHARAMICROSS': return talib.CDLHARAMICROSS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHIGHWAVE': return talib.CDLHIGHWAVE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHIKKAKE': return talib.CDLHIKKAKE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHIKKAKEMOD': return talib.CDLHIKKAKEMOD(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLHOMINGPIGEON': return talib.CDLHOMINGPIGEON(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLIDENTICAL3CROWS': return talib.CDLIDENTICAL3CROWS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLINNECK': return talib.CDLINNECK(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLINVERTEDHAMMER': return talib.CDLINVERTEDHAMMER(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLKICKING': return talib.CDLKICKING(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLKICKINGBYLENGTH': return talib.CDLKICKINGBYLENGTH(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLLADDERBOTTOM': return talib.CDLLADDERBOTTOM(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLLONGLEGGEDDOJI': return talib.CDLLONGLEGGEDDOJI(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLLONGLINE': return talib.CDLLONGLINE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLMARUBOZU': return talib.CDLMARUBOZU(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLMATCHINGLOW': return talib.CDLMATCHINGLOW(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLMATHOLD': return talib.CDLMATHOLD(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLMORNINGDOJISTAR': return talib.CDLMORNINGDOJISTAR(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), penetration=0) if name == 'CDLMORNINGSTAR': return talib.CDLMORNINGSTAR(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), penetration=0) if name == 'CDLONNECK': return talib.CDLONNECK(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLPIERCING': return talib.CDLPIERCING(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLRICKSHAWMAN': return talib.CDLRICKSHAWMAN(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLRISEFALL3METHODS': return talib.CDLRISEFALL3METHODS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLSEPARATINGLINES': return talib.CDLSEPARATINGLINES(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLSHOOTINGSTAR': return talib.CDLSHOOTINGSTAR(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLSHORTLINE': return talib.CDLSHORTLINE(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLSPINNINGTOP': return talib.CDLSPINNINGTOP(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLSTALLEDPATTERN': return talib.CDLSTALLEDPATTERN(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLSTICKSANDWICH': return talib.CDLSTICKSANDWICH(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLTAKURI': return talib.CDLTAKURI(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLTASUKIGAP': return talib.CDLTASUKIGAP(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLTHRUSTING': return talib.CDLTHRUSTING(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLTRISTAR': return talib.CDLTRISTAR(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLUNIQUE3RIVER': return talib.CDLUNIQUE3RIVER(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLUPSIDEGAP2CROWS': return talib.CDLUPSIDEGAP2CROWS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CDLXSIDEGAP3METHODS': return talib.CDLXSIDEGAP3METHODS(np.array(price_o), np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'CMO': return talib.CMO(np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'CORREL': return talib.CORREL(np.array(price_h), np.asarray(price_l, dtype='float'), timeperiod=30) if name == 'DEMA': return talib.DEMA(np.asarray(price_c, dtype='float'), timeperiod=30) if name == 'DX': return talib.DX(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'EMA': return talib.EMA(np.asarray(price_c, dtype='float'), timeperiod=30) if name == 'HT_DCPERIOD': return talib.HT_DCPERIOD(np.asarray(price_c, dtype='float')) if name == 'HT_DCPHASE': return talib.HT_DCPHASE(np.asarray(price_c, dtype='float')) if name == 'HT_PHASOR': HT_PHASOR1, HT_PHASOR2 = talib.HT_PHASOR( np.asarray(price_c, dtype='float') ) # use HT_PHASOR1 for the indication of up and down return HT_PHASOR1 if name == 'HT_SINE': HT_SINE1, HT_SINE2 = talib.HT_SINE(np.asarray(price_c, dtype='float')) return HT_SINE1 if name == 'HT_TRENDLINE': return talib.HT_TRENDLINE(np.asarray(price_c, dtype='float')) if name == 'HT_TRENDMODE': return talib.HT_TRENDMODE(np.asarray(price_c, dtype='float')) if name == 'KAMA': return talib.KAMA(np.asarray(price_c, dtype='float'), timeperiod=30) if name == 'LINEARREG': return talib.LINEARREG(np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'LINEARREG_ANGLE': return talib.LINEARREG_ANGLE(np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'LINEARREG_INTERCEPT': return talib.LINEARREG_INTERCEPT(np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'LINEARREG_SLOPE': return talib.LINEARREG_SLOPE(np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'MA': return talib.MA(np.asarray(price_c, dtype='float'), timeperiod=30, matype=0) if name == 'MACD': MACD1, MACD2, MACD3 = talib.MACD(np.asarray(price_c, dtype='float'), fastperiod=12, slowperiod=26, signalperiod=9) return MACD1 if nam == 'MACDEXT': return talib.MACDEXT(np.asarray(price_c, dtype='float'), fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) if name == 'MACDFIX': MACDFIX1, MACDFIX2, MACDFIX3 = talib.MACDFIX(np.asarray(price_c, dtype='float'), signalperiod=9) return MACDFIX1 if name == 'MAMA': MAMA1, MAMA2 = talib.MAMA(np.asarray(price_c, dtype='float'), fastlimit=0, slowlimit=0) return MAMA1 if name == 'MEDPRICE': return talib.MEDPRICE(np.array(price_h), np.asarray(price_l, dtype='float')) if name == 'MINUS_DI': return talib.MINUS_DI(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'MINUS_DM': return talib.MINUS_DM(np.array(price_h), np.asarray(price_l, dtype='float'), timeperiod=14) if name == 'MOM': return talib.MOM(np.asarray(price_c, dtype='float'), timeperiod=10) if name == 'NATR': return talib.NATR(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'OBV': return talib.OBV(np.array(price_c), np.asarray(price_v, dtype='float')) if name == 'PLUS_DI': return talib.PLUS_DI(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'PLUS_DM': return talib.PLUS_DM(np.array(price_h), np.asarray(price_l, dtype='float'), timeperiod=14) if name == 'PPO': return talib.PPO(np.asarray(price_c, dtype='float'), fastperiod=12, slowperiod=26, matype=0) if name == 'ROC': return talib.ROC(np.asarray(price_c, dtype='float'), timeperiod=10) if name == 'ROCP': return talib.ROCP(np.asarray(price_c, dtype='float'), timeperiod=10) if name == 'ROCR100': return talib.ROCR100(np.asarray(price_c, dtype='float'), timeperiod=10) if name == 'RSI': return talib.RSI(np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'SAR': return talib.SAR(np.array(price_h), np.asarray(price_l, dtype='float'), acceleration=0, maximum=0) if name == 'SAREXT': return talib.SAREXT(np.array(price_h), np.asarray(price_l, dtype='float'), startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0) if name == 'SMA': return talib.SMA(np.asarray(price_c, dtype='float'), timeperiod=30) if name == 'STDDEV': return talib.STDDEV(np.asarray(price_c, dtype='float'), timeperiod=5, nbdev=1) if name == 'STOCH': STOCH1, STOCH2 = talib.STOCH(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) return STOCH1 if name == 'STOCHF': STOCHF1, STOCHF2 = talib.STOCHF(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), fastk_period=5, fastd_period=3, fastd_matype=0) return STOCHF1 if name == 'STOCHRSI': STOCHRSI1, STOCHRSI2 = talib.STOCHRSI(np.asarray(price_c, dtype='float'), timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) return STOCHRSI1 if name == 'T3': return talib.T3(np.asarray(price_c, dtype='float'), timeperiod=5, vfactor=0) if name == 'TEMA': return talib.TEMA(np.asarray(price_c, dtype='float'), timeperiod=30) if name == 'TRANGE': return talib.TRANGE(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'TRIMA': return talib.TRIMA(np.asarray(price_c, dtype='float'), timeperiod=30) if name == 'TRIX': return talib.TRIX(np.asarray(price_c, dtype='float'), timeperiod=30) if name == 'TSF': return talib.TSF(np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'TYPPRICE': return talib.TYPPRICE(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'ULTOSC': return talib.ULTOSC(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod1=7, timeperiod2=14, timeperiod3=28) if name == 'VAR': return talib.VAR(np.asarray(price_c, dtype='float'), timeperiod=5, nbdev=1) if name == 'WCLPRICE': return talib.WCLPRICE(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float')) if name == 'WILLR': return talib.WILLR(np.array(price_h), np.array(price_l), np.asarray(price_c, dtype='float'), timeperiod=14) if name == 'WMA': return talib.WMA(np.asarray(price_c, dtype='float'), timeperiod=30)