def __generate_technical_indicators(self, close_values, high_values, low_values, open_values, volume_values): features = pd.DataFrame() features['short_sma'] = talib.SMA(close_values, 5) features['long_sma'] = talib.SMA(close_values, 20) features['sma_diff'] = features.long_sma - features.short_sma features['stochf0'] = talib.STOCHF(high=high_values, low=low_values, close=close_values)[0] features['stochf1'] = talib.STOCHF(high=high_values, low=low_values, close=close_values)[1] features['rsi'] = talib.RSI(close_values, 20) features['ad'] = talib.AD(high=high_values, low=low_values, close=close_values, volume=volume_values) features['dema'] = talib.DEMA(close_values) features['ema'] = talib.EMA(close_values) features['ht_trendiline'] = talib.HT_TRENDLINE(close_values) features['kama'] = talib.KAMA(close_values) features['midpoint'] = talib.MIDPOINT(close_values) features['midprice'] = talib.MIDPRICE(high=high_values, low=low_values) features['sar'] = talib.SAR(high=high_values, low=low_values) features['sarext'] = talib.SAREXT(high=high_values, low=low_values) features['adx'] = talib.ADX(high=high_values, low=low_values, close=close_values) features['adxr'] = talib.ADXR(high=high_values, low=low_values, close=close_values) features['apo'] = talib.APO(close_values) features['aroon0'] = talib.AROON(high=high_values, low=low_values)[0] features['aroon1'] = talib.AROON(high=high_values, low=low_values)[1] features['aroonosc'] = talib.AROONOSC(high=high_values, low=low_values) features['bop'] = talib.BOP(open=open_values, high=high_values, low=low_values, close=close_values) features['cmo'] = talib.CMO(close_values) features['dx'] = talib.DX(high=high_values, low=low_values, close=close_values) features['macdfix0'] = talib.MACDFIX(close_values)[0] features['macdfix1'] = talib.MACDFIX(close_values)[1] features['macdfix2'] = talib.MACDFIX(close_values)[2] features['mfi'] = talib.MFI(high=high_values, low=low_values, close=close_values, volume=volume_values) features['minus_di'] = talib.MINUS_DI(high=high_values, low=low_values, close=close_values) features['minus_dm'] = talib.MINUS_DM(high=high_values, low=low_values) features['mom'] = talib.MOM(close_values) features['plus_di'] = talib.PLUS_DI(high=high_values, low=low_values, close=close_values) features['plus_dm'] = talib.PLUS_DM(high=high_values, low=low_values) features['ppo'] = talib.PPO(close_values) features['roc'] = talib.ROC(close_values) features['stochf0'] = talib.STOCHF(high=high_values, low=low_values, close=close_values)[0] features['stochf1'] = talib.STOCHF(high=high_values, low=low_values, close=close_values)[1] features['stochrsi0'] = talib.STOCHRSI(close_values)[0] features['stochrsi1'] = talib.STOCHRSI(close_values)[1] # data_set['trix'] = talib.TRIX(close_values) features['ultosc'] = talib.ULTOSC(high=high_values, low=low_values, close=close_values) features['willr'] = talib.WILLR(high=high_values, low=low_values, close=close_values) features['adosc'] = talib.ADOSC(high=high_values, low=low_values, close=close_values, volume=volume_values) features['obv'] = talib.OBV(close_values, volume_values) features['ht_dcperiod'] = talib.HT_DCPERIOD(close_values) features['ht_dcphase'] = talib.HT_DCPHASE(close_values) features['ht_phasor0'] = talib.HT_PHASOR(close_values)[0] features['ht_phasor1'] = talib.HT_PHASOR(close_values)[1] features['ht_sine0'] = talib.HT_SINE(close_values)[0] features['ht_sine1'] = talib.HT_SINE(close_values)[1] features['ht_trendmode'] = talib.HT_TRENDMODE(close_values) features['atr'] = talib.ATR(high=high_values, low=low_values, close=close_values) features['trange'] = talib.TRANGE(high=high_values, low=low_values, close=close_values) features['CDL2CROWS'] = talib.CDL2CROWS(open=open_values, high=high_values, low=low_values, close=close_values) features['CDL3BLACKCROWS'] = talib.CDL3BLACKCROWS(open=open_values, high=high_values, low=low_values, close=close_values) features['CDL3INSIDE'] = talib.CDL3INSIDE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDL3LINESTRIKE'] = talib.CDL3LINESTRIKE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDL3OUTSIDE'] = talib.CDL3OUTSIDE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDL3STARSINSOUTH'] = talib.CDL3STARSINSOUTH(open=open_values, high=high_values, low=low_values, close=close_values) features['CDL3WHITESOLDIERS'] = talib.CDL3WHITESOLDIERS(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLABANDONEDBABY'] = talib.CDLABANDONEDBABY(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLADVANCEBLOCK'] = talib.CDLADVANCEBLOCK(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLBELTHOLD'] = talib.CDLBELTHOLD(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLBREAKAWAY'] = talib.CDLBREAKAWAY(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLCLOSINGMARUBOZU'] = talib.CDLCLOSINGMARUBOZU(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLCONCEALBABYSWALL'] = talib.CDLCONCEALBABYSWALL(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLCOUNTERATTACK'] = talib.CDLCOUNTERATTACK(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLDARKCLOUDCOVER'] = talib.CDLDARKCLOUDCOVER(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLDOJI'] = talib.CDLDOJI(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLDOJISTAR'] = talib.CDLDOJISTAR(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLDRAGONFLYDOJI'] = talib.CDLDRAGONFLYDOJI(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLENGULFING'] = talib.CDLENGULFING(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLEVENINGDOJISTAR'] = talib.CDLEVENINGDOJISTAR(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLEVENINGSTAR'] = talib.CDLEVENINGSTAR(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLGAPSIDESIDEWHITE'] = talib.CDLGAPSIDESIDEWHITE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLGRAVESTONEDOJI'] = talib.CDLGRAVESTONEDOJI(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHAMMER'] = talib.CDLHAMMER(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHANGINGMAN'] = talib.CDLHANGINGMAN(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHARAMI'] = talib.CDLHARAMI(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHARAMICROSS'] = talib.CDLHARAMICROSS(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHIGHWAVE'] = talib.CDLHIGHWAVE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHIKKAKE'] = talib.CDLHIKKAKE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHIKKAKEMOD'] = talib.CDLHIKKAKEMOD(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLHOMINGPIGEON'] = talib.CDLHOMINGPIGEON(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLIDENTICAL3CROWS'] = talib.CDLIDENTICAL3CROWS(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLINNECK'] = talib.CDLINNECK(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLINVERTEDHAMMER'] = talib.CDLINVERTEDHAMMER(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLKICKING'] = talib.CDLKICKING(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLKICKINGBYLENGTH'] = talib.CDLKICKINGBYLENGTH(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLLADDERBOTTOM'] = talib.CDLLADDERBOTTOM(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLLONGLEGGEDDOJI'] = talib.CDLLONGLEGGEDDOJI(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLLONGLINE'] = talib.CDLLONGLINE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLMARUBOZU'] = talib.CDLMARUBOZU(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLMATCHINGLOW'] = talib.CDLMATCHINGLOW(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLMATHOLD'] = talib.CDLMATHOLD(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLMORNINGDOJISTAR'] = talib.CDLMORNINGDOJISTAR(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLMORNINGSTAR'] = talib.CDLMORNINGSTAR(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLONNECK'] = talib.CDLONNECK(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLPIERCING'] = talib.CDLPIERCING(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLRICKSHAWMAN'] = talib.CDLRICKSHAWMAN(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLRISEFALL3METHODS'] = talib.CDLRISEFALL3METHODS(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLSEPARATINGLINES'] = talib.CDLSEPARATINGLINES(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLSHOOTINGSTAR'] = talib.CDLSHOOTINGSTAR(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLSHORTLINE'] = talib.CDLSHORTLINE(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLSPINNINGTOP'] = talib.CDLSPINNINGTOP(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLSTALLEDPATTERN'] = talib.CDLSTALLEDPATTERN(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLSTICKSANDWICH'] = talib.CDLSTICKSANDWICH(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLTAKURI'] = talib.CDLTAKURI(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLTASUKIGAP'] = talib.CDLTASUKIGAP(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLTHRUSTING'] = talib.CDLTHRUSTING(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLTRISTAR'] = talib.CDLTRISTAR(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLUNIQUE3RIVER'] = talib.CDLUNIQUE3RIVER(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLUPSIDEGAP2CROWS'] = talib.CDLUPSIDEGAP2CROWS(open=open_values, high=high_values, low=low_values, close=close_values) features['CDLXSIDEGAP3METHODS'] = talib.CDLXSIDEGAP3METHODS(open=open_values, high=high_values, low=low_values, close=close_values) return features
def main(): # read csv file and transform it to datafeed (df): df = pd.read_csv(current_dir + "/" + base_dir + "/" + in_dir + "/" + in_dir + '_' + stock_symbol + '.csv') # set numpy datafeed from df: df_numpy = { 'Date': np.array(df['date']), 'Open': np.array(df['open'], dtype='float'), 'High': np.array(df['high'], dtype='float'), 'Low': np.array(df['low'], dtype='float'), 'Close': np.array(df['close'], dtype='float'), 'Volume': np.array(df['volume'], dtype='float') } date = df_numpy['Date'] openp = df_numpy['Open'] high = df_numpy['High'] low = df_numpy['Low'] close = df_numpy['Close'] volume = df_numpy['Volume'] ######################################### ##### Momentum Indicator Functions ##### ######################################### #ADX - Average Directional Movement Index adx = ta.ADX(high, low, close, timeperiod=14) #ADXR - Average Directional Movement Index Rating adxr = ta.ADXR(high, low, close, timeperiod=14) #APO - Absolute Price Oscillator apo = ta.APO(close, fastperiod=12, slowperiod=26, matype=0) #AROON - Aroon aroondown, aroonup = ta.AROON(high, low, timeperiod=14) #AROONOSC - Aroon Oscillator aroonosc = ta.AROONOSC(high, low, timeperiod=14) #BOP - Balance Of Power bop = ta.BOP(openp, high, low, close) #CCI - Commodity Channel Index cci = ta.CCI(high, low, close, timeperiod=14) #CMO - Chande Momentum Oscillator cmo = ta.CMO(close, timeperiod=14) #DX - Directional Movement Index dx = ta.DX(high, low, close, timeperiod=14) #MACD - Moving Average Convergence/Divergence macd, macdsignal, macdhist = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) #MACDEXT - MACD with controllable MA type macd, macdsignal, macdhist = ta.MACDEXT(close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) #MACDFIX - Moving Average Convergence/Divergence Fix 12/26 macd, macdsignal, macdhist = ta.MACDFIX(close, signalperiod=9) #MFI - Money Flow Index mfi = ta.MFI(high, low, close, volume, timeperiod=14) #MINUS_DI - Minus Directional Indicator minus_di = ta.MINUS_DI(high, low, close, timeperiod=14) #MINUS_DM - Minus Directional Movement minus_dm = ta.MINUS_DM(high, low, timeperiod=14) #MOM - Momentum mom = ta.MOM(close, timeperiod=10) #PLUS_DI - Plus Directional Indicator plus_di = ta.PLUS_DI(high, low, close, timeperiod=14) #PLUS_DM - Plus Directional Movement plus_dm = ta.PLUS_DM(high, low, timeperiod=14) #PPO - Percentage Price Oscillator ppo = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0) #ROC - Rate of change : ((price/prevPrice)-1)*100 roc = ta.ROC(close, timeperiod=10) #ROCP - Rate of change Percentage: (price-prevPrice)/prevPrice rocp = ta.ROCP(close, timeperiod=10) #ROCR - Rate of change ratio: (price/prevPrice) rocr = ta.ROCR(close, timeperiod=10) #ROCR100 - Rate of change ratio 100 scale: (price/prevPrice)*100 rocr100 = ta.ROCR100(close, timeperiod=10) #RSI - Relative Strength Index rsi = ta.RSI(close, timeperiod=14) #STOCH - Stochastic slowk, slowd = ta.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) #STOCHF - Stochastic Fast stochf_fastk, stochf_fastd = ta.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) #STOCHRSI - Stochastic Relative Strength Index stochrsi_fastk, stochrsi_fastd = ta.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) #TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA trix = ta.TRIX(close, timeperiod=30) #ULTOSC - Ultimate Oscillator ultosc = ta.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) #WILLR - Williams' %R willr = ta.WILLR(high, low, close, timeperiod=14) df_save = pd.DataFrame( data={ 'Date': np.array(df['date']), 'adx': adx, 'adxr': adxr, 'apo': apo, 'aroondown': aroondown, 'aroonup': aroonup, 'aroonosc': aroonosc, 'bop': bop, 'cci': cci, 'cmo': cmo, 'dx': dx, 'macd': macd, 'macdsignal': macdsignal, 'macdhist': macdhist, 'mfi': mfi, 'minus_di': minus_di, 'minus_dm': minus_dm, 'mom': mom, 'plus_di': plus_di, 'plus_dm': plus_dm, 'ppo': ppo, 'roc': roc, 'rocp': rocp, 'rocr': rocr, 'rocr100': rocr100, 'rsi': rsi, 'slowk': slowk, 'slowd': slowd, 'stochf_fastk': stochf_fastk, 'stochf_fastd': stochf_fastd, 'stochrsi_fastk': stochrsi_fastk, 'stochrsi_fastd': stochrsi_fastd, 'trix': trix, 'ultosc': ultosc, 'willr': willr }) df_save.to_csv(current_dir + "/" + base_dir + "/" + out_dir + '/' + stock_symbol + "/" + out_dir + '_ta_momentum_indicator_' + stock_symbol + '.csv', index=False)
# 调用talib计算指数移动平均线的值 close = df['Close'].values open = df['Open'].values high = df['High'].values low = df['Low'].values volume = df['Volume'].values df['EMA12'] = talib.EMA(np.array(close), timeperiod=6) df['EMA26'] = talib.EMA(np.array(close), timeperiod=12) df['SAR'] = talib.SAR(high, low, acceleration=0, maximum=0) df['SMA'] = talib.SMA(close, timeperiod=30) df['MACD'], df['MACDsignal'], df['MACDhist'] = talib.MACD(np.array(close), fastperiod=6, slowperiod=12, signalperiod=9) df['RSI'] = talib.RSI(np.array(close), timeperiod=12) #RSI的天数一般是6、12、24 df['MOM'] = talib.MOM(np.array(close), timeperiod=5) df['CDL2CROWS'] = talib.CDL2CROWS(open, high, low, close) df['CDL3BLACKCROWS'] = talib.CDL3BLACKCROWS(open, high, low, close) df['CDL3INSIDE'] = talib.CDL3INSIDE(open, high, low, close) df['CDL3LINESTRIKE'] = talib.CDL3LINESTRIKE(open, high, low, close) df['CDLBREAKAWAY'] = talib.CDLBREAKAWAY(open, high, low, close) df['CDLADVANCEBLOCK'] = talib.CDLADVANCEBLOCK(open, high, low, close) df['BETA'] = talib.BETA(high, low, timeperiod=5) df['CORREL'] = talib.CORREL(high, low, timeperiod=30) df['AD'] = talib.AD(high, low, close, volume) df['TSF'] = talib.TSF(close, timeperiod=14) df['CDLDARKCLOUDCOVER'] = talib.CDLDARKCLOUDCOVER(open, high, low, close, penetration=0)
def attribute_gen(data): ''' generate attributes from cleansed data of local database :param data: is tick level np.array with 28 columns :return: X, y, Xpred are dataframe with datetime index ''' ####################################################################################### # indexed by datetime data_indexed = pd.DataFrame(data) data_indexed = data_indexed.set_index( 2) # the original 28 column is removed automatically data_indexed.drop([0, 1], axis=1, inplace=True) # 8503 # type(data_indexed[7][0]) # it is int, we need float instead data_indexed[7] = data_indexed[7].astype(float) ####################################################################################### ''' # five minutes first & last & mean # 1 data_indexed_5m_first = data_indexed.resample('5T').first() data_indexed_5m_first = data_indexed_5m_first.between_time('9:30', '15:00') inde = data_indexed_5m_first[data_indexed_5m_first[3].isna()].index data_indexed_5m_first.drop(inde, inplace=True) # data_indexed_5m_first = data_indexed_5m_first.fillna(method='backfill') data_indexed_5m_first.drop(data_indexed_5m_first.tail(1).index, inplace=True) data_indexed_5m_first.drop(data_indexed_5m_first.tail(1).index, inplace=True) # 2 data_indexed_5m_last = data_indexed.resample('5T').last() # 354 data_indexed_5m_last = data_indexed_5m_last.between_time('9:30', '15:00') # 133 inde = data_indexed_5m_last[data_indexed_5m_last[3].isna()].index data_indexed_5m_last.drop(inde, inplace=True) # 96 # data_indexed_5m_last = data_indexed_5m_last.fillna(method='backfill') # cut the last item data_indexed_5m_last.drop(data_indexed_5m_last.tail(1).index, inplace=True) # 3 data_indexed_5m_last = data_indexed_5m_last.between_time('9:30', '15:00') # 133 inde = data_indexed_5m_last[data_indexed_5m_last[3].isna()].index data_indexed_5m_last.drop(inde, inplace=True) # 96 # data_indexed_5m_last = data_indexed_5m_last.fillna(method='backfill') # cut the last item data_indexed_5m_last.drop(data_indexed_5m_last.tail(1).index, inplace=True) # 4 data_indexed_5m_change = data_indexed_5m_last - data_indexed_5m_first ''' ######################################################################################## # five minutes current price last & change & max & min data_cp = pd.DataFrame(data[:, [2, 3]]) # data is np.array() data_cp = data_cp.set_index(0) data_cplast = data_cp.resample('5T').last() # 354 inde = data_cplast[data_cplast[1].isna()].index data_cplast.drop(inde, inplace=True) # 96 # data_cpfirst = data_cp.resample('5T').first() # 354 # inde = data_cpfirst[data_cpfirst[1].isna()].index # data_cpfirst.drop(inde, inplace=True) # 96 data_cprice_change = data_cplast.diff() data_cprice_change = data_cprice_change.fillna(method='bfill') # data_cpratio = data_cplast/data_cpfirst # move one step forward # data_cprice_change = data_cprice_change[1][data_cprice_change.index[1:len(data_cprice_change)]] # cut the last item # data_cplast = data_cplast[1][data_cplast.index[0:len(data_cplast) - 1]] data_cpmax = data_cp.resample('5T').max() # 354 indexname = data_cpmax[data_cpmax[1].isna()].index data_cpmax.drop(indexname, inplace=True) # 96 # ?????? doesn't work: data_cpmax = data_cpmax.resample('5T').agg(lambda x: max(x[i]) for i in range(len(x))) # 354 # cut the last item # data_cpmax = data_cpmax[1][data_cpmax.index[0:len(data_cpmax)-1]] data_cpmin = data_cp.resample('5T').min() # 354 ind = data_cpmin[data_cpmin[1].isna()].index data_cpmin.drop(ind, inplace=True) # 96 # cut the last item # data_cpmin = data_cpmin[1][data_cpmin.index[0:len(data_cpmin)-1]] data_cp = pd.DataFrame() data_cp['max'] = data_cpmax[1] data_cp['min'] = data_cpmin[1] data_cp['last'] = data_cplast[1] data_cp['change'] = data_cprice_change[1] # data_cp['ratio'] = data_cpratio[1] ratio can not increase forecast accuracy ###################################################################################################### # flag data data_flag = pd.DataFrame(data[:, [2, 7]]) data_flag = data_flag.set_index(0) data_flag_plus = data_flag data_flag_minus = data_flag # data_flag_plus[1][data_flag_plus[1] == -1] = 0 # data_flag_minus[1][data_flag_minus[1] == 1] = 0 # data_flag_minus[1][data_flag_minus[1] == -1] = 1 # flagtry = list(map(lambda x: x+1 if x == -1 else x, data_flag_plus[1])) data_flag_plus = data_flag_plus.applymap(lambda x: x + 1 if x == -1 else x) data_flag_minus = data_flag_minus.applymap(lambda x: x - 1 if x == 1 else x + 2) data_flag_plsum = data_flag_plus.resample('5T').sum() # 354 data_flag_misum = data_flag_minus.resample('5T').sum() # 354 # index = data_flag_plsum[data_flag_plsum[1] == 0].index data_flag_plsum.drop(inde, inplace=True) # 96 data_flag_misum.drop(inde, inplace=True) # 96 data_flag_ratio = data_flag_plsum / data_flag_misum # 96 flag = pd.DataFrame() flag['psum'] = data_flag_plsum[1] # flag['msum'] = data_flag_misum[1] flag['ratio'] = data_flag_ratio[1] # cut the last item # flag.drop(flag.tail(1).index, inplace=True) #################################################################################### ''' data_flag = pd.Series(data_flag[1]) # the index is attached automatically data_flag = data_flag.resample('5T').agg(lambda x: x[-1]/x[1] - 1) index = data_indexed_5m_first[data_flag[3].isna()].index data_flag.drop(index, inplace=True) data_flag = data_flag.fillna(method='backfill') ''' # standardize money (column five) data_money = pd.DataFrame(data[:, [2, 5]]) # data is np.array() data_money = data_money.set_index(0) data_money = pd.Series(data_money[1]) data_money_5m = data_money.resample('5T').sum() # 354 ind = data_money_5m[data_money_5m == 0.0].index data_money_5m.drop(ind, inplace=True) # 96 data_money_5m_d = data_money_5m.diff() data_money_5m_d = data_money_5m_d.fillna(method='backfill') # cut the last item # data_money_5m_d.drop(data_money_5m_d.tail(1).index, inplace=True) data_money = pd.DataFrame() data_money['money'] = data_money_5m data_money['diff'] = data_money_5m_d ######################################################################################################### # MACD macd = data_indexed[3] # 8503 macd = pd.DataFrame(macd) macd_12_ema = macd.ewm(span=12, adjust=False).mean() macd_26_ema = macd.ewm(span=26, adjust=False).mean() macd_real = macd_12_ema - macd_26_ema macd_reference = macd_real.ewm(span=9, adjust=False).mean() # transform to 5 minutes' frequency mmaa = pd.DataFrame() mmaa['12'] = macd_12_ema[3] mmaa['26'] = macd_26_ema[3] mmaa['real'] = macd_real[3] mmaa['refer'] = macd_reference[3] mmaa_5m = mmaa.resample('5T') mmaa_5m_mean = mmaa_5m.mean() # 354 indd = mmaa_5m_mean[mmaa_5m_mean['12'].isna()].index mmaa_5m_mean.drop(indd, inplace=True) # 96 # cut the last item # mmaa_5m_mean.drop(mmaa_5m_mean.tail(1).index, inplace=True) # 95 # macd, macdsignal, macdhist = talib.MACD(close, fastperiod=12, # slowperiod=26, signalperiod=9) #################################################################################### high = data_cpmax[1] low = data_cpmin[1] close = data_cplast[1] data_vol = pd.DataFrame(data[:, [2, 6]]) # data is np.array() data_vol = data_vol.set_index(0) data_vol = pd.Series(data_vol[1]) data_vol_5m = data_vol.resample('5T').sum() # 354 ind = data_vol_5m[data_vol_5m == 0.0].index data_vol_5m.drop(ind, inplace=True) # 96 volume = data_vol_5m #################################################################################### # sma of current price data_sma_20 = talib.SMA(np.asarray(close), 20) data_sma_20 = pd.DataFrame(data_sma_20) data_sma_20 = data_sma_20.fillna(method='backfill') data_sma_10 = talib.SMA(np.asarray(close), 10) data_sma_10 = pd.DataFrame(data_sma_10) data_sma_10 = data_sma_10.fillna(method='backfill') data_sma_5 = talib.SMA(np.asarray(close), 5) data_sma_5 = pd.DataFrame(data_sma_5) data_sma_5 = data_sma_5.fillna(method='backfill') data_sma = pd.DataFrame() data_sma['5'] = data_sma_5[0] data_sma['10'] = data_sma_10[0] data_sma['20'] = data_sma_20[0] data_sma['0'] = volume.index data_sma = data_sma.set_index('0') #################################################################################### # Average Directional Movement Index # real = talib.ADX(high, low, close, timeperiod=14) direct_move = talib.ADX(high, low, close, timeperiod=14) direct_move = direct_move.fillna(method='backfill') ######################################################################################## # Absolute Price Oscillator oscillator = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) oscillator = oscillator.fillna(method='backfill') ######################################################################################## # macd fix macdfix, macdsignal, macdhist = talib.MACDFIX(close, signalperiod=9) # nan 40 macdfix.fillna(method='backfill') ######################################################################################### # money flow money_flow = talib.MFI(high, low, close, volume, timeperiod=14) money_flow.fillna(method='backfill') ######################################################################################### # minus directional indicator minus_direct = talib.MINUS_DI(high, low, close, timeperiod=14) minus_direct.fillna(method='backfill') ######################################################################################### # momentum of the close prices, with a time period of 5: mom = talib.MOM(close, timeperiod=10) mom = mom.fillna(method='backfill') ######################################################################################### # PLUS_DI - Plus Directional Indicator plus_direct = talib.PLUS_DI(high, low, close, timeperiod=14) plus_direct = plus_direct.fillna(method='backfill') ######################################################################################### # PLUS_DM - Plus Directional Movement plus_direct_move = talib.PLUS_DM(high, low, timeperiod=14) plus_direct_move = plus_direct_move.fillna(method='backfill') ######################################################################################### # PPO - Percentage Price Oscillator percent_oscillator = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) percent_oscillator = percent_oscillator.fillna(method='backfill') ######################################################################################### # ROCP - Rate of change Percentage: (price-prevPrice)/prevPrice rate_of_change = talib.ROCP(close, timeperiod=10) rate_of_change = rate_of_change.fillna(method='backfill') ######################################################################################### # Rate of change ratio rate_ratio = talib.ROCR(close, timeperiod=10) rate_ratio = rate_ratio.fillna(method='backfill') ######################################################################################### # RSI - Relative Strength Index strength = talib.RSI(close, timeperiod=14) strength = strength.fillna(method='backfill') ######################################################################################### # STOCH - Stochastic slowk, slowd = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) slowk = slowk.fillna(method='backfill') slowd = slowd.fillna(method='backfill') stochastic = pd.DataFrame() stochastic['slowk'] = slowk stochastic['slowd'] = slowd ######################################################################################### # STOCHF - Stochastic Fast fastk, fastd = talib.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) fastk = fastk.fillna(method='backfill') fastd = fastd.fillna(method='backfill') fast = pd.DataFrame() fast['fastk'] = fastk fast['fastd'] = fastd ######################################################################################### # STOCHRSI - Stochastic Relative Strength Index fastk, fastd = talib.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) fastk = fastk.fillna(method='backfill') fastd = fastd.fillna(method='backfill') relative_strength = pd.DataFrame() relative_strength['fastk'] = fastk relative_strength['fastd'] = fastd ######################################################################################### # ULTOSC - Ultimate Oscillator ulti_osci = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) ulti_osci = ulti_osci.fillna(method='backfill') ######################################################################################### # WILLR - Williams' %R willer = talib.WILLR(high, low, close, timeperiod=14) willer = willer.fillna(method='backfill') ######################################################################################### # buy sell info factors buy_sell = data[:, 8:28] buy_sell = pd.DataFrame(buy_sell, index=data[:, 2]) # col 8-12 buy price, col 13-17 sell price, # col 18-22 buy quant, col 23-27 sell quant buy_sell_5m = buy_sell.resample('5T').last() # 354 inde = buy_sell_5m[buy_sell_5m[1].isna()].index buy_sell_5m.drop(inde, inplace=True) # 96 ######################################################################################### # bollinger bands, with triple exponential moving average: from talib import MA_Type upper, middle, lower = talib.BBANDS(close, matype=MA_Type.T3) ######################################################################################### # generate label y data_cp2 = pd.DataFrame(data[:, [2, 3]]) # data is np.array() data_cp2 = data_cp2.set_index(0) data_cplast = data_cp2.resample('5T').last() # 354 inddd = data_cplast[data_cplast[1].isna()].index data_cplast.drop(inddd, inplace=True) # 96 pchange = data_cplast.diff() # 96 pchange = pchange.fillna(method='backfill') # cut the last item # data_cplast = data_cplast[1][data_cplast.index[0:len(data_cplast) - 1]] func = lambda x: np.sign(x) if x != 0 else np.sign(x + 1) pchange['sign'] = list(map(func, pchange[1])) # pchange.drop(pchange.head(1).index, inplace=True) # 95 #################################################################################### # generate train and label data X = pd.concat([ mmaa_5m_mean, data_sma, direct_move, oscillator, macdfix, money_flow, minus_direct, mom, plus_direct, plus_direct_move, percent_oscillator, rate_of_change, rate_ratio, strength, stochastic, fast, relative_strength, ulti_osci, willer, data_money, flag, data_cp ], axis=1) y = pchange['sign'] price_change = data_cprice_change return X, y, price_change
def get_mom(df, nday): temp_serise = talib.MOM(df['close'], timeperiod=nday) temp_serise.dropna(inplace=True) mom_serise = temp_serise.reset_index(drop=True) return mom_serise
def TA(S, s, c): S = S.fillna(0) ope = numpy.asfarray(S.Open) high = numpy.asfarray(S.High) low = numpy.asfarray(S.Low) close = numpy.asfarray(S.Close) volume = numpy.asfarray(S.Volume) ##ROI calculation ROI = [(close[i + 1] - close[i]) / close[i] for i in range(len(close) - 1)] ROI.append(0) #add zero value for last day d = pandas.DataFrame(ROI, index=S.index, columns=['ROI']) d.to_csv("C:\\Users\\...\\Documents\\Data_TA\\{0}\\ROI_{1}.csv".format( s, c)) ##Baselines try: bah.append((S.Close['2013-04-30'] - S.Close['2002-05-01']) / S.Close['2002-05-01']) sah.append((-S.Close['2013-04-30'] + S.Close['2002-05-01']) / S.Close['2002-05-01']) except: bah.append((S.Close['2013-04-30'] - S.Close['2002-05-02']) / S.Close['2002-05-02']) sah.append((-S.Close['2013-04-30'] + S.Close['2002-05-02']) / S.Close['2002-05-02']) rp.append( numpy.dot(numpy.random.uniform(-1, 1, len(S)), numpy.asfarray(ROI))) ##talib application #overlap BBANDS = ta.BBANDS(close) DEMA = ta.DEMA(close) EMA = ta.EMA(close) HT_TRENDLINE = ta.HT_TRENDLINE(close) KAMA = ta.KAMA(close) MA = ta.MA(close) MAMA = ta.MAMA(close) MIDPOINT = ta.MIDPOINT(close) MIDPRICE = ta.MIDPRICE(high, low) SAR = ta.SAR(high, low) SAREXT = ta.SAREXT(high, low) SMA = ta.SMA(close) T3 = ta.T3(close) TEMA = ta.TEMA(close) TRIMA = ta.TRIMA(close) WMA = ta.WMA(close) #momentum ADX = ta.ADX(high, low, close) ADXR = ta.ADXR(high, low, close) APO = ta.APO(close) AROON = ta.AROON(high, low) AROONOSC = ta.AROONOSC(high, low) BOP = ta.BOP(ope, high, low, close) CCI = ta.CCI(high, low, close) CMO = ta.CMO(close) DX = ta.DX(high, low, close) MACD = ta.MACD(close) MACDEXT = ta.MACDEXT(close) MFI = ta.MFI(high, low, close, volume) MINUS_DI = ta.MINUS_DI(high, low, close) MINUS_DM = ta.MINUS_DM(high, low) MOM = ta.MOM(close) PLUS_DI = ta.PLUS_DI(high, low, close) PLUS_DM = ta.PLUS_DM(high, low) PPO = ta.PPO(close) ROC = ta.ROC(close) ROCP = ta.ROCP(close) ROCR = ta.ROCR(close) RSI = ta.RSI(close) STOCH = ta.STOCH(high, low, close) STOCHF = ta.STOCHF(high, low, close) STOCHRSI = ta.STOCHRSI(close) TRIX = ta.TRIX(close) ULTOSC = ta.ULTOSC(high, low, close) WILLR = ta.WILLR(high, low, close) #volume AD = ta.AD(high, low, close, volume) ADOSC = ta.ADOSC(high, low, close, volume) OBV = ta.OBV(close, volume) #cycle HT_DCPERIOD = ta.HT_DCPERIOD(close) HT_DCPHASE = ta.HT_DCPHASE(close) HT_PHASOR = ta.HT_PHASOR(close) HT_SINE = ta.HT_SINE(close) HT_TRENDMODE = ta.HT_TRENDMODE(close) #price AVGPRICE = ta.AVGPRICE(ope, high, low, close) MEDPRICE = ta.MEDPRICE(high, low) TYPPRICE = ta.TYPPRICE(high, low, close) WCLPRICE = ta.WCLPRICE(high, low, close) #volatility ATR = ta.ATR(high, low, close) NATR = ta.NATR(high, low, close) TRANGE = ta.TRANGE(high, low, close) #pattern CDL2CROWS = ta.CDL2CROWS(ope, high, low, close) CDL3BLACKCROWS = ta.CDL3BLACKCROWS(ope, high, low, close) CDL3INSIDE = ta.CDL3INSIDE(ope, high, low, close) CDL3LINESTRIKE = ta.CDL3LINESTRIKE(ope, high, low, close) CDL3OUTSIDE = ta.CDL3OUTSIDE(ope, high, low, close) CDL3STARSINSOUTH = ta.CDL3STARSINSOUTH(ope, high, low, close) CDL3WHITESOLDIERS = ta.CDL3WHITESOLDIERS(ope, high, low, close) CDLABANDONEDBABY = ta.CDLABANDONEDBABY(ope, high, low, close) CDLADVANCEBLOCK = ta.CDLADVANCEBLOCK(ope, high, low, close) CDLBELTHOLD = ta.CDLBELTHOLD(ope, high, low, close) CDLBREAKAWAY = ta.CDLBREAKAWAY(ope, high, low, close) CDLCLOSINGMARUBOZU = ta.CDLCLOSINGMARUBOZU(ope, high, low, close) CDLCONCEALBABYSWALL = ta.CDLCONCEALBABYSWALL(ope, high, low, close) CDLCOUNTERATTACK = ta.CDLCOUNTERATTACK(ope, high, low, close) CDLDARKCLOUDCOVER = ta.CDLDARKCLOUDCOVER(ope, high, low, close) CDLDOJI = ta.CDLDOJI(ope, high, low, close) CDLDOJISTAR = ta.CDLDOJISTAR(ope, high, low, close) CDLDRAGONFLYDOJI = ta.CDLDRAGONFLYDOJI(ope, high, low, close) CDLENGULFING = ta.CDLENGULFING(ope, high, low, close) CDLEVENINGDOJISTAR = ta.CDLEVENINGDOJISTAR(ope, high, low, close) CDLEVENINGSTAR = ta.CDLEVENINGSTAR(ope, high, low, close) CDLGAPSIDESIDEWHITE = ta.CDLGAPSIDESIDEWHITE(ope, high, low, close) CDLGRAVESTONEDOJI = ta.CDLGRAVESTONEDOJI(ope, high, low, close) CDLHAMMER = ta.CDLHAMMER(ope, high, low, close) CDLHANGINGMAN = ta.CDLHANGINGMAN(ope, high, low, close) CDLHARAMI = ta.CDLHARAMI(ope, high, low, close) CDLHARAMICROSS = ta.CDLHARAMICROSS(ope, high, low, close) CDLHIGHWAVE = ta.CDLHIGHWAVE(ope, high, low, close) CDLHIKKAKE = ta.CDLHIKKAKE(ope, high, low, close) CDLHIKKAKEMOD = ta.CDLHIKKAKEMOD(ope, high, low, close) CDLHOMINGPIGEON = ta.CDLHOMINGPIGEON(ope, high, low, close) CDLIDENTICAL3CROWS = ta.CDLIDENTICAL3CROWS(ope, high, low, close) CDLINNECK = ta.CDLINNECK(ope, high, low, close) CDLINVERTEDHAMMER = ta.CDLINVERTEDHAMMER(ope, high, low, close) CDLKICKING = ta.CDLKICKING(ope, high, low, close) CDLKICKINGBYLENGTH = ta.CDLKICKINGBYLENGTH(ope, high, low, close) CDLLADDERBOTTOM = ta.CDLLADDERBOTTOM(ope, high, low, close) CDLLONGLEGGEDDOJI = ta.CDLLONGLEGGEDDOJI(ope, high, low, close) CDLLONGLINE = ta.CDLLONGLINE(ope, high, low, close) CDLMARUBOZU = ta.CDLMARUBOZU(ope, high, low, close) CDLMATCHINGLOW = ta.CDLMATCHINGLOW(ope, high, low, close) CDLMATHOLD = ta.CDLMATHOLD(ope, high, low, close) CDLMORNINGDOJISTAR = ta.CDLMORNINGDOJISTAR(ope, high, low, close) CDLMORNINGSTAR = ta.CDLMORNINGSTAR(ope, high, low, close) CDLONNECK = ta.CDLONNECK(ope, high, low, close) CDLPIERCING = ta.CDLPIERCING(ope, high, low, close) CDLRICKSHAWMAN = ta.CDLRICKSHAWMAN(ope, high, low, close) CDLRISEFALL3METHODS = ta.CDLRISEFALL3METHODS(ope, high, low, close) CDLSEPARATINGLINES = ta.CDLSEPARATINGLINES(ope, high, low, close) CDLSHOOTINGSTAR = ta.CDLSHOOTINGSTAR(ope, high, low, close) CDLSHORTLINE = ta.CDLSHORTLINE(ope, high, low, close) CDLSPINNINGTOP = ta.CDLSPINNINGTOP(ope, high, low, close) CDLSTALLEDPATTERN = ta.CDLSTALLEDPATTERN(ope, high, low, close) CDLSTICKSANDWICH = ta.CDLSTICKSANDWICH(ope, high, low, close) CDLTAKURI = ta.CDLTAKURI(ope, high, low, close) CDLTASUKIGAP = ta.CDLTASUKIGAP(ope, high, low, close) CDLTHRUSTING = ta.CDLTHRUSTING(ope, high, low, close) CDLTRISTAR = ta.CDLTRISTAR(ope, high, low, close) CDLUNIQUE3RIVER = ta.CDLUNIQUE3RIVER(ope, high, low, close) CDLUPSIDEGAP2CROWS = ta.CDLUPSIDEGAP2CROWS(ope, high, low, close) CDLXSIDEGAP3METHODS = ta.CDLXSIDEGAP3METHODS(ope, high, low, close) f = numpy.column_stack( (ATR, NATR, TRANGE, HT_DCPERIOD, HT_DCPHASE, HT_PHASOR[0], HT_PHASOR[1], HT_SINE[0], HT_SINE[1], HT_TRENDMODE, AVGPRICE, MEDPRICE, TYPPRICE, WCLPRICE, ADX, ADXR, APO, AROON[0], AROON[1], AROONOSC, BOP, CCI, CMO, DX, MACD[0], MACD[1], MACD[2], MACDEXT[0], MACDEXT[1], MACDEXT[2], MFI, MINUS_DI, MINUS_DM, MOM, PLUS_DI, PLUS_DM, PPO, ROC, ROCP, ROCR, RSI, STOCH[0], STOCH[1], STOCHF[0], STOCHF[1], STOCHRSI[0], STOCHRSI[1], TRIX, ULTOSC, WILLR, CDL2CROWS, CDL3BLACKCROWS, CDL3INSIDE, CDL3LINESTRIKE, CDL3OUTSIDE, CDL3STARSINSOUTH, CDL3WHITESOLDIERS, CDLABANDONEDBABY, CDLADVANCEBLOCK, CDLBELTHOLD, CDLBREAKAWAY, CDLCLOSINGMARUBOZU, CDLCONCEALBABYSWALL, CDLCOUNTERATTACK, CDLDARKCLOUDCOVER, CDLDOJI, CDLDOJISTAR, CDLDRAGONFLYDOJI, CDLENGULFING, CDLEVENINGDOJISTAR, CDLEVENINGSTAR, CDLGAPSIDESIDEWHITE, CDLGRAVESTONEDOJI, CDLHAMMER, CDLHANGINGMAN, CDLHARAMI, CDLHARAMICROSS, CDLHIGHWAVE, CDLHIKKAKE, CDLHIKKAKEMOD, CDLHOMINGPIGEON, CDLIDENTICAL3CROWS, CDLINNECK, CDLINVERTEDHAMMER, CDLKICKING, CDLKICKINGBYLENGTH, CDLLADDERBOTTOM, CDLLONGLEGGEDDOJI, CDLLONGLINE, CDLMARUBOZU, CDLMATCHINGLOW, CDLMATHOLD, CDLMORNINGDOJISTAR, CDLMORNINGSTAR, CDLONNECK, CDLPIERCING, CDLRICKSHAWMAN, CDLRISEFALL3METHODS, CDLSEPARATINGLINES, CDLSHOOTINGSTAR, CDLSHORTLINE, CDLSPINNINGTOP, CDLSTALLEDPATTERN, CDLSTICKSANDWICH, CDLTAKURI, CDLTASUKIGAP, CDLTHRUSTING, CDLTRISTAR, CDLUNIQUE3RIVER, CDLUPSIDEGAP2CROWS, CDLXSIDEGAP3METHODS, BBANDS[0], BBANDS[1], BBANDS[2], DEMA, EMA, HT_TRENDLINE, KAMA, MA, MAMA[0], MAMA[1], MIDPOINT, MIDPRICE, SAR, SAREXT, SMA, T3, TEMA, TRIMA, WMA, AD, ADOSC, OBV)) h = numpy.apply_along_axis(nor, 0, f) # normalize columnwise df = pandas.DataFrame( h, index=S.index, columns=[ 'ATR', 'NATR', 'TRANGE', 'HT_DCPERIOD', 'HT_DCPHASE', 'HT_PHASOR[0]', 'HT_PHASOR[1]', 'HT_SINE[0]', 'HT_SINE[1]', 'HT_TRENDMODE', 'AVGPRICE', 'MEDPRICE', 'TYPPRICE', 'WCLPRICE', 'ADX', 'ADXR', 'APO', 'AROON[0]', 'AROON[1]', 'AROONOSC', 'BOP', 'CCI', 'CMO', 'DX', 'MACD[0]', 'MACD[1]', 'MACD[2]', 'MACDEXT[0]', 'MACDEXT[1]', 'MACDEXT[2]', 'MFI', 'MINUS_DI', 'MINUS_DM', 'MOM', 'PLUS_DI', 'PLUS_DM', 'PPO', 'ROC', 'ROCP', 'ROCR', 'RSI', 'STOCH[0]', 'STOCH[1]', 'STOCHF[0]', 'STOCHF[1]', 'STOCHRSI[0]', 'STOCHRSI[1]', 'TRIX', 'ULTOSC', 'WILLR', 'CDL2CROWS', 'CDL3BLACKCROWS', 'CDL3INSIDE', 'CDL3LINESTRIKE', 'CDL3OUTSIDE', 'CDL3STARSINSOUTH', 'CDL3WHITESOLDIERS', 'CDLABANDONEDBABY', 'CDLADVANCEBLOCK', 'CDLBELTHOLD', 'CDLBREAKAWAY', 'CDLCLOSINGMARUBOZU', 'CDLCONCEALBABYSWALL', 'CDLCOUNTERATTACK', 'CDLDARKCLOUDCOVER', 'CDLDOJI', 'CDLDOJISTAR', 'CDLDRAGONFLYDOJI', 'CDLENGULFING', 'CDLEVENINGDOJISTAR', 'CDLEVENINGSTAR', 'CDLGAPSIDESIDEWHITE', 'CDLGRAVESTONEDOJI', 'CDLHAMMER', 'CDLHANGINGMAN', 'CDLHARAMI', 'CDLHARAMICROSS', 'CDLHIGHWAVE', 'CDLHIKKAKE', 'CDLHIKKAKEMOD', 'CDLHOMINGPIGEON', 'CDLIDENTICAL3CROWS', 'CDLINNECK', 'CDLINVERTEDHAMMER', 'CDLKICKING', 'CDLKICKINGBYLENGTH', 'CDLLADDERBOTTOM', 'CDLLONGLEGGEDDOJI', 'CDLLONGLINE', 'CDLMARUBOZU', 'CDLMATCHINGLOW', 'CDLMATHOLD', 'CDLMORNINGDOJISTAR', 'CDLMORNINGSTAR', 'CDLONNECK', 'CDLPIERCING', 'CDLRICKSHAWMAN', 'CDLRISEFALL3METHODS', 'CDLSEPARATINGLINES', 'CDLSHOOTINGSTAR', 'CDLSHORTLINE', 'CDLSPINNINGTOP', 'CDLSTALLEDPATTERN', 'CDLSTICKSANDWICH', 'CDLTAKURI', 'CDLTASUKIGAP', 'CDLTHRUSTING', 'CDLTRISTAR', 'CDLUNIQUE3RIVER', 'CDLUPSIDEGAP2CROWS', 'CDLXSIDEGAP3METHODS', 'BBANDS[0]', 'BBANDS[1]', 'BBANDS[2]', 'DEMA', 'EMA', 'HT_TRENDLINE', 'KAMA', 'MA', 'MAMA[0]', 'MAMA[1]', 'MIDPOINT', 'MIDPRICE', 'SAR', 'SAREXT', 'SMA', 'T3', 'TEMA', 'TRIMA', 'WMA', 'AD', 'ADOSC', 'OBV' ]) df.to_csv("C:\\Users\\...\\Documents\\Data_TA\\{0}\\{1}.csv".format(s, c))
EMA_lengths = [20, 50, 200] for l in EMA_lengths: prices['EMA_'+str(l)] = prices_g.close.apply(lambda x: ta.EMA(x,l)) prices['EMA_ratio'] = prices['EMA_200']/prices['EMA_50'] # Ratio of moving average prices['RSI'] = prices_g.close.apply(lambda x: ta.RSI(x, 14)) # 14 day relative strength index def CCI_func(x): xh = x.high xl = x.low xc = x.close return ta.CCI(high=xh, low=xl, close=xc, timeperiod=5) prices['CCI'] = prices_g.apply(CCI_func).reset_index(level=0,).drop(columns='name') # created an extra column. prices['Momentum'] = prices_g.close.apply(lambda x: ta.MOM(x, 10)) # 10 day rolling momentum changed to prices_g.close def Stoch_fast(x, index): xh = x.high xl = x.low xc = x.close return ta.STOCHF(high=xh, low=xl, close=xc, fastk_period=14, fastd_period=3)[index] # Params 14, 3 #prices['Stoch_fastk'] = prices_g.apply(Stoch_fast, 0).reset_index(level=1,).drop(columns='symbol') # Stochastic fast indicator #prices['Stoch_fastd'] = prices_g.apply(Stoch_fast, 1).reset_index(level=1,).drop(columns='symbol') # def OBV_func(x): # xc = x.close # xv = x.volume # return ta.OBV(xc, xv) #prices['OBV'] = prices_g.apply(OBV_func).reset_index(level=1,).drop(columns='symbol') # On Balance Volume
def MOM (close, timeperiod=10): # N DAYS MOMENTUM real_ = ta.MOM(close, timeperiod) return real_
# df['adx'] = ADX(df, timeperiod = 14) df['adx'] = ADX(df, timeperiod = 7) # df['adxr' ] = talib.ADXR(df['high'],df['low'],df['close'],timeperiod = 14) df['adxr'] = talib.ADXR(df['high'],df['low'],df['close'],timeperiod = 7) # df['apo'] = talib.APO(df['close'], fastperiod=12, slowperiod=26, matype=0) df['apo'] = talib.APO(df['close'], fastperiod=6, slowperiod=13, matype=0) # df['cmo'] = talib.CMO(df['close'], timeperiod=14) df['cmo'] = talib.CMO(df['close'], timeperiod=7) # df['mom'] = talib.MOM(df['close'], timeperiod=10) df['mom'] = talib.MOM(df['close'], timeperiod=5) # df['ppo'] = talib.PPO(df['close'], fastperiod=12, slowperiod=26, matype=0) df['ppo'] = talib.PPO(df['close'], fastperiod=6, slowperiod=13, matype=0) # df['trix'] = talib.TRIX(df['close'], timeperiod=30) df['trix'] = talib.TRIX(df['close'], timeperiod=5) # df['bop'] = BOP(df) # df['cci'] = CCI(df) # df['dx'] = DX(df) # df['macdext'] = MACDEXT(df)
def get_momentum_indicators(df_price): df_local = df_price.copy() df_nonna_idxs = df_local[~df_local.Close.isna()].Close.index np_adj_close = df_local.Adj_Close.values np_close = df_local.Close.values np_open = df_local.Open.values np_high = df_local.High.values np_low = df_local.Low.values np_volume = df_local.Volume.values np_nan_indices = np.isnan(np_close) #ADX-Average Directional Movement Index ADX = pd.Series(ta.ADX(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices], timeperiod=20), index=df_nonna_idxs) df_local['ADX'] = ADX #ADXR-Average Directional Movement Index Rating ADXR = pd.Series(ta.ADXR(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['ADXR'] = ADXR #APO-Absolute Price Oscillator APO = pd.Series(ta.APO(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['APO'] = APO #AROON-Aroon AROON = ta.AROON(np_high[~np_nan_indices], np_low[~np_nan_indices]) df_local['AROON_DOWN'] = pd.Series(AROON[0], index=df_nonna_idxs) df_local['AROON_UP'] = pd.Series(AROON[1], index=df_nonna_idxs) #AROONOSC-Aroon Oscillator AROONOSC = pd.Series(ta.AROONOSC(np_high[~np_nan_indices], np_low[~np_nan_indices]), index=df_nonna_idxs) df_local['AROONOSC'] = AROONOSC #BOP-Balance Of Power BOP = pd.Series(ta.BOP(np_open[~np_nan_indices], np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['BOP'] = BOP #AROONOSC-Aroon Oscillator AROONOSC = pd.Series(ta.AROONOSC(np_high[~np_nan_indices], np_low[~np_nan_indices]), index=df_nonna_idxs) df_local['AROONOSC'] = AROONOSC #CCI-Commodity Channel Index CCI = pd.Series(ta.CCI(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['CCI'] = CCI #CMO-Chande Momentum Oscillator CMO = pd.Series(ta.CMO(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['CMO'] = CMO #DX-Directional Movement Index DX = pd.Series(ta.DX(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['DX'] = DX #MACD-Moving Average Convergence/Divergence MACD = pd.Series(ta.MACD(np_adj_close[~np_nan_indices])[0], index=df_nonna_idxs) df_local['MACD'] = MACD #MACDEXT-MACD with controllable MA type MACDEXT = pd.Series(ta.MACDEXT(np_adj_close[~np_nan_indices])[0], index=df_nonna_idxs) df_local['MACDEXT'] = MACDEXT #MACDFIX-Moving Average Convergence/Divergence Fix 12/26 MACDFIX = pd.Series(ta.MACDFIX(np_adj_close[~np_nan_indices])[0], index=df_nonna_idxs) df_local['MACDFIX'] = MACDFIX #MFI-Money Flow Index MFI = pd.Series( ta.MFI( np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices], ##np_volume[~np_nan_indices] np.asarray(np_volume[~np_nan_indices], dtype='float')), index=df_nonna_idxs) df_local['MFI'] = MFI #MINUS_DI-Minus Directional Indicator MINUS_DI = pd.Series(ta.MINUS_DI(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['MINUS_DI'] = MINUS_DI #MINUS_DM-Minus Directional Movement MINUS_DM = pd.Series(ta.MINUS_DM(np_high[~np_nan_indices], np_low[~np_nan_indices]), index=df_nonna_idxs) df_local['MINUS_DM'] = MINUS_DM #MOM-Momentum MOM = pd.Series(ta.MOM(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['MOM'] = MOM #PLUS_DI-Plus Directional Indicator PLUS_DI = pd.Series(ta.PLUS_DI(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['PLUS_DI'] = PLUS_DI #PLUS_DM-Plus Directional Movement PLUS_DM = pd.Series(ta.PLUS_DM(np_high[~np_nan_indices], np_low[~np_nan_indices]), index=df_nonna_idxs) df_local['PLUS_DM'] = PLUS_DM #PPO-Percentage Price Oscillator PPO = pd.Series(ta.PPO(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['PPO'] = PPO #ROC-Rate of change : ((price/prevPrice)-1)*100 ROC = pd.Series(ta.ROC(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['ROC'] = ROC #ROCP-Rate of change Percentage: (price-prevPrice)/prevPrice ROCP = pd.Series(ta.ROCP(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['ROCP'] = ROCP #RSI-Relative Strength Index RSI = pd.Series(ta.RSI(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['RSI'] = RSI #STOCH-Stochastic STOCH = ta.STOCH(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]) df_local['STOCH_SLOWK'] = pd.Series(STOCH[0], index=df_nonna_idxs) df_local['STOCH_SLOWD'] = pd.Series(STOCH[1], index=df_nonna_idxs) #STOCHF-Stochastic Fast STOCHF = ta.STOCHF(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]) df_local['STOCHF_FASTK'] = pd.Series(STOCHF[0], index=df_nonna_idxs) df_local['STOCHF_FASTD'] = pd.Series(STOCHF[1], index=df_nonna_idxs) #STOCHRSI-Stochastic Relative Strength Index STOCHRSI = ta.STOCHF(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]) df_local['STOCHRSI_FASTK'] = pd.Series(STOCHRSI[0], index=df_nonna_idxs) df_local['STOCHRSI_FASTD'] = pd.Series(STOCHRSI[1], index=df_nonna_idxs) #TRIX-1-day Rate-Of-Change (ROC) of a Triple Smooth EMA TRIX = pd.Series(ta.TRIX(np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['TRIX'] = TRIX #ULTOSC-Ultimate Oscillator ULTOSC = pd.Series(ta.ULTOSC(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices]), index=df_nonna_idxs) df_local['ULTOSC'] = ULTOSC #WILLR-Williams' %R WILLR = pd.Series(ta.WILLR(np_high[~np_nan_indices], np_low[~np_nan_indices], np_adj_close[~np_nan_indices], timeperiod=14), index=df_nonna_idxs) df_local['WILLR'] = WILLR return df_local
def get_data(type): data_df = pd.read_csv(FILE_PATH, index_col=[0]) #data_df['date'] = data_df['date'].map(lambda x: dt.strptime(x, '%m/%d/%Y %I:%M:%S %p').date()) data_df['volume'] = data_df['volume'].astype(float) #data_df = data_df.set_index('date') # Simple Moving Average data_df['sma_5'] = talib.SMA(data_df['close'].values, timeperiod=5) data_df['sma_10'] = talib.SMA(data_df['close'].values, timeperiod=10) # Exponential Moving Average data_df['ema_20'] = talib.EMA(data_df['close'].values, timeperiod=20) # Momentum 6 Month / Momentum 12 Month data_df['mtm6_mtm12'] = talib.MOM( data_df['close'].values, timeperiod=126) / talib.MOM( data_df['close'].values, timeperiod=252) # Stochastic Relative Strength Index data_df['fastk'], data_df['fastd'] = talib.STOCHRSI( data_df['close'].values, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) # Rate Of Change data_df['roc_10'] = talib.ROC(data_df['close'].values, timeperiod=10) # Bollinger Bands data_df['bband_upper'], data_df['bband_middle'], data_df[ 'bband_lower'] = talib.BBANDS(data_df['close'].values, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) # Moving Average Convergence Divergence data_df['macd'], data_df['macdsignal'], data_df['macdhist'] = talib.MACD( data_df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9) # Chaikin A/D Oscillator data_df['adosc'] = talib.ADOSC(data_df['high'], data_df['low'], data_df['close'], data_df['volume'], fastperiod=3, slowperiod=10) # Commodity Channel Index data_df['cci_14'] = talib.CCI(data_df['high'].values, data_df['low'].values, data_df['close'].values, timeperiod=14) # Average True Range data_df['atr_14'] = talib.ATR(data_df['high'].values, data_df['low'].values, data_df['close'].values, timeperiod=14) # Target data_df['target'] = np.append(data_df['close'][1:].values, [np.nan]) # Drop Rows With NA Values In Any Column data_df = data_df.dropna(axis=0, how='any') if type == "reg": # Popping The Target Column target = data_df.pop('target').values elif type == "class": print("type = classification") data_df['class'] = np.where((data_df['close'] < data_df['target']), 1, 0) data_df.drop('target', axis=1, inplace=True) target = data_df.pop('class').values return data_df, target
def add_techicalAnalysis(df): # open_price = df["adj_open"].values close_price = df["adj_close"].values # low_price = df["adj_low"].values # high_price = df["adj_high"].values # volume = df["adj_volume"].values #'EMA', 'TEMA', #'APO', 'CMO', 'MACD', 'MACD_SIG', 'MACD_HIST', 'MOM', 'PPO', 'ROCP', 'RSI', 'TRIX' #'HT_DCPERIOD', 'HT_DCPHASE', 'SINE', 'LEADSINE', 'INPHASE', 'QUADRATURE' # ===================================== # Overlap Studies # ===================================== df['EMA'] = talib.EMA(close_price) # TEMA - Triple Exponential Moving Average df['TEMA'] = talib.EMA(close_price) # WMA - Weighted Moving Average #df['WMA'] = talib.WMA(close_price, timeperiod=30) # HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline #df['HT_TRENDLINE'] = talib.HT_TRENDLINE(close_price) # ===================================== # Momentum Indicator Functions # ===================================== # APO - Absolute Price Oscillator df['APO'] = talib.APO(close_price, fastperiod=12, slowperiod=26, matype=0) # CMO - Chande Momentum Oscillator df['CMO'] = talib.CMO(close_price, timeperiod=14) # MACD - Moving Average Convergence/Divergence df['MACD'], df['MACD_SIG'], df['MACD_HIST'] = talib.MACD(close_price, fastperiod=12, slowperiod=26, signalperiod=9) # MOM - Momentum df['MOM'] = talib.MOM(close_price) # PPO - Percentage Price Oscillator df['PPO'] = talib.PPO(close_price, fastperiod=12, slowperiod=26, matype=0) # ROCP - Rate of change Percentage: (price-prevPrice)/prevPrice df['ROCP'] = talib.ROCP(close_price, timeperiod=10) # RSI - Relative Strength Index df['RSI'] = talib.RSI(close_price, timeperiod=14) # TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA df['TRIX'] = talib.TRIX(close_price) # NOT USED # ADXR - Average Directional Movement Index Rating # df['ADXR'] = talib.ADXR(high_price, low_price, close_price) # AROON - Aroon # df['AROON_UP'], _ = talib.AROON(high_price, low_price) # CCI - Commodity Channel Index # df['CCI'] = talib.CCI(high_price, low_price, close_price) # ULTOSC - Ultimate Oscillator # df['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price) # WILLR - Williams' %R # df['WILLR'] = talib.WILLR(high_price, low_price, close_price) # ===================================== # Cycle Indicator Functions # ===================================== # HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period df['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price) # HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase df['HT_DCPHASE'] = talib.HT_DCPHASE(close_price) # HT_SINE - Hilbert Transform - SineWave df['SINE'], df['LEADSINE'] = talib.HT_SINE(close_price) # HT_TRENDMODE - Hilbert Transform - Trend vs Cycle Mode #df['HT_TRENDMODE'] = talib.HT_TRENDMODE(close_price) # HT_PHASOR - Hilbert Transform - Phasor Components df['INPHASE'], df['QUADRATURE'] = talib.HT_PHASOR(close_price) # NOT USED # df['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price) # df['FASTK'], df['FASTD'] = talib.STOCHF(high_price, low_price, close_price) # df['NATR'] = talib.NATR(high_price, low_price, close_price) return df
#print df.head() #df.apply(pd.to_numeric, errors='ignore') df['start'] = pd.to_datetime(df['start'], unit='s') df.set_index('start', inplace=True) # for y in df.columns: # print y,df[y].dtype #newdf = df.tail(60) newdf = df price = np.array(newdf['vwp']) h = np.array(newdf['high']) l = np.array(newdf['low']) c = np.array(newdf['close']) #sma = talib.SMA(price,50) m = talib.MACD(price, 12, 26, 9) mom = talib.MOM(price, 12) rsi = talib.RSI(price) krsi, drsi = talib.STOCHRSI(price) roc = talib.ROC(price) mean5d = newdf['vwp'].rolling(window=5, center=False).mean() mean10d = newdf['vwp'].rolling(window=10, center=False).mean() newdf['willR'] = talib.WILLR(h, l, c) newdf['krsi'] = krsi newdf['drsi'] = drsi newdf['disp5d'] = newdf['vwp'] - mean5d newdf['disp10d'] = newdf['vwp'] - mean10d newdf['cci'] = talib.CCI(h, l, c) newdf['roc'] = roc #m[1][40:60] # newdf['msig'] = np.where((m[1]>m[2]),1,0) newdf['msig'] = m[1] - m[2]
import tushare as ts import numpy as np import talib df_dwzj=ts.get_k_data('601198') close_dwzj = [float(x) for x in df_dwzj['close']] df_dwzj['RSI']=talib.RSI(np.array(close_dwzj), timeperiod=6) #RSI的天数一般是6、12、24 df_dwzj['MOM']=talib.MOM(np.array(close_dwzj), timeperiod=5) df_dwzj.tail(6) df_yhzj=ts.get_k_data('601881') close_yhzj = [float(x) for x in df_yhzj['close']] df_yhzj['RSI']=talib.RSI(np.array(close_yhzj), timeperiod=6) #RSI的天数一般是6、12、24 df_yhzj['MOM']=talib.MOM(np.array(close_yhzj), timeperiod=5) df_yhzj.tail(6) pro = ts.pro_api() new = pro.cctv_news(date='20190131')
def test__mom(self): me = ta_mom(DF_TEST["Close"])[-100:] ta = talib.MOM(DF_TEST["Close"])[-100:] np.testing.assert_array_almost_equal(me, ta)
def MOM(data, **kwargs): _check_talib_presence() prices = _extract_series(data) return talib.MOM(prices, **kwargs)
def get_factors(index, Open, Close, High, Low, Volume, rolling = 26, drop=False, normalization=True): tmp = pd.DataFrame() tmp['tradeTime'] = index #累积/派发线(Accumulation / Distribution Line,该指标将每日的成交量通过价格加权累计, #用以计算成交量的动量。属于趋势型因子 tmp['AD'] = talib.AD(High, Low, Close, Volume) # 佳庆指标(Chaikin Oscillator),该指标基于AD曲线的指数移动均线而计算得到。属于趋势型因子 tmp['ADOSC'] = talib.ADOSC(High, Low, Close, Volume, fastperiod=3, slowperiod=10) # 平均动向指数,DMI因子的构成部分。属于趋势型因子 tmp['ADX'] = talib.ADX(High, Low, Close,timeperiod=14) # 相对平均动向指数,DMI因子的构成部分。属于趋势型因子 tmp['ADXR'] = talib.ADXR(High, Low, Close,timeperiod=14) # 绝对价格振荡指数 tmp['APO'] = talib.APO(Close, fastperiod=12, slowperiod=26) # Aroon通过计算自价格达到近期最高值和最低值以来所经过的期间数,帮助投资者预测证券价格从趋势到区域区域或反转的变化, #Aroon指标分为Aroon、AroonUp和AroonDown3个具体指标。属于趋势型因子 tmp['AROONDown'], tmp['AROONUp'] = talib.AROON(High, Low,timeperiod=14) tmp['AROONOSC'] = talib.AROONOSC(High, Low,timeperiod=14) # 均幅指标(Average TRUE Ranger),取一定时间周期内的股价波动幅度的移动平均值, #是显示市场变化率的指标,主要用于研判买卖时机。属于超买超卖型因子。 tmp['ATR14']= talib.ATR(High, Low, Close, timeperiod=14) tmp['ATR6']= talib.ATR(High, Low, Close, timeperiod=6) # 布林带 tmp['Boll_Up'],tmp['Boll_Mid'],tmp['Boll_Down']= talib.BBANDS(Close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0) # 均势指标 tmp['BOP'] = talib.BOP(Open, High, Low, Close) #5日顺势指标(Commodity Channel Index),专门测量股价是否已超出常态分布范围。属于超买超卖型因子。 tmp['CCI5'] = talib.CCI(High, Low, Close, timeperiod=5) tmp['CCI10'] = talib.CCI(High, Low, Close, timeperiod=10) tmp['CCI20'] = talib.CCI(High, Low, Close, timeperiod=20) tmp['CCI88'] = talib.CCI(High, Low, Close, timeperiod=88) # 钱德动量摆动指标(Chande Momentum Osciliator),与其他动量指标摆动指标如相对强弱指标(RSI)和随机指标(KDJ)不同, # 钱德动量指标在计算公式的分子中采用上涨日和下跌日的数据。属于超买超卖型因子 tmp['CMO_Close'] = talib.CMO(Close,timeperiod=14) tmp['CMO_Open'] = talib.CMO(Close,timeperiod=14) # DEMA双指数移动平均线 tmp['DEMA6'] = talib.DEMA(Close, timeperiod=6) tmp['DEMA12'] = talib.DEMA(Close, timeperiod=12) tmp['DEMA26'] = talib.DEMA(Close, timeperiod=26) # DX 动向指数 tmp['DX'] = talib.DX(High, Low, Close,timeperiod=14) # EMA 指数移动平均线 tmp['EMA6'] = talib.EMA(Close, timeperiod=6) tmp['EMA12'] = talib.EMA(Close, timeperiod=12) tmp['EMA26'] = talib.EMA(Close, timeperiod=26) # KAMA 适应性移动平均线 tmp['KAMA'] = talib.KAMA(Close, timeperiod=30) # MACD tmp['MACD_DIF'],tmp['MACD_DEA'],tmp['MACD_bar'] = talib.MACD(Close, fastperiod=12, slowperiod=24, signalperiod=9) # 中位数价格 不知道是什么意思 tmp['MEDPRICE'] = talib.MEDPRICE(High, Low) # 负向指标 负向运动 tmp['MiNUS_DI'] = talib.MINUS_DI(High, Low, Close,timeperiod=14) tmp['MiNUS_DM'] = talib.MINUS_DM(High, Low,timeperiod=14) # 动量指标(Momentom Index),动量指数以分析股价波动的速度为目的,研究股价在波动过程中各种加速, #减速,惯性作用以及股价由静到动或由动转静的现象。属于趋势型因子 tmp['MOM'] = talib.MOM(Close, timeperiod=10) # 归一化平均值范围 tmp['NATR'] = talib.NATR(High, Low, Close,timeperiod=14) # OBV 能量潮指标(On Balance Volume,OBV),以股市的成交量变化来衡量股市的推动力, #从而研判股价的走势。属于成交量型因子 tmp['OBV'] = talib.OBV(Close, Volume) # PLUS_DI 更向指示器 tmp['PLUS_DI'] = talib.PLUS_DI(High, Low, Close,timeperiod=14) tmp['PLUS_DM'] = talib.PLUS_DM(High, Low, timeperiod=14) # PPO 价格振荡百分比 tmp['PPO'] = talib.PPO(Close, fastperiod=6, slowperiod= 26, matype=0) # ROC 6日变动速率(Price Rate of Change),以当日的收盘价和N天前的收盘价比较, #通过计算股价某一段时间内收盘价变动的比例,应用价格的移动比较来测量价位动量。属于超买超卖型因子。 tmp['ROC6'] = talib.ROC(Close, timeperiod=6) tmp['ROC20'] = talib.ROC(Close, timeperiod=20) #12日量变动速率指标(Volume Rate of Change),以今天的成交量和N天前的成交量比较, #通过计算某一段时间内成交量变动的幅度,应用成交量的移动比较来测量成交量运动趋向, #达到事先探测成交量供需的强弱,进而分析成交量的发展趋势及其将来是否有转势的意愿, #属于成交量的反趋向指标。属于成交量型因子 tmp['VROC6'] = talib.ROC(Volume, timeperiod=6) tmp['VROC20'] = talib.ROC(Volume, timeperiod=20) # ROC 6日变动速率(Price Rate of Change),以当日的收盘价和N天前的收盘价比较, #通过计算股价某一段时间内收盘价变动的比例,应用价格的移动比较来测量价位动量。属于超买超卖型因子。 tmp['ROCP6'] = talib.ROCP(Close, timeperiod=6) tmp['ROCP20'] = talib.ROCP(Close, timeperiod=20) #12日量变动速率指标(Volume Rate of Change),以今天的成交量和N天前的成交量比较, #通过计算某一段时间内成交量变动的幅度,应用成交量的移动比较来测量成交量运动趋向, #达到事先探测成交量供需的强弱,进而分析成交量的发展趋势及其将来是否有转势的意愿, #属于成交量的反趋向指标。属于成交量型因子 tmp['VROCP6'] = talib.ROCP(Volume, timeperiod=6) tmp['VROCP20'] = talib.ROCP(Volume, timeperiod=20) # RSI tmp['RSI'] = talib.RSI(Close, timeperiod=14) # SAR 抛物线转向 tmp['SAR'] = talib.SAR(High, Low, acceleration=0.02, maximum=0.2) # TEMA tmp['TEMA6'] = talib.TEMA(Close, timeperiod=6) tmp['TEMA12'] = talib.TEMA(Close, timeperiod=12) tmp['TEMA26'] = talib.TEMA(Close, timeperiod=26) # TRANGE 真实范围 tmp['TRANGE'] = talib.TRANGE(High, Low, Close) # TYPPRICE 典型价格 tmp['TYPPRICE'] = talib.TYPPRICE(High, Low, Close) # TSF 时间序列预测 tmp['TSF'] = talib.TSF(Close, timeperiod=14) # ULTOSC 极限振子 tmp['ULTOSC'] = talib.ULTOSC(High, Low, Close, timeperiod1=7, timeperiod2=14, timeperiod3=28) # 威廉指标 tmp['WILLR'] = talib.WILLR(High, Low, Close, timeperiod=14) # 标准化 if normalization: factors_list = tmp.columns.tolist()[1:] if rolling >= 26: for i in factors_list: tmp[i] = (tmp[i] - tmp[i].rolling(window=rolling, center=False).mean()) /tmp[i].rolling(window=rolling, center=False).std() elif rolling < 26 & rolling > 0: print ('Recommended rolling range greater than 26') elif rolling <=0: for i in factors_list: tmp[i] = (tmp[i] - tmp[i].mean())/tmp[i].std() if drop: tmp.dropna(inplace=True) tmp.set_index('tradeTime', inplace=True) return tmp
def MOM(close, timeperiod=10): # Momentum return talib.MOM(close, timeperiod=timeperiod)
def price_predictions(ticker, start, end, forecast_out): file_path = symbol_to_path(ticker) df = pd.read_csv(file_path, index_col="<DTYYYYMMDD>", parse_dates=True, usecols=[ "<DTYYYYMMDD>", "<OpenFixed>", "<HighFixed>", "<LowFixed>", "<CloseFixed>", "<Volume>" ], na_values="nan") df = df.rename( columns={ '<DTYYYYMMDD>': 'Date', "<OpenFixed>": 'Open', '<HighFixed>': 'High', '<LowFixed>': 'Low', '<CloseFixed>': 'Close', '<Volume>': 'Volume' }) # columns order for backtrader type columnsOrder = ["Open", "High", "Low", "Close", "Volume", "OpenInterest"] # change the index by new index df = df.reindex(columns=columnsOrder) # change date index to increasing order df = df.sort_index() # take a part of dataframe df = df.loc[start:end] df['HL_PCT'] = (df['High'] - df['Low']) / df['Close'] * 100.0 df['PCT_change'] = (df['Close'] - df['Open']) / df['Open'] * 100.0 bbwindow = 25 vlwindow = 10 mmtum = 10 df['BB_Value'] = compute_indicator_bb(df, window=bbwindow) df['Volatility'] = compute_indicator_volatility(df, timeperiod=vlwindow) df['Momentum'] = talib.MOM(df['Close'].values, timeperiod=mmtum) df['OBV'] = talib.OBV(df['Close'].values, df['Volume'].values.astype(np.float64)) df['MACD'], _, _ = talib.MACD(df['Close'].values, fastperiod=12, slowperiod=26, signalperiod=9) _, df['STOCH'] = talib.STOCH(df['High'].values, df['Low'].values, df['Close'].values, fastk_period=14, slowk_period=1, slowd_period=5) df['MFI'] = talib.MFI(df['High'].values, df['Low'].values, df['Close'].values, df['Volume'].values.astype(np.float64), timeperiod=14) # df['EMA3'] = pd.Series(pd.Series.ewm(df['Close'], span = 3, min_periods = 3-1).mean()) # df['EMA6'] = pd.Series(pd.Series.ewm(df['Close'], span = 6, min_periods = 6-1).mean()) # df['EMA18'] = pd.Series(pd.Series.ewm(df['Close'], span = 18, min_periods = 18-1).mean()) df['PDI'] = talib.PLUS_DI(df['High'].values, df['Low'].values, df['Close'].values, timeperiod=14) df['NDI'] = talib.MINUS_DI(df['High'].values, df['Low'].values, df['Close'].values, timeperiod=14) # df = df[['Close', 'HL_PCT', 'PCT_change', 'Volume','BB_Value', # 'Volatility', 'Momentum', 'MACD', 'STOCH', 'MFI', 'OBV']] # df = df[['Close', 'HL_PCT', 'PCT_change', 'Volume', 'BB_Value']] df.fillna(method="ffill", inplace=True) df.fillna(method="backfill", inplace=True) forecast_col = 'Close' #inplace : boolean, default False # If True, fill in place. Note: this will modify any other views on this object, # (e.g. a no-copy slice for a column in a DataFrame). # Du bao 1% cua du lieu # Copy du lieu tu cot Adj. Close vao cot moi # Lenh Shift df['Target'] = df[forecast_col].shift(-forecast_out) # Lenh Drop loai bo label #axis : int or axis name: column # Whether to drop labels from the index (0 / ‘index’) or columns (1 / ‘columns’). X = np.array(df.drop(['Target'], 1)) y_true = df[forecast_col][-forecast_out:] # Preprocessing Input Data X = preprocessing.scale(X) #from sklearn.preprocessing import MinMaxScaler #scaler = MinMaxScaler() #X = scaler.fit_transform(X) # Tach gia tri X va X_lately ra khoi chuoi X_lately = X[-forecast_out:] X = X[:-forecast_out] # Loai bo cac gia tri NA # df.dropna(inplace=True) # Target la vector y lay tu cot label y = np.array(df['Target'].dropna()) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) #X_train, X_test, y_train, y_test = train_test_split(X, y) #from sklearn.preprocessing import MinMaxScaler #from sklearn.preprocessing import StandardScaler #scaler = MinMaxScaler() #scaler = StandardScaler() #X_train = scaler.fit_transform(X_train) #X_test = scaler.transform(X_test) #X_lately = scaler.transform(X_lately) n_neighbors = 5 knn = neighbors.KNeighborsRegressor(n_neighbors, weights='uniform') knn.fit(X_train, y_train) print('Train score KNN: ', knn.score(X_train, y_train), 'Test score KNN : ', knn.score(X_test, y_test)) forecast_set = knn.predict(X_lately) print('Price for next {} days'.format(forecast_out), forecast_set) bagging = BaggingRegressor(DecisionTreeRegressor(), n_estimators=50, random_state=50) bagging.fit(X_train, y_train) print('Train score BAG: ', bagging.score(X_train, y_train), 'Test score BAG : ', bagging.score(X_test, y_test)) forecast_set = bagging.predict(X_lately) print('Price for next {} days'.format(forecast_out), forecast_set) rf = RandomForestRegressor(n_estimators=50, random_state=50) rf.fit(X_train, y_train) print('Train score RF: ', rf.score(X_train, y_train), 'Test score RF : ', rf.score(X_test, y_test)) forecast_set = rf.predict(X_lately) print('Price for next {} days'.format(forecast_out), forecast_set) adaboost = AdaBoostRegressor(neighbors.KNeighborsRegressor(n_neighbors=5), n_estimators=30, random_state=0) #adaboost = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), # n_estimators=30, random_state=0) adaboost.fit(X_train, y_train) print('Train score Ada: ', adaboost.score(X_train, y_train), 'Test score Ada : ', adaboost.score(X_test, y_test)) forecast_set = adaboost.predict(X_lately) print('Price for next {} days'.format(forecast_out), forecast_set)
def mom(se,n): mom_n = talib.MOM(se,timeperiod=n) return mom_n
def get_feature_from_vector(vector, dtype=None): feature_dict = {} feature_dict['ema_10'] = tl.EMA(vector, timeperiod=10) feature_dict['ema_16'] = tl.EMA(vector, timeperiod=16) feature_dict['ema_22'] = tl.EMA(vector, timeperiod=22) feature_dict['sma_10'] = tl.SMA(vector, timeperiod=10) feature_dict['sma_16'] = tl.SMA(vector, timeperiod=16) feature_dict['sma_22'] = tl.SMA(vector, timeperiod=22) if dtype == 'adj_close': upper, middle, lower = tl.BBANDS(vector, timeperiod=20, nbdevup=2, nbdevdn=2) feature_dict['bbands_20_upper'] = upper feature_dict['bbands_20_lower'] = lower upper, middle, lower = tl.BBANDS(vector, timeperiod=26, nbdevup=2, nbdevdn=2) feature_dict['bbands_26_upper'] = upper feature_dict['bbands_26_lower'] = lower upper, middle, lower = tl.BBANDS(vector, timeperiod=32, nbdevup=2, nbdevdn=2) feature_dict['bbands_32_upper'] = upper feature_dict['bbands_32_lower'] = lower feature_dict['momentum_12'] = tl.MOM(vector, timeperiod=12) feature_dict['momentum_18'] = tl.MOM(vector, timeperiod=18) feature_dict['momentum_24'] = tl.MOM(vector, timeperiod=24) ema = tl.EMA(feature_dict['momentum_12'], timeperiod=2) feature_dict['momentum_to_ema_12'] = np.divide( feature_dict['momentum_12'], ema) feature_dict['ema_to_mom_12'] = ema ema = tl.EMA(feature_dict['momentum_18'], timeperiod=2) feature_dict['momentum_to_ema_18'] = np.divide( feature_dict['momentum_18'], ema) feature_dict['ema_to_mom_18'] = ema ema = tl.EMA(feature_dict['momentum_24'], timeperiod=2) feature_dict['momentum_to_ema_24'] = np.divide( feature_dict['momentum_24'], ema) feature_dict['ema_to_mom_24'] = ema feature_dict['rate_of_change_10'] = tl.ROCP(vector, timeperiod=10) feature_dict['rate_of_change_16'] = tl.ROCP(vector, timeperiod=16) feature_dict['rate_of_change_22'] = tl.ROCP(vector, timeperiod=22) macd = tl.MACD(vector, fastperiod=12, slowperiod=18)[0] feature_dict['macd_0_18'] = macd ema = tl.EMA(feature_dict['macd_0_18'], timeperiod=9) feature_dict['macds_0_18'] = ema feature_dict['macdsr_0_18'] = np.divide(feature_dict['macd_0_18'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=18)[1] feature_dict['macd_1_18'] = macd ema = tl.EMA(macd, timeperiod=9) feature_dict['macds_1_18'] = ema feature_dict['macdsr_1_18'] = np.divide(feature_dict['macd_1_18'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=18)[2] feature_dict['macd_2_18'] = macd ema = tl.EMA(macd, timeperiod=9) feature_dict['macds_2_18'] = ema feature_dict['macdsr_2_18'] = np.divide(feature_dict['macd_2_18'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=24)[0] feature_dict['macd_0_24'] = macd ema = tl.EMA(feature_dict['macd_0_24'], timeperiod=9) feature_dict['macds_0_24'] = ema feature_dict['macdsr_0_24'] = np.divide(feature_dict['macd_0_24'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=24)[1] feature_dict['macd_1_24'] = macd ema = tl.EMA(feature_dict['macd_1_24'], timeperiod=9) feature_dict['macds_1_24'] = ema feature_dict['macdsr_1_24'] = np.divide(feature_dict['macd_1_24'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=24)[2] feature_dict['macd_2_24'] = macd ema = tl.EMA(feature_dict['macd_2_24'], timeperiod=9) feature_dict['macds_2_24'] = ema feature_dict['macdsr_2_24'] = np.divide(feature_dict['macd_2_24'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=30)[0] feature_dict['macd_0_30'] = macd ema = tl.EMA(feature_dict['macd_0_30'], timeperiod=9) feature_dict['macds_0_30'] = ema feature_dict['macdsr_0_30'] = np.divide(feature_dict['macd_0_30'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=30)[1] feature_dict['macd_1_30'] = macd ema = tl.EMA(feature_dict['macd_1_30'], timeperiod=9) feature_dict['macds_1_30'] = ema feature_dict['macdsr_1_30'] = np.divide(feature_dict['macd_1_30'], ema) macd = tl.MACD(vector, fastperiod=12, slowperiod=30)[2] feature_dict['macd_2_30'] = macd ema = tl.EMA(feature_dict['macd_2_30'], timeperiod=9) feature_dict['macds_2_30'] = ema feature_dict['macdsr_2_30'] = np.divide(feature_dict['macd_2_30'], ema) feature_dict['rsi_8'] = tl.RSI(vector, timeperiod=8) feature_dict['rsi_14'] = tl.RSI(vector, timeperiod=14) feature_dict['rsi_20'] = tl.RSI(vector, timeperiod=20) return feature_dict
def add_technical_indicators_with_intervals(df, indicators=[], intervals=[], true_value=False, verbose=False): indicators_range = range(len(indicators)) if not verbose else tqdm( range(len(indicators))) for i in indicators_range: indicator = indicators[i] for interval in intervals: # 1 SMA if indicator == 'SMA': if not true_value: df["SMA_" + str(interval)] = df["Close"] - talib.SMA( df["Close"], timeperiod=interval) else: df["SMA_" + str(interval)] = talib.SMA(df["Close"], timeperiod=interval) # 2 KAMA if indicator == 'KAMA': if not true_value: df["KAMA_" + str(interval)] = df["Close"] - talib.KAMA( df["Close"], timeperiod=interval) else: df["KAMA_" + str(interval)] = talib.KAMA( df["Close"], timeperiod=interval) # 3 MIDPRICE if indicator == 'MIDPRICE': if not true_value: df["MIDPRICE_" + str(interval)] = df["Close"] - talib.MIDPRICE( df["High"], df["Low"], timeperiod=interval) else: df["MIDPRICE_" + str(interval)] = talib.MIDPRICE( df["High"], df["Low"], timeperiod=interval) # 4 MIDPOINT if indicator == 'MIDPOINT': if not true_value: df["MIDPOINT_" + str(interval)] = df["Close"] - talib.MIDPOINT( df["Close"], timeperiod=interval) else: df["MIDPOINT_" + str(interval)] = talib.MIDPOINT( df["Close"], timeperiod=interval) # 5 EMA if indicator == 'EMA': if not true_value: df["EMA_" + str(interval)] = df["Close"] - talib.EMA( df["Close"], timeperiod=interval) else: df["EMA_" + str(interval)] = talib.EMA(df["Close"], timeperiod=interval) # 6 DEMA if indicator == 'DEMA': if not true_value: df["DEMA_" + str(interval)] = df["Close"] - talib.DEMA( df["Close"], timeperiod=interval) else: df["DEMA_" + str(interval)] = talib.DEMA( df["Close"], timeperiod=interval) # 7 TEMA if indicator == 'TEMA': if not true_value: df["TEMA_" + str(interval)] = df["Close"] - talib.TEMA( df["Close"], timeperiod=interval) else: df["TEMA_" + str(interval)] = talib.TEMA( df["Close"], timeperiod=interval) # 8 TRIMA if indicator == 'TRIMA': if not true_value: df["TRIMA_" + str(interval)] = df["Close"] - talib.TRIMA( df["Close"], timeperiod=interval) else: df["TRIMA_" + str(interval)] = talib.TRIMA( df["Close"], timeperiod=interval) # 9 WMA if indicator == 'WMA': if not true_value: df["WMA_" + str(interval)] = df["Close"] - talib.WMA( df["Close"], timeperiod=interval) else: df["WMA_" + str(interval)] = talib.WMA(df["Close"], timeperiod=interval) # 10 LINEARREG if indicator == 'LINEARREG': if not true_value: df["LINEARREG_" + str(interval)] = df["Close"] - talib.LINEARREG( df["Close"], timeperiod=interval) else: df["LINEARREG_" + str(interval)] = talib.LINEARREG( df["Close"], timeperiod=interval) # 11 TSF if indicator == 'TSF': if not true_value: df["TSF_" + str(interval)] = df["Close"] - talib.TSF( df["Close"], timeperiod=interval) else: df["TSF_" + str(interval)] = talib.TSF(df["Close"], timeperiod=interval) # 12 SMAO if indicator == 'SMAO': if not true_value: df["SMAO_" + str(interval)] = df["Close"] - talib.SMA( df["Open"], timeperiod=interval) else: df["SMAO_" + str(interval)] = talib.SMA( df["Open"], timeperiod=interval) # 13 KAMAO if indicator == 'KAMAO': if not true_value: df["KAMAO_" + str(interval)] = df["Close"] - talib.KAMA( df["Open"], timeperiod=interval) else: df["KAMAO_" + str(interval)] = talib.KAMA( df["Open"], timeperiod=interval) # 14 MIDPOINTO if indicator == 'MIDPOINTO': if not true_value: df["MIDPOINTO_" + str(interval)] = df["Close"] - talib.MIDPOINT( df["Open"], timeperiod=interval) else: df["MIDPOINTO_" + str(interval)] = talib.MIDPOINT( df["Open"], timeperiod=interval) # 15 EMAO if indicator == 'EMAO': if not true_value: df["EMAO_" + str(interval)] = df["Close"] - talib.EMA( df["Open"], timeperiod=interval) else: df["EMAO_" + str(interval)] = talib.EMA( df["Open"], timeperiod=interval) # 16 DEMAO if indicator == 'DEMAO': if not true_value: df["DEMAO_" + str(interval)] = df["Close"] - talib.DEMA( df["Open"], timeperiod=interval) else: df["DEMAO_" + str(interval)] = talib.DEMA( df["Open"], timeperiod=interval) # 17 TEMAO if indicator == 'TEMAO': if not true_value: df["TEMAO_" + str(interval)] = df["Close"] - talib.TEMA( df["Open"], timeperiod=interval) else: df["TEMAO_" + str(interval)] = talib.TEMA( df["Open"], timeperiod=interval) # 18 TRIMAO if indicator == 'TRIMAO': if not true_value: df["TRIMAO_" + str(interval)] = df["Close"] - talib.TRIMA( df["Open"], timeperiod=interval) else: df["TRIMAO_" + str(interval)] = talib.TRIMA( df["Open"], timeperiod=interval) # 19 WMAO if indicator == 'WMAO': if not true_value: df["WMAO_" + str(interval)] = df["Close"] - talib.WMA( df["Open"], timeperiod=interval) else: df["WMAO_" + str(interval)] = talib.WMA( df["Open"], timeperiod=interval) # 20 LINEARREGO if indicator == 'LINEARREGO': if not true_value: df["LINEARREGO_" + str(interval)] = df["Close"] - talib.LINEARREG( df["Open"], timeperiod=interval) else: df["LINEARREGO_" + str(interval)] = talib.LINEARREG( df["Open"], timeperiod=interval) # 21 TSFO if indicator == 'TSFO': if not true_value: df["TSFO_" + str(interval)] = df["Close"] - talib.TSF( df["Open"], timeperiod=interval) else: df["TSFO_" + str(interval)] = talib.TSF( df["Open"], timeperiod=interval) # 22 MACD if indicator == 'MACD' or indicator == 'MACDhist': df["MACD_" + str(interval)], _, df["MACDhist_" + str(interval)] = talib.MACD( df["Close"], fastperiod=interval, slowperiod=interval * 2, signalperiod=int( interval * 1.5)) # 23 MACDFIX if indicator == 'MACDFIX': df["MACDFIX_" + str(interval)], _, _ = talib.MACDFIX( df["Close"], signalperiod=interval) # 24 MOM if indicator == 'MOM': df["MOM_" + str(interval)] = talib.MOM(df["Close"], timeperiod=interval) # 25 ROCP if indicator == 'ROCP': df["ROCP_" + str(interval)] = talib.ROCP(df["Close"], timeperiod=interval) # 26 APO if indicator == 'APO': df["APO_" + str(interval)] = talib.APO(df["Close"], fastperiod=interval, slowperiod=interval * 2) # 27 MINUS_DM if indicator == 'MINUS_DM': df["MINUS_DM_" + str(interval)] = talib.MINUS_DM( df["High"], df["Low"], timeperiod=interval) # 28 PLUS_DM if indicator == 'PLUS_DM': df["PLUS_DM_" + str(interval)] = talib.PLUS_DM( df["High"], df["Low"], timeperiod=interval) # 29 BETA if indicator == 'BETA': df["BETA_" + str(interval)] = talib.BETA( df["High"], df["Low"], timeperiod=interval) / 100.0 # 30 TRIX if indicator == 'TRIX': df["TRIX_" + str(interval)] = talib.TRIX(df["Close"], timeperiod=interval) # 31 ATR if indicator == 'ATR': df["ATR_" + str(interval)] = talib.ATR(df["High"], df["Low"], df["Close"], timeperiod=interval) # 32 PPO if indicator == 'PPO': df["PPO_" + str(interval)] = talib.PPO(df["Close"], fastperiod=interval, slowperiod=interval * 2) # 33 RSI if indicator == 'RSI': df["RSI_" + str(interval)] = talib.RSI( df["Close"], timeperiod=interval) / 100.0 # 34 RSIO if indicator == 'RSIO': df["RSIO_" + str(interval)] = talib.RSI( df["Open"], timeperiod=interval) / 100.0 # 35 LINEARREG_ANGLE if indicator == 'LINEARREG_ANGLE': df["LINEARREG_ANGLE_" + str(interval)] = talib.LINEARREG_ANGLE( df["Close"], timeperiod=interval) / 100.0 # 36 MINUS_DI if indicator == 'MINUS_DI': df["MINUS_DI_" + str(interval)] = talib.MINUS_DI( df["High"], df["Low"], df["Close"], timeperiod=interval) / 100.0 # 37 PLUS_DI if indicator == 'PLUS_DI': df["PLUS_DI_" + str(interval)] = talib.PLUS_DI( df["High"], df["Low"], df["Close"], timeperiod=interval) / 100.0 # 38 DX if indicator == 'DX': df["DX_" + str(interval)] = talib.DX( df["High"], df["Low"], df["Close"], timeperiod=interval) / 100.0 # 39 ADX if indicator == 'ADX': df["ADX_" + str(interval)] = talib.ADX( df["High"], df["Low"], df["Close"], timeperiod=interval) / 100.0 # 40 ADXO if indicator == 'ADXO': df["ADXO_" + str(interval)] = talib.ADX( df["High"], df["Low"], df["Open"], timeperiod=interval) / 100.0 # 41 ADXR if indicator == 'ADXR': df["ADXR_" + str(interval)] = talib.ADXR( df["High"], df["Low"], df["Close"], timeperiod=interval) / 100.0 # 42 CCI if indicator == 'CCI': df["CCI_" + str(interval)] = talib.CCI( df["High"], df["Low"], df["Close"], timeperiod=interval) / 100.0 # 42 CCIO if indicator == 'CCIO': df["CCIO_" + str(interval)] = talib.CCI( df["High"], df["Low"], df["Open"], timeperiod=interval) / 100.0 # 43 WILLR if indicator == 'WILLR': df["WILLR_" + str(interval)] = talib.WILLR( df["High"], df["Low"], df["Close"], timeperiod=interval) / 100.0 # 44 WILLRO if indicator == 'WILLRO': df["WILLRO_" + str(interval)] = talib.WILLR( df["High"], df["Low"], df["Open"], timeperiod=interval) / 100.0 # 45 CMO if indicator == 'CMO': df["CMO_" + str(interval)] = talib.CMO( df["Close"], timeperiod=interval) / 100.0 # 46 AROONOSC if indicator == 'AROONOSC': df["AROONOSC_" + str(interval)] = talib.AROONOSC( df["High"], df["Low"], timeperiod=interval) / 100.0 # 47 CORREL if indicator == 'CORREL': df["CORREL_" + str(interval)] = talib.CORREL( df["High"], df["Low"], timeperiod=interval) return df.dropna()
def momentum_process(event): print(event.widget.get()) momentum = event.widget.get() upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) fig, axes = plt.subplots(2, 1, sharex=True) ax1, ax2 = axes[0], axes[1] axes[0].plot(close, 'rd-', markersize=3) axes[0].plot(upperband, 'y-') axes[0].plot(middleband, 'b-') axes[0].plot(lowerband, 'y-') axes[0].set_title(momentum, fontproperties="SimHei") if momentum == '绝对价格振荡器': real = ta.APO(close, fastperiod=12, slowperiod=26, matype=0) axes[1].plot(real, 'r-') elif momentum == '钱德动量摆动指标': real = ta.CMO(close, timeperiod=14) axes[1].plot(real, 'r-') elif momentum == '移动平均收敛/散度': macd, macdsignal, macdhist = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) axes[1].plot(macd, 'r-') axes[1].plot(macdsignal, 'g-') axes[1].plot(macdhist, 'b-') elif momentum == '带可控MA类型的MACD': macd, macdsignal, macdhist = ta.MACDEXT(close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) axes[1].plot(macd, 'r-') axes[1].plot(macdsignal, 'g-') axes[1].plot(macdhist, 'b-') elif momentum == '移动平均收敛/散度 固定 12/26': macd, macdsignal, macdhist = ta.MACDFIX(close, signalperiod=9) axes[1].plot(macd, 'r-') axes[1].plot(macdsignal, 'g-') axes[1].plot(macdhist, 'b-') elif momentum == '动量': real = ta.MOM(close, timeperiod=10) axes[1].plot(real, 'r-') elif momentum == '比例价格振荡器': real = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0) axes[1].plot(real, 'r-') elif momentum == '变化率': real = ta.ROC(close, timeperiod=10) axes[1].plot(real, 'r-') elif momentum == '变化率百分比': real = ta.ROCP(close, timeperiod=10) axes[1].plot(real, 'r-') elif momentum == '变化率的比率': real = ta.ROCR(close, timeperiod=10) axes[1].plot(real, 'r-') elif momentum == '变化率的比率100倍': real = ta.ROCR100(close, timeperiod=10) axes[1].plot(real, 'r-') elif momentum == '相对强弱指数': real = ta.RSI(close, timeperiod=14) axes[1].plot(real, 'r-') elif momentum == '随机相对强弱指标': fastk, fastd = ta.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) axes[1].plot(fastk, 'r-') axes[1].plot(fastd, 'r-') elif momentum == '三重光滑EMA的日变化率': real = ta.TRIX(close, timeperiod=30) axes[1].plot(real, 'r-') plt.show()
def momentum(np_array): if check_np_array(np_array): return talib.MOM(np_array)
b_upper, b_middle, b_lower = ta.BBANDS(close) #bollinger ban ma = ta.MA(close, timeperiod=predict_len) #moving average dmi = ta.DX(high, low, close, timeperiod=predict_len) #direct movement index macd, macdsignal, macdhist = ta.MACD( close, fastperiod=predict_len, slowperiod=predict_len * 2, signalperiod=4) #moving average convergence/divergence slowk, slowd = ta.STOCH(high, low, close, fastk_period=predict_len, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) #Stochastic mom = ta.MOM(close, timeperiod=predict_len) rsi = ta.RSI(close, timeperiod=predict_len * 2) #Relative Strength Index cci = ta.CCI(high, low, close, timeperiod=predict_len * 2) sma = ta.SMA(close, timeperiod=predict_len) #Simple Moving average wma = ta.WMA(close, timeperiod=predict_len) MLP_x = np.c_[close, ma, b_upper, b_middle, b_lower, macd, macdsignal, macdhist, dmi, cci, sma, wma] / 10000 CNN_x = np.c_[ma, b_upper, b_middle, b_lower, dmi, cci, sma, wma, mom, close] RNN_x = np.c_[b_upper, b_middle, b_lower, rsi, sma, wma] / 100000 #MLP restore======================= M_X = tf.placeholder(tf.float32, [None, M_input_n], name="M_X") #10개의 값이 10일치 가 1-set M_Y = tf.placeholder(tf.float32, [None, 1])
openv = numpy.array(openIn) volume = numpy.array(volumeIn) adx = talib.ADX(high, low, close) adxr = talib.ADXR(high, low, close) apo = talib.APO(close) aroondown, aroonup = talib.AROON(high, low) aroonosc = talib.AROONOSC(high, low) bop = talib.BOP(openv, high, low, close) cci = talib.CCI(high, low, close) cmo = talib.CMO(close) dx = talib.DX(high, low, close) macd, macdsignal, macdhist = talib.MACD(close) mfi = talib.MFI(high, low, close, volume) minus_di = talib.MINUS_DI(high, low, close) #minus_dm=talib.MINUS_DM(high, low, close) momentum = talib.MOM(close) plus_di = talib.PLUS_DI(high, low, close) #plus_dm=talib.PLUS_DM(high,low,close) ppo = talib.PPO(close) roc = talib.ROC(close) rocp = talib.ROCP(close) rocr = talib.ROCR(close) rocr100 = talib.ROCR100(close) rsi = talib.RSI(close, 10) slowk, slowd = talib.STOCH(high, low, close) fastk, fastd = talib.STOCHF(high, low, close) fastkrsi, fastdrsi = talib.STOCHRSI(close) trix = talib.TRIX(close) ultosc = talib.ULTOSC(high, low, close) willr = talib.WILLR(high, low, close)
diretorio=os.getcwd() dataset=pd.read_csv(diretorio+"/"+"BTC"+str(dia)+".csv",sep=";",index_col="datetime") dataset.tail(2) # # # indicadores dataset["h-l"]=dataset["high"]-dataset["low"] dataset["o-c"]=dataset["close"]-dataset["open"] dataset["3day_ma"]=dataset["close"].shift(1).rolling(window=3).mean() dataset["10day_ma"]=dataset["close"].shift(1).rolling(window=10).mean() dataset["30day_ma"]=dataset["close"].shift(1).rolling(window=30).mean() dataset['ema1'] = ta.EMA(dataset.close,timeperiod = 5) #exponential moving average with time_ema1 dataset['ema2'] = ta.EMA(dataset.close,timeperiod = 18) #exponential moving average with time_ema2 dataset['macd'],dataset['macdsignal'],dataset['macdhist']=ta.MACD(dataset.close, fastperiod=12,slowperiod=26,signalperiod=9) dataset['mom']=ta.MOM(dataset.close, timeperiod=6) dataset["apo"]=ta.APO(dataset.close,fastperiod=6,slowperiod=18,matype=0) dataset["rsi"]=ta.RSI(dataset["close"].values,timeperiod=9) dataset['upperband'],dataset['middleband'],dataset['lowerband']=ta.BBANDS(dataset.close, timeperiod=5,nbdevup=2,nbdevdn=2,matype=0) dataset["williams"]=ta.WILLR(dataset["high"].values, dataset["low"].values, dataset["close"].values,7) # # Vamos criar sinais de compra e venda com base em indicadores tecnicos usados no mercado ## RSI dataset['rsi_s']=0 dataset.loc[(dataset['rsi'] >= 60),'rsi_s']=-1 dataset.loc[(dataset['rsi'] <= 40),'rsi_s']=1 ## MACDhist dataset["macd_s"]=0
def add_factor_of_price(df: pd.DataFrame, ohlcav_col_name_list, drop=False, log_av=True): open_key = ohlcav_col_name_list[0] high_key = ohlcav_col_name_list[1] low_key = ohlcav_col_name_list[2] close_key = ohlcav_col_name_list[3] amount_key = ohlcav_col_name_list[4] volume_key = ohlcav_col_name_list[5] open_s = df[open_key] high_s = df[high_key] low_s = df[low_key] close_s = df[close_key] amount_s = df[amount_key] volume_s = df[volume_key] # 平均成交价格 deal_price_s = amount_s / volume_s deal_price_s[volume_s.isna()] = ( (open_s * 2 + high_s + low_s + close_s * 2) / 6)[volume_s.isna()] df[f'deal_price'] = deal_price_s # 均线因子 df[f'rr'] = close_s.to_returns() for n in [5, 10, 15, 20, 30, 60]: df[f'ma{n}'] = close_s.rolling(n).mean() # 波动率因子 expanding = close_s.expanding(5) df[f'volatility_all'] = expanding.std() / expanding.mean() for n in [20, 60]: df[f'volatility{n}'] = close_s.rolling(n).std() / close_s.rolling( n).mean() # 收益率方差 rr = close_s.to_returns() for n in [20, 60]: df[f'rr_std{n}'] = rr.rolling(n).std() #累积/派发线(Accumulation / Distribution Line,该指标将每日的成交量通过价格加权累计, #用以计算成交量的动量。属于趋势型因子 df['AD'] = talib.AD(high_s, low_s, close_s, volume_s) # 佳庆指标(Chaikin Oscillator),该指标基于AD曲线的指数移动均线而计算得到。属于趋势型因子 df['ADOSC'] = talib.ADOSC(high_s, low_s, close_s, volume_s, fastperiod=3, slowperiod=10) # 平均动向指数,DMI因子的构成部分。属于趋势型因子 df['ADX'] = talib.ADX(high_s, low_s, close_s, timeperiod=14) # 相对平均动向指数,DMI因子的构成部分。属于趋势型因子 df['ADXR'] = talib.ADXR(high_s, low_s, close_s, timeperiod=14) # 绝对价格振荡指数 df['APO'] = talib.APO(close_s, fastperiod=12, slowperiod=26) # Aroon通过计算自价格达到近期最高值和最低值以来所经过的期间数, # 帮助投资者预测证券价格从趋势到区域区域或反转的变化, # Aroon指标分为Aroon、AroonUp和AroonDown3个具体指标。属于趋势型因子 df['AROONDown'], df['AROONUp'] = talib.AROON(high_s, low_s, timeperiod=14) df['AROONOSC'] = talib.AROONOSC(high_s, low_s, timeperiod=14) # 均幅指标(Average TRUE Ranger),取一定时间周期内的股价波动幅度的移动平均值, # 是显示市场变化率的指标,主要用于研判买卖时机。属于超买超卖型因子。 for n in [6, 14]: df[f'ATR{n}'] = talib.ATR(high_s, low_s, close_s, timeperiod=n) # 布林带 df['Boll_Up'], df['Boll_Mid'], df['Boll_Down'] = \ talib.BBANDS(close_s, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0) # 均势指标 df['BOP'] = talib.BOP(open_s, high_s, low_s, close_s) # 5日顺势指标(Commodity Channel Index),专门测量股价是否已超出常态分布范围。属于超买超卖型因子。 for n in [5, 10, 20, 88]: df[f'CCI{n}'] = talib.CCI(high_s, low_s, close_s, timeperiod=5) # 钱德动量摆动指标(Chande Momentum Osciliator),与其他动量指标摆动指标如 # 相对强弱指标(RSI)和随机指标(KDJ)不同, # 钱德动量指标在计算公式的分子中采用上涨日和下跌日的数据。属于超买超卖型因子 df['CMO_Close'] = talib.CMO(close_s, timeperiod=14) df['CMO_Open'] = talib.CMO(open_s, timeperiod=14) # DEMA双指数移动平均线 for n in [6, 12, 26]: df[f'DEMA{n}'] = talib.DEMA(close_s, timeperiod=n) # DX 动向指数 df['DX'] = talib.DX(high_s, low_s, close_s, timeperiod=14) # EMA 指数移动平均线 for n in [6, 12, 26, 60]: df[f'EMA{n}'] = talib.EMA(close_s, timeperiod=n) # KAMA 适应性移动平均线 df['KAMA'] = talib.KAMA(close_s, timeperiod=30) # MACD df['MACD_DIF'], df['MACD_DEA'], df['MACD_bar'] = \ talib.MACD(close_s, fastperiod=12, slowperiod=24, signalperiod=9) # 中位数价格 不知道是什么意思 df['MEDPRICE'] = talib.MEDPRICE(high_s, low_s) # 负向指标 负向运动 df['MiNUS_DI'] = talib.MINUS_DI(high_s, low_s, close_s, timeperiod=14) df['MiNUS_DM'] = talib.MINUS_DM(high_s, low_s, timeperiod=14) # 动量指标(Momentom Index),动量指数以分析股价波动的速度为目的,研究股价在波动过程中各种加速, # 减速,惯性作用以及股价由静到动或由动转静的现象。属于趋势型因子 df['MOM'] = talib.MOM(close_s, timeperiod=10) # 归一化平均值范围 df['NATR'] = talib.NATR(high_s, low_s, close_s, timeperiod=14) # OBV 能量潮指标(On Balance Volume,OBV),以股市的成交量变化来衡量股市的推动力, # 从而研判股价的走势。属于成交量型因子 df['OBV'] = talib.OBV(close_s, volume_s) # PLUS_DI 更向指示器 df['PLUS_DI'] = talib.PLUS_DI(high_s, low_s, close_s, timeperiod=14) df['PLUS_DM'] = talib.PLUS_DM(high_s, low_s, timeperiod=14) # PPO 价格振荡百分比 df['PPO'] = talib.PPO(close_s, fastperiod=6, slowperiod=26, matype=0) # ROC 6日变动速率(Price Rate of Change),以当日的收盘价和N天前的收盘价比较, # 通过计算股价某一段时间内收盘价变动的比例,应用价格的移动比较来测量价位动量。属于超买超卖型因子。 for n in [6, 20]: df[f'ROC{n}'] = talib.ROC(close_s, timeperiod=n) # 12日量变动速率指标(Volume Rate of Change),以今天的成交量和N天前的成交量比较, # 通过计算某一段时间内成交量变动的幅度,应用成交量的移动比较来测量成交量运动趋向, # 达到事先探测成交量供需的强弱,进而分析成交量的发展趋势及其将来是否有转势的意愿, # 属于成交量的反趋向指标。属于成交量型因子 for n in [6, 20]: df[f'VROC{n}'] = talib.ROC(volume_s, timeperiod=n) # ROC 6日变动速率(Price Rate of Change),以当日的收盘价和N天前的收盘价比较, # 通过计算股价某一段时间内收盘价变动的比例,应用价格的移动比较来测量价位动量。属于超买超卖型因子。 for n in [6, 20]: df[f'ROCP{n}'] = talib.ROCP(close_s, timeperiod=n) # 12日量变动速率指标(Volume Rate of Change),以今天的成交量和N天前的成交量比较, # 通过计算某一段时间内成交量变动的幅度,应用成交量的移动比较来测量成交量运动趋向, # 达到事先探测成交量供需的强弱,进而分析成交量的发展趋势及其将来是否有转势的意愿, # 属于成交量的反趋向指标。属于成交量型因子 for n in [6, 20]: df[f'VROCP{n}'] = talib.ROCP(volume_s, timeperiod=n) # RSI df['RSI'] = talib.RSI(close_s, timeperiod=14) # SAR 抛物线转向 df['SAR'] = talib.SAR(high_s, low_s, acceleration=0.02, maximum=0.2) # TEMA for n in [6, 12, 26]: df[f'TEMA{n}'] = talib.TEMA(close_s, timeperiod=n) # TRANGE 真实范围 df['TRANGE'] = talib.TRANGE(high_s, low_s, close_s) # TYPPRICE 典型价格 df['TYPPRICE'] = talib.TYPPRICE(high_s, low_s, close_s) # TSF 时间序列预测 df['TSF'] = talib.TSF(close_s, timeperiod=14) # ULTOSC 极限振子 df['ULTOSC'] = talib.ULTOSC(high_s, low_s, close_s, timeperiod1=7, timeperiod2=14, timeperiod3=28) # 威廉指标 df['WILLR'] = talib.WILLR(high_s, low_s, close_s, timeperiod=14) # 对 volume amount 取 log if log_av: df[volume_key] = np.log(volume_s.fillna(0) + 1) df[amount_key] = np.log(amount_s.fillna(0) + 1) if drop: df.dropna(inplace=True) return df
data['WILLR'] = WILLR_usual # BOV指标 OBV_usual = talib.OBV(arrClose, arrVolume) data['OBV'] = OBV_usual # SAR指标 SAR_usual = talib.SAR(arrHigh, arrLow) data['SAR'] = SAR_usual # DEMA指标 DEMA_usual = talib.DEMA(arrClose) data['DEMA'] = DEMA_usual #MOM指标 MOM_usual = talib.MOM(arrClose) data['MOM'] = MOM_usual """ #DEMA DEMA=talib.DEMA(arrClose) data['DEMA_30min']=DEMA #WMA WMA=talib.WMA(arrClose) data['WMA_30min']=WMA #ADX ADX=talib.ADX(arrHigh,arrLow,arrClose,timeperiod=30) data['ADA_30min']=ADX #APO
def MOM(self, name, **parameters): data = self.__data[name] return talib.MOM(data, **parameters)