df = web.DataReader('BABA', 'iex', start, end) df = df.dropna() df = df.iloc[:, :4] # Calculate some technical indicator # df['MA_10'] = df['close'].rolling(window=10).mean() # df['Corr'] = df['close'].rolling(window=10).corr(df['MA_10']) # df['open-close'] = df['open'] - df['close'].shift(1) df['ADX'] = ta.ADX(np.array(df['high']), np.array(df['low']), np.array(df['close']), timeperiod=14) df['BOP'] = ta.BOP(np.array(df['open']), np.array(df['high']), np.array(df['low']), np.array(df['close'])) df['CCI'] = ta.CCI(np.array(df['high']), np.array(df['low']), np.array(df['close']), timeperiod=14) # List X and y df = df.dropna() X = df.iloc[:, :7] y = np.where((df['close'].shift(-1) > df['close']), 1, -1) # Run the Logistic Regression # Taking training set and test set to 7/3 X_train, X_test, y_train, y_test = model_selection.train_test_split( X, y.ravel(), random_state=7, shuffle=False) # LogisticRegression model = lm.LogisticRegression()
def create_features(twii): import talib sma = talib.SMA(twii, timeperiod=120) wma = talib.WMA(twii, timeperiod=120) mom = talib.MOM(twii, timeperiod=120) k, d = talib.STOCH(twii, twii, twii, fastk_period=120, slowk_period=60, slowd_period=60) k2, d2 = talib.STOCH(twii, twii, twii, fastk_period=240, slowk_period=120, slowd_period=120) k3, d3 = talib.STOCH(twii, twii, twii, fastk_period=360, slowk_period=180, slowd_period=180) k4, d4 = talib.STOCH(twii, twii, twii, fastk_period=480, slowk_period=240, slowd_period=240) k5, d5 = talib.STOCH(twii, twii, twii, fastk_period=640, slowk_period=320, slowd_period=320) k6, d6 = talib.STOCH(twii, twii, twii, fastk_period=720, slowk_period=360, slowd_period=360) k7, d7 = talib.STOCH(twii, twii, twii, fastk_period=840, slowk_period=420, slowd_period=420) k8, d8 = talib.STOCH(twii, twii, twii, fastk_period=960, slowk_period=480, slowd_period=480) rsi = talib.RSI(twii, timeperiod=120) rsi2 = talib.RSI(twii, timeperiod=240) rsi3 = talib.RSI(twii, timeperiod=480) rsi4 = talib.RSI(twii, timeperiod=640) rsi5 = talib.RSI(twii, timeperiod=720) rsi6 = talib.RSI(twii, timeperiod=840) macd1, macd2, macd3 = talib.MACD(twii, fastperiod=120, slowperiod=60, signalperiod=60) willr = talib.WILLR(twii, twii, twii, timeperiod=120) cci = talib.CCI(twii, twii, twii, timeperiod=120) return pd.DataFrame({ 'RSIb': rsi / 50, 'RSIb2': rsi2 / 50, 'RSIb3': rsi3 / 50, 'RSIb4': rsi4 / 50, 'RSIb5': rsi5 / 50, 'RSIb6': rsi6 / 50, 'MOMb': mom - 0, 'KDb': k - d, 'KDb2': k2 - d2, 'KDb3': k3 - d3, 'KDb4': k4 - d4, 'KDb5': k5 - d5, 'KDb6': k6 - d6, 'KDb7': k7 - d7, 'KDb8': k8 - d8, 'a5': (twii.rolling(5).mean() / twii), 'a10': (twii.rolling(10).mean() / twii), 'a20': (twii.rolling(20).mean() / twii), 'a40': (twii.rolling(40).mean() / twii), 'a80': (twii.rolling(80).mean() / twii), 'a160': (twii.rolling(160).mean() / twii), 'a320': (twii.rolling(320).mean() / twii), 'a640': (twii.rolling(640).mean() / twii), 'a720': (twii.rolling(720).mean() / twii), 'a840': (twii.rolling(840).mean() / twii), 'a960': (twii.rolling(960).mean() / twii), 'a1024': (twii.rolling(1024).mean() / twii), 'b1': twii / twii.shift(50), 'b2': twii / twii.shift(100), 'b3': twii / twii.shift(150), 'b4': twii / twii.shift(200), 'b5': twii / twii.shift(250), 'b6': twii / twii.shift(300), 'b7': twii / twii.shift(350), 'LINEARREG_SLOPE0': talib.LINEARREG_SLOPE(twii, 60), 'LINEARREG_SLOPE1': talib.LINEARREG_SLOPE(twii, 120), 'ADXR0': talib.ADXR(twii, twii, twii, 60), 'ADXR1': talib.ADXR(twii, twii, twii, 120), 'ADXR2': talib.ADXR(twii, twii, twii, 240), 'ADXR3': talib.ADXR(twii, twii, twii, 360), 'ADXR4': talib.ADXR(twii, twii, twii, 480), 'ADXR5': talib.ADXR(twii, twii, twii, 640), })
def CCI(High, Low, Close, timeperiod=14): cci = pd.DataFrame() for i in High.columns: cci[i] = ta.CCI(High[i], Low[i], Close[i], timeperiod) return cci
def get_cci(close, high, low, period=14, max_period=10): cci = talib.CCI(np.array(high), np.array(low), np.array(close), period) return cci[max_period:]
def parse(code,start_date='0'): df = pd.read_sql('select * from day_k_data where code="'+code+'" order by date asc;',engine) buy_price = {} buy_date = {} sell_price = {} sell_date = {} is_buy = {} rate = {} profit = {} profit['based'] = {} ma5 = talib.MA(df['close'].values,5) ma10 = talib.MA(df['close'].values,10) ma20 = talib.MA(df['close'].values,20) ma30 = talib.MA(df['close'].values,30) K, D = talib.STOCH(df['high'].values,df['low'].values,df['close'].values, fastk_period=9, slowk_period=3) J = K * 3 - D * 2 atr = talib.ATR(df['high'].values,df['low'].values,df['close'].values) natr = talib.NATR(df['high'].values,df['low'].values,df['close'].values) trange = talib.TRANGE(df['high'].values,df['low'].values,df['close'].values) cci = talib.CCI(df['high'].values,df['low'].values,df['close'].values,14) dif, dea, bar = talib.MACDFIX(df['close'].values) bar = bar * 2 close = df['close'] start_date_open = 0 for idx in range(len(ma10)): if df.date[idx] < start_date: continue ma10_buy = 1 ma10_sell = 0 bar_buy = 0 bar_sell = 0 cci_buy = 1 cci_sell = 0 dif_buy = 1 dif_sell = 0 K_buy = 1 K_sell = 0 close_val = close[idx] ma10_val = ma10[idx] ma20_val = ma20[idx] ma5_val = ma5[idx] cci_val = cci[idx] dif_val = dif[idx] dea_val = dea[idx] bar_val = bar[idx] K_val = K[idx] D_val = D[idx] J_val = J[idx] if start_date_open == 0: start_date_open = close_val if idx>=1: bar_val_y = bar[idx-1] dif_val_y = dif[idx-1] dea_val_y = dea[idx-1] cci_val_y = cci[idx-1] K_val_y = K[idx-1] if df.date[idx-1] not in profit['based']: profit['based'][df.date[idx-1]] = 1.0 profit['based'][df.date[idx]] = profit['based'][df.date[idx-1]] * close[idx] / close[idx-1] else: bar_val_y = 0 dif_val_y = 0 dea_val_y = 0 cci_val_y = 0 K_val_y = 0 if close_val > ma10_val: ma10_buy = 1 if bar_val>0 and bar_val>bar_val_y: bar_buy = 1 print 'bar_buy' if cci_val>=100 and cci_val>cci_val_y: cci_buy = 1 if dif_val>dea_val and dif_val>dif_val_y: dif_buy = 1 if K_val>D_val and K_val>K_val_y: K_buy = 1 if close_val<ma10_val: ma10_sell = 0 if bar_val<0 and bar_val<bar_val_y: bar_sell = 1 if cci_val<=100 and cci_val<cci_val_y: cci_sell = 0 if dif_val<dea_val and dif_val<dif_val_y: dif_sell = 0 if K_val<D_val and K_val<K_val_y: K_sell = 0 s_type = '11111' if s_type not in buy_price: buy_price[s_type] = [] buy_date[s_type] = [] sell_price[s_type] = [] sell_date[s_type] = [] is_buy[s_type] = 0 if ma10_buy * bar_buy * cci_buy * dif_buy * K_buy: if is_buy[s_type] == 0: is_buy[s_type] = 1 buy_price[s_type].append(close_val) buy_date[s_type].append(df.date[idx]) continue if ma10_sell or bar_sell or cci_sell or dif_sell or K_sell: if is_buy[s_type] == 1: is_buy[s_type] = 0 sell_price[s_type].append(close_val) sell_date[s_type].append(df.date[idx]) rate['based'] = close[len(close)-1] * (1 - 0.002) / start_date_open for s_type in sell_price: rate[s_type] = 1.0 profit[s_type] = {} for i in range(len(buy_price[s_type])): try: rate[s_type] = rate[s_type] * (sell_price[s_type][i] * (1 - 0.002) / buy_price[s_type][i]) profit[s_type][sell_date[s_type][i]] = rate[s_type] print s_type,"buy date : %s, buy price : %.2f, sell date : %s, sell price: %.2f" %(buy_date[s_type][i], buy_price[s_type][i], sell_date[s_type][i], sell_price[s_type][i]) except: rate[s_type] = rate[s_type] * (close[len(close)-1] * (1 - 0.002) / buy_price[s_type][len(buy_price[s_type])-1]) rate = sorted(rate.items(),key=lambda x:x[1],reverse=True) return rate,profit
def commodity_channel_index(high, low, close, n): try: cci_data = talib.CCI(high, low, close, timeperiod=n) return cci_data except Exception as e: raise (e)
def cci(df, n): real = talib.CCI(df['high'][(-n - 1):], df['low'][(-n - 1):], df['close'][(-n - 1):], timeperiod=n) df['CCI' + str(n)][-1] = real[-1]
def __get_quotas(self): #stock_amount = cral_CNstock_order_ana.main() greeks = GetDf('MOMO',db_file='../stock_option_data.db') greeks = greeks.greeks greeks['15IV'] = greeks['15IV'].apply(float) greeks['24IV'] = greeks['24IV'].apply(float) #volume = self.__df['volume'].values close = self.__df['close'] high_prices = self.__df['high'].values low_prices = self.__df['low'].values close_prices = close.values ma5 = talib.MA(close_prices,5) ma10 = talib.MA(close_prices,10) ma20 = talib.MA(close_prices,20) ma30 = talib.MA(close_prices,30) K, D = talib.STOCH(high_prices,low_prices,close_prices, fastk_period=9, slowk_period=3) J = K * 3 - D * 2 sar = talib.SAR(high_prices, low_prices, acceleration=0.05, maximum=0.2) sar = pd.DataFrame(sar-close) sar.index = self.__df.index atr = talib.ATR(high_prices,low_prices,close_prices) natr = talib.NATR(high_prices,low_prices,close_prices) trange = talib.TRANGE(high_prices,low_prices,close_prices) cci = talib.CCI(high_prices,low_prices,close_prices,14) dif, dea, bar = talib.MACDFIX(close_prices) bar = bar * 2 #ma5_volume = talib.MA(volume,5) #ma10_volume = talib.MA(volume,10) #ma20_volume = talib.MA(volume,20) #ma30_volume = talib.MA(volume,30) #dif_volume, dea_volume, bar_volume = talib.MACDFIX(volume) #bar_volume = bar_volume * 2 df_all = self.__df[['close','volume','high','low']] df_all.insert(0,'ma5',ma5) df_all.insert(0,'ma10',ma10) df_all.insert(0,'ma20',ma20) df_all.insert(0,'ma30',ma30) df_all.insert(0,'K',K) df_all.insert(0,'D',D) df_all.insert(0,'J',J) df_all.insert(0,'cci',cci) df_all.insert(0,'bar',bar) df_all.insert(0,'dif',dif) df_all.insert(0,'dea',dea) df_all.insert(0,'sar',sar) #df_all.insert(0,'ma5_volume',ma5_volume) #df_all.insert(0,'ma10_volume',ma10_volume) #df_all.insert(0,'ma20_volume',ma20_volume) #df_all.insert(0,'ma30_volume',ma30_volume) #df_all.insert(0,'bar_volume',bar_volume) #df_all.insert(0,'dif_volume',dif_volume) #df_all.insert(0,'dea_volume',dea_volume) #df_all = pd.concat([df_all,stock_amount],axis=1) df_all = pd.concat([df_all,greeks],axis=1) df_yesterday = df_all.T index_c = df_all.index added = [np.nan] * len(df_all.columns) df_yesterday.insert(0, len(df_yesterday.columns), added) df_yesterday = df_yesterday.T df_yesterday = df_yesterday.drop(df_all.index[len(df_all.index)-1]) df_yesterday.insert(0, 'index_c', index_c) df_yesterday = df_yesterday.set_index('index_c') df_dif = df_all - df_yesterday df_dif_close_plus_one_day = df_dif.copy() for i in range(len(df_dif_close_plus_one_day['close'])-1): df_dif_close_plus_one_day['close'][i] = df_dif_close_plus_one_day['close'][i+1] df_dif_close_plus_one_day['close'][len(df_dif_close_plus_one_day['close'])-1] = np.nan df_dif = df_dif.dropna(axis=0,how='any') df_dif_close_plus_one_day = df_dif_close_plus_one_day.dropna(axis=0,how='any') return df_dif, df_dif_close_plus_one_day,df_all
def test__cci(self): me = ta_cci(DF_TEST)[-100:] ta = talib.CCI(DF_TEST["High"], DF_TEST["Low"], DF_TEST["Close"])[-100:] np.testing.assert_array_almost_equal(me, ta / 100)
def set_cci(self): self.cci = talib.CCI(np.array(self.data['High'], dtype='f8'), np.array(self.data['Low'], dtype='f8'), np.array(self.data['Close'], dtype='f8'), timeperiod=14)
def enhance_with_indicators(data): set = [] OPEN = data[:, Candle.OPEN] HIGH = data[:, Candle.HIGH] LOW = data[:, Candle.LOW] CLOSE = data[:, Candle.CLOSE] VOLUME = data[:, Candle.VOLUME] low_high = talib.BBANDS(CLOSE, timeperiod=14, nbdevup=2, nbdevdn=2, matype=1) low_high = np.asarray([low_high[0][-1], low_high[2][-1]]).reshape(-1, 1) low_high_scaler = StandardScaler() low_high_scaler.fit(low_high) one = np.asarray([-1, 1]).reshape(-1, 1) one_scaler = StandardScaler() one_scaler.fit(one) hundred = np.asarray([-100, 100]).reshape(-1, 1) hundred_scaler = StandardScaler() hundred_scaler.fit(hundred) thousand = np.asarray([-1000, 1000]).reshape(-1, 1) thousand_scaler = StandardScaler() thousand_scaler.fit(thousand) million = np.asarray([-1000000, 1000000]).reshape(-1, 1) million_scaler = StandardScaler() million_scaler.fit(million) set.append(scale(OPEN, low_high_scaler)) set.append(scale(HIGH, low_high_scaler)) set.append(scale(LOW, low_high_scaler)) set.append(scale(CLOSE, low_high_scaler)) #Bollinger Bands are envelopes plotted at a standard deviation level above and below a simple moving average of the price. set.append( scale( talib.BBANDS(CLOSE, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0), low_high_scaler)) #121.03399999999903 19719.281591268886 set.append( scale( talib.BBANDS(CLOSE, timeperiod=14, nbdevup=2, nbdevdn=2, matype=1), low_high_scaler)) #The DEMA uses two exponential moving averages (EMAs) to eliminate lag, as some traders view lag as a problem. set.append(scale(talib.DEMA(CLOSE, timeperiod=10), low_high_scaler)) #122.0 19573.564771355504 set.append(scale(talib.DEMA(CLOSE, timeperiod=21), low_high_scaler)) #122.0 19546.76082510694 set.append(scale(talib.DEMA(CLOSE, timeperiod=100), low_high_scaler)) #123.84710711425136 19578.715808186673 #However, whereas SMA simply calculates an average of price data, EMA applies more weight to data that is more current. set.append(scale(talib.EMA(CLOSE, timeperiod=10), low_high_scaler)) #122.0 19499.362560116417 set.append(scale(talib.EMA(CLOSE, timeperiod=21), low_high_scaler)) #122.0 19433.26416788178 set.append(scale(talib.EMA(CLOSE, timeperiod=100), low_high_scaler)) #122.11270000000005 19059.124645340504 #The HTTrendline at a specific bar gives the current Hilbert Transform Trendline as instantaneously measured at that #bar. In its Series form, the Instantaneous Trendline appears much like a Moving Average, but with minimal lag #compared with the lag normally associated with such averages for equivalent periods. set.append(scale(talib.HT_TRENDLINE(CLOSE), low_high_scaler)) #122.0 19471.324 #Kaufman's Adaptive Moving Average (KAMA) is a moving average designed to account for market noise or volatility. #KAMA will closely follow prices when the price swings are relatively small and the noise is low. set.append(scale(talib.KAMA(CLOSE, timeperiod=10), low_high_scaler)) #122.0 19397.611724437047 set.append(scale(talib.KAMA(CLOSE, timeperiod=21), low_high_scaler)) #122.0 19336.434082122203 set.append(scale(talib.KAMA(CLOSE, timeperiod=100), low_high_scaler)) #123.61 19301.746826077375 #The MESA adaptive moving average is a trend-following indicator. It adapts to price movements in a very unique way, #based on the rate of change (ROC), as measured by the Hilbert Transform Discriminator. set.append( scale(talib.MAMA((HIGH + LOW) / 2., fastlimit=0.5, slowlimit=0.05), low_high_scaler)) #121.04112572694972 19494.294994956996 set.append(scale(talib.MIDPOINT(CLOSE, timeperiod=5), low_high_scaler)) #122.0 19544.95 set.append(scale(talib.MIDPRICE(LOW, HIGH, timeperiod=5), low_high_scaler)) #122.0 19562.6 #The parabolic SAR indicator, developed by J. Welles Wilder Jr., is used by traders to determine trend direction # and potential reversals in price. set.append( scale(talib.SAR(HIGH, LOW, acceleration=0.02, maximum=0.2), low_high_scaler)) #122.0 19660.0 #A simple moving average (SMA) is an arithmetic moving average calculated by adding recent prices and then dividing # that figure by the number of time periods in the calculation average. set.append(scale(talib.SMA(CLOSE, timeperiod=5), low_high_scaler)) #122.0 19553.340000000037 set.append(scale(talib.SMA(CLOSE, timeperiod=25), low_high_scaler)) #122.0 19405.74400000004 set.append(scale(talib.SMA(CLOSE, timeperiod=50), low_high_scaler)) #122.0 19286.443999999996 #The Triple Exponential Moving Average (T3) developed by Tim Tillson attempts to offer a moving average with better #smoothing then traditional exponential moving average. It incorporates a smoothing technique which allows it to #plot curves more gradual than ordinary moving averages and with a smaller lag. set.append(scale(talib.T3(CLOSE, timeperiod=5, vfactor=0), low_high_scaler)) #122.0 19498.31237177043 set.append( scale(talib.T3(CLOSE, timeperiod=10, vfactor=0), low_high_scaler)) #122.0 19419.991324685387 set.append( scale(talib.T3(CLOSE, timeperiod=21, vfactor=0), low_high_scaler)) #122.84310194419339 19306.63501695168 #The triple exponential moving average was designed to smooth price fluctuations, thereby making it easier to #identify trends without the lag associated with traditional moving averages (MA). set.append(scale(talib.TEMA(CLOSE, timeperiod=7), low_high_scaler)) #122.0 19617.222402494965 set.append(scale(talib.TEMA(CLOSE, timeperiod=15), low_high_scaler)) #122.0 19586.42515855386 #The Triangular Moving Average is basically a double-smoothed Simple Moving Average that gives more weight to the #middle section of the data interval. The TMA has a significant lag to current prices and is not well-suited to #fast moving markets. set.append(scale(talib.TRIMA(CLOSE, timeperiod=5), low_high_scaler)) #122.0 19567.31111092877 set.append(scale(talib.TRIMA(CLOSE, timeperiod=25), low_high_scaler)) #122.0 19459.8816568341 set.append(scale(talib.TRIMA(CLOSE, timeperiod=50), low_high_scaler)) #122.0 19359.257076923175 #The weighted moving average (WMA) is a technical indicator that assigns a greater weighting to the most recent data #points, and less weighting to data points in the distant past. The WMA is obtained by multiplying each number in #the data set by a predetermined weight and summing up the resulting values. set.append(scale(talib.WMA(CLOSE, timeperiod=5), low_high_scaler)) #122.0 19567.840000466134 set.append(scale(talib.WMA(CLOSE, timeperiod=10), low_high_scaler)) #122.0 19527.127272724356 set.append(scale(talib.WMA(CLOSE, timeperiod=21), low_high_scaler)) #122.0 19479.342424127473 set.append(scale(talib.WMA(CLOSE, timeperiod=50), low_high_scaler)) #122.0 19355.600000135404 set.append(scale(talib.WMA(CLOSE, timeperiod=100), low_high_scaler)) #122.21647326732675 19265.66566335264 set.append(scale(talib.LINEARREG(CLOSE, timeperiod=14), low_high_scaler)) # 122.0 19585.157142857144 set.append( scale(talib.LINEARREG_INTERCEPT(CLOSE, timeperiod=14), low_high_scaler)) #121.54000000000003 19643.968571428577 set.append(scale(talib.TSF(CLOSE, timeperiod=14), low_high_scaler)) #122.0 19609.668131868133 #ADX stands for Average Directional Movement Index and can be used to help measure the overall strength of a trend. #The ADX indicator is an average of expanding price range values. set.append( scale(talib.ADX(HIGH, LOW, CLOSE, timeperiod=10), hundred_scaler)) #0.0 99.99999999999963 set.append( scale(talib.ADX(HIGH, LOW, CLOSE, timeperiod=14), hundred_scaler)) #0.0 99.9999999940751 set.append( scale(talib.ADX(HIGH, LOW, CLOSE, timeperiod=21), hundred_scaler)) #0.0 99.99998408446837 #ADXR stands for Average Directional Movement Index Rating. ADXR is a component of the Directional Movement System #developed by Welles Wilder. set.append( scale(talib.ADXR(HIGH, LOW, CLOSE, timeperiod=14), hundred_scaler)) #0.0 99.9999999892742 #The Aroon indicator is a technical indicator that is used to identify trend changes in the price of an asset, #as well as the strength of that trend. In essence, the indicator measures the time between highs and the time #between lows over a time period. ... The indicator signals when this is happening, and when it isn't set.append(scale(talib.AROON(HIGH, LOW), hundred_scaler)) #0.0 100.0 #The Directional Movement Index, or DMI, is an indicator developed by J. ... An optional third line, called #directional movement (DX) shows the difference between the lines. When +DI is above -DI, there is more upward #pressure than downward pressure in the price. set.append(scale(talib.DX(HIGH, LOW, CLOSE, timeperiod=5), hundred_scaler)) #0.0 100.0 set.append(scale(talib.DX(HIGH, LOW, CLOSE, timeperiod=21), hundred_scaler)) #0.0 100.0 set.append(scale(talib.DX(HIGH, LOW, CLOSE, timeperiod=50), hundred_scaler)) #0.0 100.0 set.append( scale(talib.MFI(HIGH, LOW, CLOSE, VOLUME, timeperiod=14), hundred_scaler)) #-5.888410733172162e-08 100.00000000707982 set.append( scale(talib.MFI(HIGH, LOW, CLOSE, VOLUME, timeperiod=26), hundred_scaler)) #-1.3802451942902055e-09 100.00000001185656 set.append( scale(talib.MFI(HIGH, LOW, CLOSE, VOLUME, timeperiod=100), hundred_scaler)) #-5.3901183535126315e-08 100.00000000091877 set.append( scale(talib.MINUS_DI(HIGH, LOW, CLOSE, timeperiod=14), hundred_scaler)) #0.0 99.99999996020233 set.append( scale(talib.PLUS_DI(HIGH, LOW, CLOSE, timeperiod=14), hundred_scaler)) #0.0 100.0 set.append(scale(talib.RSI(CLOSE, timeperiod=14), hundred_scaler)) #0.0 100.0 set.append( scale( talib.STOCH(HIGH, LOW, CLOSE, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0), hundred_scaler)) #-1.0137076363510762e-12 100.00000000000279 set.append( scale( talib.STOCHF(HIGH, LOW, CLOSE, fastk_period=5, fastd_period=3, fastd_matype=0), hundred_scaler)) #-1.0137076363510762e-12 100.0000000000012 set.append( scale( talib.STOCHRSI(CLOSE, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0), hundred_scaler)) #-1.2166564052525548e-12 100.00000000000011 set.append( scale( talib.ULTOSC(HIGH, LOW, CLOSE, timeperiod1=7, timeperiod2=14, timeperiod3=28), hundred_scaler)) # 0.0 100.00000032481957 set.append( scale(talib.CDL3WHITESOLDIERS(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append(scale(talib.CDLDOJI(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLDRAGONFLYDOJI(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLGRAVESTONEDOJI(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append(scale(talib.CDLHAMMER(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLHOMINGPIGEON(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLINVERTEDHAMMER(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLLADDERBOTTOM(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLLONGLEGGEDDOJI(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLMATCHINGLOW(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLMORNINGDOJISTAR(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLMORNINGSTAR(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append(scale(talib.CDLPIERCING(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLRICKSHAWMAN(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLSTICKSANDWICH(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append(scale(talib.CDLTAKURI(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 set.append( scale(talib.CDLUNIQUE3RIVER(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #0 100 #Absolute Price Oscillator crossing above zero is considered bullish, while crossing below zero is bearish. A #positive indicator value indicates an upward movement, while negative readings signal a downward trend. set.append( scale(talib.APO(CLOSE, fastperiod=12, slowperiod=26, matype=1), thousand_scaler)) #-536.1910463572985 443.13971636041424 set.append(scale(VOLUME, thousand_scaler)) #The Commodity Channel Index (CCI) measures the current price level relative to an average price level over a given #period of time. CCI is relatively high when prices are far above their average. set.append(scale(talib.CCI(HIGH, LOW, CLOSE), thousand_scaler)) #-466.66671166042244 466.66673951370416 #Moving average convergence divergence (MACD) is a trend-following momentum indicator that shows the relationship #between two moving averages of a security’s price. set.append( scale(talib.MACD(CLOSE, fastperiod=5, slowperiod=14, signalperiod=5), thousand_scaler)) # -536.1910463572985 443.13971636041424 set.append( scale(talib.MACD(CLOSE, fastperiod=12, slowperiod=26, signalperiod=9), thousand_scaler)) #-536.1910463572985 443.13971636041424 set.append( scale(talib.MACD(CLOSE, fastperiod=14, slowperiod=50, signalperiod=25), thousand_scaler)) # -536.1910463572985 443.13971636041424 set.append(scale(talib.ATR(HIGH, LOW, CLOSE), thousand_scaler)) #0.0 672.1715311610562 set.append(scale(talib.HT_DCPHASE(CLOSE), hundred_scaler)) #-44.99298332517037 314.99654478107004 set.append( scale(talib.LINEARREG_SLOPE(CLOSE, timeperiod=14), hundred_scaler)) #-222.33604395604476 152.37032967033085 set.append( scale(talib.STDDEV(CLOSE, timeperiod=5, nbdev=1), thousand_scaler)) #0.0 709.4023698851089 set.append(scale(talib.MINUS_DM(HIGH, LOW, timeperiod=14), thousand_scaler)) #0.0 2909.3760999618785 set.append(scale(talib.MOM(CLOSE, timeperiod=10), thousand_scaler)) #-2508.0 1711.2000000000007 set.append(scale(talib.MOM(CLOSE, timeperiod=25), thousand_scaler)) # -2508.0 1711.2000000000007 set.append(scale(talib.PLUS_DM(HIGH, LOW, timeperiod=14), thousand_scaler)) #0.0 3697.0008310558483 set.append(scale(talib.ADOSC(HIGH, LOW, CLOSE, VOLUME), thousand_scaler)) #-1843.4418435977714 1237.4131984846026 set.append(scale(talib.TRANGE(HIGH, LOW, CLOSE), thousand_scaler)) #0.0 4000.0 set.append(scale(talib.HT_PHASOR(CLOSE), thousand_scaler)) #-2873.977625168652 3422.2535328187428 #The Balance of Power indicator measures the market strength of buyers against sellers by assessing the ability of #each side to drive prices to an extreme level. The calculation is: Balance of Power = (Close price – Open price) / #(High price – Low price) The resulting value can be smoothed by a moving average. set.append(scale(talib.BOP(OPEN, HIGH, LOW, CLOSE), one_scaler)) #-1.0 1.0 set.append(scale(talib.ROCP(CLOSE, timeperiod=10), one_scaler)) #-0.30688987999999995 0.46745909457773854 set.append(scale(talib.ROCR(CLOSE, timeperiod=10), one_scaler)) #0.69311012 1.4674590945777386 set.append(scale(talib.TRIX(CLOSE, timeperiod=30), one_scaler)) #-0.6044429731575707 0.434667877456385 set.append(scale(talib.HT_SINE(CLOSE), one_scaler)) #-0.9999999999996187 0.9999999999940317 set.append(scale(talib.HT_TRENDMODE(CLOSE), one_scaler)) #0 1 set.append( scale(talib.PPO(CLOSE, fastperiod=12, slowperiod=26, matype=0), hundred_scaler)) #-13.640389725420714 13.383459677599681 set.append(scale(talib.ROC(CLOSE, timeperiod=10), hundred_scaler)) #-30.688987999999995 46.74590945777386 set.append(scale(talib.NATR(HIGH, LOW, CLOSE), one_scaler)) #0.0 7.881777549670427 set.append(scale(talib.HT_DCPERIOD(CLOSE), hundred_scaler)) #6.91050087362864 49.99951053223339 set.append(scale(talib.CORREL(HIGH, LOW, timeperiod=30), one_scaler)) #-2.4748737341529163 2.2135943621178655 set.append(scale(talib.AD(HIGH, LOW, CLOSE, VOLUME), million_scaler)) #-3719.2404462314116 199644.25743563366 set.append(scale(talib.OBV(CLOSE, VOLUME), million_scaler)) #-23849.75116020021 29139.83770172981 set.append(scale(talib.BETA(HIGH, LOW, timeperiod=5), million_scaler)) #-2971957.111723269 1250567.1172035346 set.append( scale(talib.VAR(CLOSE, timeperiod=5, nbdev=1), million_scaler)) #-1.4901161193847656e-07 503251.7223986089 # The Aroon Oscillator is a trend-following indicator that uses aspects of the Aroon Indicator (Aroon Up and Aroon # Down) to gauge the strength of a current trend and the likelihood that it will continue. Readings above zero # indicate that an uptrend is present, while readings below zero indicate that a downtrend is present. set.append(scale(talib.AROONOSC(HIGH, LOW), hundred_scaler)) # -100.0 100.0 # The Chande Momentum Oscillator (CMO) is a technical momentum indicator developed by Tushar Chande. The CMO # indicator is created by calculating the difference between the sum of all recent higher closes and the sum of all # recent lower closes and then dividing the result by the sum of all price movement over a given time period. set.append(scale(talib.CMO(CLOSE, timeperiod=5), hundred_scaler)) # -100.0 100.0 set.append(scale(talib.CMO(CLOSE, timeperiod=14), hundred_scaler)) # -99.99999998652726 100.0 set.append(scale(talib.CMO(CLOSE, timeperiod=25), hundred_scaler)) # -99.99854211548185 100.0 set.append(scale(talib.CDL3INSIDE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDL3LINESTRIKE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDL3OUTSIDE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLABANDONEDBABY(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLBELTHOLD(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLBREAKAWAY(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLCLOSINGMARUBOZU(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLCOUNTERATTACK(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLDOJISTAR(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLENGULFING(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLGAPSIDESIDEWHITE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLHARAMI(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLHARAMICROSS(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLHIGHWAVE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLKICKING(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLKICKINGBYLENGTH(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLLONGLINE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLMARUBOZU(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLRISEFALL3METHODS(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLSEPARATINGLINES(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLSHORTLINE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLSPINNINGTOP(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLTASUKIGAP(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append(scale(talib.CDLTRISTAR(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.CDLXSIDEGAP3METHODS(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 100 set.append( scale(talib.LINEARREG_ANGLE(CLOSE, timeperiod=14), hundred_scaler)) #-89.74230272272693 89.62397563202859 set.append( scale(talib.WILLR(HIGH, LOW, CLOSE, timeperiod=14), hundred_scaler)) # -100.00000000000001 0.0 #The Two Crows is a three-line bearish reversal candlestick pattern. The pattern requires confirmation, that is, #the following candles should break a trendline or the nearest support area which may be formed by the first #candle's line. If the pattern is not confirmed it may act only as a temporary pause within an uptrend. set.append(scale(talib.CDL2CROWS(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDL3BLACKCROWS(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLADVANCEBLOCK(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) # -100 0 set.append( scale(talib.CDLDARKCLOUDCOVER(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLEVENINGDOJISTAR(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLEVENINGSTAR(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLHANGINGMAN(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLIDENTICAL3CROWS(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append(scale(talib.CDLINNECK(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append(scale(talib.CDLONNECK(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLSHOOTINGSTAR(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLSTALLEDPATTERN(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append( scale(talib.CDLTHRUSTING(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-100 0 set.append(scale(talib.CDLHIKKAKE(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-200 200 set.append( scale(talib.CDLHIKKAKEMOD(OPEN, HIGH, LOW, CLOSE), hundred_scaler)) #-200 200 return np.concatenate(set)
def generate_tech_data(stock, open_name, close_name, high_name, low_name, max_time_window=10): open_price = stock[open_name].values close_price = stock[close_name].values low_price = stock[low_name].values high_price = stock[high_name].values data = stock.copy() data['MOM'] = talib.MOM(close_price, timeperiod=max_time_window) data['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price) data['HT_DCPHASE'] = talib.HT_DCPHASE(close_price) data['sine'], data['leadsine'] = talib.HT_SINE(close_price) data['inphase'], data['quadrature'] = talib.HT_PHASOR(close_price) data['ADXR'] = talib.ADXR(high_price, low_price, close_price, timeperiod=max_time_window) data['APO'] = talib.APO(close_price, fastperiod=max_time_window // 2, slowperiod=max_time_window) data['AROON_UP'], _ = talib.AROON(high_price, low_price, timeperiod=max_time_window) data['CCI'] = talib.CCI(high_price, low_price, close_price, timeperiod=max_time_window) data['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price, timeperiod=max_time_window) data['PPO'] = talib.PPO(close_price, fastperiod=max_time_window // 2, slowperiod=max_time_window) data['macd'], data['macd_sig'], data['macd_hist'] = talib.MACD( close_price, fastperiod=max_time_window // 2, slowperiod=max_time_window, signalperiod=max_time_window // 2) data['CMO'] = talib.CMO(close_price, timeperiod=max_time_window) data['ROCP'] = talib.ROCP(close_price, timeperiod=max_time_window) data['fastk'], data['fastd'] = talib.STOCHF(high_price, low_price, close_price) data['TRIX'] = talib.TRIX(close_price, timeperiod=max_time_window) data['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price, timeperiod1=max_time_window // 2, timeperiod2=max_time_window, timeperiod3=max_time_window * 2) data['WILLR'] = talib.WILLR(high_price, low_price, close_price, timeperiod=max_time_window) data['NATR'] = talib.NATR(high_price, low_price, close_price, timeperiod=max_time_window) data = data.drop([open_name, close_name, high_name, low_name], axis=1) data = data.dropna().astype(np.float32) return data
df['CDLSEPARATINGLINES'] = talib.CDLSEPARATINGLINES(open, high, low, close) df['CDLSHOOTINGSTAR'] = talib.CDLSHOOTINGSTAR(open, high, low, close) df['CDLSHORTLINE'] = talib.CDLSHORTLINE(open, high, low, close) df['CDLSPINNINGTOP'] = talib.CDLSPINNINGTOP(open, high, low, close) df['CDLSTALLEDPATTERN'] = talib.CDLSTALLEDPATTERN(open, high, low, close) df['CDLUPSIDEGAP2CROWS'] = talib.CDLUPSIDEGAP2CROWS(open, high, low, close) df['CDLXSIDEGAP3METHODS'] = talib.CDLXSIDEGAP3METHODS(open, high, low, close) df['CDLUNIQUE3RIVER'] = talib.CDLUNIQUE3RIVER(open, high, low, close) df['ADOSC'] = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10) df['OBV'] = talib.OBV(close, volume) df['CCI'] = talib.CCI(high, low, close, timeperiod=14) df['ROC'] = talib.ROC(close, timeperiod=10) df['ROCR'] = talib.ROCR(close, timeperiod=10) df['HT_DCPERIOD'] = talib.HT_DCPERIOD(close) df['ADXR'] = talib.ADXR(np.array(high), np.array(low), np.array(close), timeperiod=14) df['BOP'] = talib.BOP(np.array(open), np.array(high), np.array(low), np.array(close)) df['CMO'] = talib.CMO(np.array(close), timeperiod=14) df['DX'] = talib.DX(np.array(high), np.array(low), np.array(close), timeperiod=14) #21 df['MFI'] = talib.MFI(np.array(high),
def technical_indicators_panda_dataframe(df_price): open = df_price['Open'] high = df_price['High'] low = df_price['Low'] close = df_price['Close'] volume = df_price['Volume'] ## Commoditty Channel Index cci = tb.CCI(high, low, close, timeperiod=20) ## Rate of change roc = tb.ROC(close, timeperiod=21) ## Momentum Indicators Functions #Aroon aroondown, aroonup = tb.AROON(high, low, timeperiod=14) #Average Directional Movement Index adx = tb.ADX(high, low, close, timeperiod=14) di_plus = tb.PLUS_DI(high, low, close, timeperiod=14) di_minus = tb.MINUS_DI(high, low, close, timeperiod=14) #Money Flow Index mfi = tb.MFI(high, low, close, volume, timeperiod=14) #Relative Strength Index rsi = tb.RSI(close, timeperiod=14) #Stochastic slowk, slowd = tb.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) #STOCHF - Stochastic Fast fastk, fastd = tb.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) #Moving Average Convergence/Divergence macd, macdema, macdhist = tb.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) ## Overlap Studies Functions #Bollinger Bands upperband, middleband, lowerband = tb.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) #Weighted Moving Average wma5 = tb.WMA(close, timeperiod=5) wma30 = tb.WMA(close, timeperiod=30) #Exponential Moving Average ema5 = tb.EMA(close, timeperiod=5) ema21 = tb.EMA(close, timeperiod=21) #Double Exponential Moving Average dema5 = tb.DEMA(close, timeperiod=5) dema21 = tb.DEMA(close, timeperiod=21) #Triple Exponential Moving Average tema = tb.TEMA(close, timeperiod=30) #Triangular Moving Average trima5 = tb.DEMA(close, timeperiod=5) trima30 = tb.TRIMA(close, timeperiod=30) ## Volume indicators Functions #AD - Chaikin A/D Line chaikin = tb.AD(high, low, close, volume) ##=> MERGE kwargs = {"cci":cci, "roc": roc, "aroondown": aroondown, "aroonup": aroonup, "adx": adx, "di_plus":di_plus, "di_minus":di_minus, "mfi": mfi, "rsi": rsi, "slowk": slowk, "slowd": slowd, "fastk": fastk, "fastd": fastd, "macd": macd, "macdema": macdema, "macdhist": macdhist,\ "upperband": upperband , "middleband": middleband , "lowerband": lowerband, "wma5" : wma5, "wma30" : wma30, "ema5" : ema5, "ema21" : ema21, "dema21": dema21, "dema5": dema5, "tema": tema, "trima5": trima5,"trima30": trima30,\ "chaikin": chaikin} return df_price.assign(**kwargs)
def indicator(filename): database = pd.read_csv(filename) close = np.array(database.close) high = np.array(database.high) low = np.array(database.low) volume = np.array(database.volume) o = np.array(database.open) #简单区分其到底处于什么区间内 Add('OPEN', o) Add('HIGH', high) Add('LOW', low) Add('CLOSE', close) Add('VOLUME', volume) upperband, middleband, lowerband = talib.BBANDS(close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0) Length = len(upperband) increase = [] for i in range(0, Length): if math.isnan(upperband[i]): increase.append(np.nan) else: increase.append(upperband[i] - middleband[i]) Add('BBANDS', np.asarray(increase)) real = talib.DEMA(close, timeperiod=10) real1 = talib.DEMA(close, timeperiod=20) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('DEMA', real0) real = talib.EMA(close, timeperiod=5) real1 = talib.EMA(close, timeperiod=10) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('EMA', real0) #暂时不会用这个指标 real = talib.HT_TRENDLINE(close) Add('HT_TRENDLINE', real) real = talib.KAMA(close, timeperiod=30) real1 = talib.KAMA(close, timeperiod=60) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('KAMA', real0) real = talib.MA(close, timeperiod=7, matype=0) real1 = talib.MA(close, timeperiod=14, matype=0) real0 = [] for i in range(Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('MA', real0) #暂时没找到怎么去用 mama, fama = talib.MAMA(close, fastlimit=0.5, slowlimit=0.05) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(mama[i] - fama[i]) else: real0.append(np.nan) Add('MAMA', np.asarray(real0)) #没找到 real = talib.MIDPOINT(close, timeperiod=14) Add('MIDPOINT', real) #没找到 real = talib.MIDPRICE(high, low, timeperiod=14) Add('MIDPRICE', real) real = talib.SAR(high, low, acceleration=0, maximum=0) real0 = [] for i in range(0, Length): if not math.isnan(real[i]): real0.append(close[i] - real[i]) else: real0.append(np.nan) Add('SAR', real0) #暂时不会 real = talib.SAREXT(high, low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0) Add('SAREXT', real) real = talib.SMA(close, timeperiod=3) real1 = talib.SMA(close, timeperiod=5) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('SMA', real0) #暂时不懂 real = talib.T3(close, timeperiod=5, vfactor=0) Add('T3', real) real = talib.TEMA(close, timeperiod=7) real1 = talib.TEMA(close, timeperiod=14) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('TEMA', real0) real = talib.TRIMA(close, timeperiod=7) real1 = talib.TRIMA(close, timeperiod=14) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('TRIMA', real0) real = talib.WMA(close, timeperiod=7) real1 = talib.WMA(close, timeperiod=14) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('WMA', real0) #ADX与ADXR的关系需要注意一下 real = talib.ADX(high, low, close, timeperiod=14) Add('ADX', real) real = talib.ADXR(high, low, close, timeperiod=14) Add('ADXR', real) #12个和26个简单移动平均线的差值 real = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) Add('APO', real) ''' aroondown, aroonup = talib.AROON(high, low, timeperiod=14) real0 = [] for i in range(0,Length): if not(math.isnan(aroondown) or math.isnan(aroonup)): real0.append(aroonup[i] - aroondown[i]) else: real0.append(numpy.nan) Add('AROON',numpy.asarray(real0)) ''' #AROONOSC就是Aroonup-aroondown real = talib.AROONOSC(high, low, timeperiod=14) Add('AROONOSC', real) #不懂 real = talib.BOP(o, high, low, close) Add('BOP', real) # real = talib.CCI(high, low, close, timeperiod=14) Add('CCI', real) real = talib.CMO(close, timeperiod=14) Add('CMO', real) #需要再考虑一下,因为DX代表的市场的活跃度 real = talib.DX(high, low, close, timeperiod=14) Add('DX', real) macd, macdsignal, macdhist = talib.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) Add('MACD', macdhist) macd, macdsignal, macdhist = talib.MACDEXT(close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) Add('MACDEXT', macdhist) macd, macdsignal, macdhist = talib.MACDFIX(close, signalperiod=9) Add('MACDFIX', macdhist) real = talib.MFI(high, low, close, volume, timeperiod=14) real1 = talib.MA(real, 7) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('MFI', np.asarray(real0)) real = talib.MINUS_DI(high, low, close, timeperiod=14) real1 = talib.PLUS_DI(high, low, close, timeperiod=14) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real1[i] - real[i]) else: real0.append(np.nan) Add('PLUS_DI', np.asarray(real0)) real = talib.MINUS_DM(high, low, timeperiod=14) Add('MINUS_DM', real) #虽然大概了解了规则,但在标普500上怎么用还不是很清楚 real = talib.MOM(close, timeperiod=14) Add('MOM', real) real = talib.PLUS_DM(high, low, timeperiod=14) Add('PLUS_DM', real) #暂时不用 real = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) Add('PPO', real) real = talib.ROC(close, timeperiod=14) Add('ROC', real) real = talib.ROCP(close, timeperiod=14) Add('ROCP', real) real = talib.ROCR(close, timeperiod=14) Add('ROCR', real) real = talib.ROCR100(close, timeperiod=14) Add('ROCR100', real) real = talib.RSI(close, timeperiod=14) Add('RSI', real) slowk, slowd = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) slowj = [] for i in range(Length): if not (math.isnan(slowk[i]) or math.isnan(slowd[i])): slowj.append(3 * slowk[i] - 2 * slowd[i]) else: slowj.append(np.nan) Add('STOCH', np.asarray(slowj)) fastk, fastd = talib.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) fastj = [] for i in range(Length): if not (math.isnan(fastk[i]) or math.isnan(fastd[i])): fastj.append(3 * fastk[i] - 2 * fastd[i]) else: fastj.append(np.nan) Add('STOCHF', np.asarray(fastj)) fastk, fastd = talib.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) fastj = [] for i in range(Length): if not (math.isnan(fastk[i]) or math.isnan(fastd[i])): fastj.append(3 * fastk[i] - 2 * fastd[i]) else: fastj.append(np.nan) Add('STOCHRSI', np.asarray(fastj)) real = talib.TRIX(close, timeperiod=30) real1 = talib.MA(real, 6) real0 = [] for i in range(0, Length): if not (math.isnan(real[i] or math.isnan(real1[i]))): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('TRIX', real) real = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) Add('ULTOSC', real) real = talib.WILLR(high, low, close, timeperiod=14) real0 = [] for i in range(0, Length): if not math.isnan(real[i]): if real[i] > -20: real0.append(1.0) elif real[i] < -80: real0.append(-1.0) else: real0.append(0.0) else: real0.append(np.nan) Add('WILLR', np.asarray(real0)) real = talib.AD(high, low, close, volume) real1 = talib.MA(real, 6) real0 = [] for i in range(0, Length): if not (math.isnan(real[i]) or math.isnan(real1[i])): real0.append(real[i] - real1[i]) else: real0.append(np.nan) Add('AD', np.asarray(real0)) real = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10) Add('ADOSC', real) #对于每个指标的处理还是很有问题的呀 real = talib.OBV(close, volume) Add('OBV', real) real = talib.ATR(high, low, close, timeperiod=14) Add('ATR', real) real = talib.NATR(high, low, close, timeperiod=14) Add('NATR', real) real = talib.TRANGE(high, low, close) Add('TRANGE', real) integer = talib.HT_TRENDMODE(close) Add('HT_TRENDMODE', integer) real = talib.LINEARREG_SLOPE(close, timeperiod=14) Add('LINEARREG_SLOPE', real) real = talib.STDDEV(close, timeperiod=5, nbdev=1) Add('STDDEV', real) real = talib.TSF(close, timeperiod=14) Add('TSF', real) real = talib.VAR(close, timeperiod=5, nbdev=1) Add('VAR', real) real = talib.MEDPRICE(high, low) Add('MEDPRICE', real) real = talib.TYPPRICE(high, low, close) Add('TYPPRICE', real) real = talib.WCLPRICE(high, low, close) Add('WCLPRICE', real) real = talib.DIV(high, low) Add('DIV', real) real = talib.MAX(close, timeperiod=30) Add('MAX', real) real = talib.MIN(close, timeperiod=30) Add('MIN', real) real = talib.SUB(high, low) Add('SUB', real) real = talib.SUM(close, timeperiod=30) Add('SUM', real) return [dict1, dict2]
def CCI(high, low, close, timeperiod=14): # COMMODITY CHANNEL INDEX real_ = ta.CCI(high, low, close, timeperiod) return real_
def getIndi(tiker='BTC'): df = pd.read_csv("csv/" + tiker + "_ohlcv.csv") cci_ = talib.CCI(df.high, df.low, df.close, 14) cci_signal = talib.MA(cci_, 9, 0) mfi_ = talib.MFI(df.high, df.low, df.close, df.volume, 14) obv_ = talib.OBV(df.close, df.volume) obv_signal = talib.MA(obv_, 9, 0) adx_ = talib.ADX(df.high, df.low, df.close, 14) di_pl = talib.PLUS_DI(df.high, df.low, df.close, 14) di_mi = talib.MINUS_DI(df.high, df.low, df.close, 14) sar_ = talib.SAR(df.high, df.low, 0.02, 0.2) upperband, middleband, lowerband = talib.BBANDS(df.close, 7, 2, 2, 0) bandwidth = 100.0 * (upperband - lowerband) / middleband df['band'] = 0.0 for i in range(0, len(bandwidth.index)): if bandwidth[i] < 5: df['band'][i] = 0.0 continue df['band'][i] = bandwidth[i] df['cci'] = 0 chk = 0 for i in range(1, len(cci_.index)): if cci_[i] == cci_[i] and cci_signal[i] == cci_signal[i]: if cci_[i - 1] <= cci_signal[i - 1] and cci_[i] >= cci_signal[i]: if cci_[i] <= -100: chk = 8 elif cci_[i] <= 0: chk = 7 elif cci_[i] <= 100: chk = 6 else: chk = 5 elif cci_[i - 1] >= cci_signal[i - 1] and cci_[i] <= cci_signal[i]: if cci_[i] >= 100: chk = 1 elif cci_[i] >= 0: chk = 2 elif cci_[i] >= -100: chk = 3 else: chk = 4 df['cci'][i] = chk df['adx'] = 0 chk1 = 0 chk2 = 0 chk3 = 0 chk4 = 0 chk5 = 0 chk6 = 0 max_ = 0 min_ = 0 for i in range(1, len(df.index)): if di_pl[i] == di_pl[i] and di_mi[i] == di_mi[i] and adx_[i] == adx_[ i] and sar_[i] == sar_[i]: if adx_[i] >= 20: if sar_[i] >= df['close'][i]: chk1 = 0 else: chk1 = 1 else: chk1 = 2 if di_pl[i - 1] < di_mi[i - 1] and di_pl[i] > di_mi[i]: chk2 = 1 chk5 = 0 chk6 = 0 if chk3 == 0: max_ = i elif df['close'][i] >= df['high'][max_]: chk4 = 1 chk3 = 1 elif di_pl[i - 1] > di_mi[i - 1] and di_pl[i] < di_mi[i]: chk2 = 2 chk3 = 0 chk4 = 0 if chk5 == 0: min_ = i elif df['close'][i] <= df['low'][min_]: chk6 = 1 chk5 = 1 if chk1 == 0: if chk2 == 1: if chk4 == 1: df['adx'][i] = 12 else: df['adx'][i] = 11 else: if chk6 == 1: df['adx'][i] = 9 else: df['adx'][i] = 10 elif chk1 == 1: if chk2 == 1: if chk4 == 1: df['adx'][i] = 4 else: df['adx'][i] = 3 else: if chk6 == 1: df['adx'][i] = 1 else: df['adx'][i] = 2 else: if chk2 == 1: if chk4 == 1: df['adx'][i] = 8 else: df['adx'][i] = 7 else: if chk6 == 1: df['adx'][i] = 5 else: df['adx'][i] = 6 df['obv'] = 0 for i in range(len(df.index)): if obv_[i] == obv_[i] and obv_signal[i] == obv_signal[i]: df['obv'][i] = obv_[i] - obv_signal[i] df = df.drop(df.columns[[0, 1, 2, 3, 4]], axis=1) df.to_csv("csv/" + tiker + "_indi1.csv", index=False) return df
def cci(self, n, array=False): """CCI指标""" result = talib.CCI(self.high, self.low, self.close, n) if array: return result return result[-1]
def crawStock(dayNum): # ============================== 创建汇总表模板 ============================== # ============= 取出日期后缀 ============= demo1 = ts.pro_bar('600000.SH', adj='qfq', start_date=start_time2) demo = demo1.sort_values('trade_date', ascending=True) # sort demo.index = demo1.index # sort the index demoT = demo['trade_date'].T # get column names demoT = demoT[-dayNum:] # slice # print(demoT) # ============= 0 收盘率、收盘价和最高价 ============= CloseRatio = ["%CloseRatio_" + str(demoT.iloc[-1])] closeName = demoT.tolist() # df -> list closeName = ['Close_day_' + i for i in closeName] # add index label highName = demoT.tolist() # df -> list highName = ['High_day_' + i for i in highName] # add index label # ============= 1 日前复权数据 DB2 ============= dateName = demoT.tolist() # df -> list dateName = ['DB2_day_' + i for i in dateName] # add index label # ============= 2 周前复权数据 DB2 ============= demo_week1 = ts.pro_bar('600000.SH', freq='W', adj='qfq', start_date=start_time2) demo_week = demo_week1.sort_values('trade_date', ascending=True) demo_week.index = demo_week1.index demo_weekT = demo_week['trade_date'].T demo_weekT = demo_weekT[-dayNum:] weekName = demo_weekT.tolist() weekName = ['DB2_week_' + i for i in weekName] # ============= 3 MACD_hist ============= macdName = demoT.tolist() # df -> list macdName = ['MACD_' + i for i in macdName] # add index label # ============= 4 RSI ============= RSIName = demoT.tolist() # df -> list RSIName = ['RSI_' + i for i in RSIName] # add index label # ============= 5 CCI ============= CCIName = demoT.tolist() # df -> list CCIName = ['CCI_' + i for i in CCIName] # add index label # ============= 6 KDJ ============= KtName = demoT.tolist() # df -> list KtName = ['Kt_' + i for i in KtName] # add index label DtName = demoT.tolist() # df -> list DtName = ['Dt_' + i for i in DtName] # add index label JtName = demoT.tolist() # df -> list JtName = ['Jt_' + i for i in JtName] # add index label # ============= 7 企业基本数据 ============= empl = pro.stock_company() emplName = empl.columns.tolist() # demo_empl = empl[empl['ts_code'].isin(["002635.SZ"])] # print(demo_empl) # demo_empl.to_csv('./emp_info.csv', encoding="utf_8_sig", float_format='%.2f') # ============= 汇总文件前缀 ============= colsName = CloseRatio + closeName + highName + dateName + weekName \ + macdName + RSIName + CCIName \ + KtName + DtName + JtName \ + emplName conclude = pd.DataFrame(columns=colsName) print(conclude) concLen = len(conclude.columns.tolist()) # ============= 8 检测汇总表文件是否存在 ============= saveFile = savePath + "/conclusion_weighted_" + str(dayNum) + ".csv" if os.path.exists(saveFile): print("文件", saveFile, "已存在,开始续写。") else: conclude.to_csv(saveFile, encoding="utf_8_sig", float_format='%.2f') # ============= 9 记录已汇总股票指数 ============= conc = pd.read_csv(saveFile, error_bad_lines=False, engine='python') tlist = np.array(conc.iloc[:, 0].values).tolist() # print(tlist) # ============================== 开始爬取数据 ============================== for sheet in sheets: # ============= 处理工作表 ============= # 0-1 读入单张工作表 print("正在读取第", sheet, "张工作表") try: sheet_i = pd.read_excel(path, sheet_name=sheet) except: print("无法找到工作表: ", sheet, ",已跳过") continue # 0-2读入股票代码列 stockCode = sheet_i["代码"] # ============= 0-3 根据股票代码读取日指数并保存 ============= for SC in stockCode: code = str(SC) # ============= 1 补零到六位代码 ============= while len(code) < 6: code = "0" + code # 2 补全复权代码 if code[0] == '6': code = code + '.SH' else: code = code + '.SZ' # 2-2 跳过已汇总代码 if code in tlist: print("代码", code, "已在表中,跳过读取。") continue # ============= 3 读入企业信息 ============= try: # df_basics = empl.loc[code] df_basics = empl[empl['ts_code'].isin([code])].iloc[0] # print(df_basics) # print(type(df_basics)) except: print("无法找到企业代码: ", code, " 的信息") continue # print(df_basics) # 4 读入日复权指数 df1 = ts.pro_bar(code, adj='qfq', start_date='2017-01-01') # df1.to_csv('./df.csv', encoding="utf_8_sig", float_format='%.2f') # 4-1 如果数据为空 if df1 is None: print("股票代码", end='') print(code, end='') print("的查询结果为空") continue # 4-2 数据量小于21 if len(df1) < 21: print("股票代码", end='') print(code, end='') print("的记录数量小于21,不予计算 DB2") continue # 5-1 计算 DB1 和 DB2 df = df1.sort_values('trade_date', ascending=True) df.index = df1.index try: df['MIN_21'], df['MAX_21'], df['DB1'] = calculateDB1( df.high, df.low, df.close, N, N2, []) df['DB2'] = calculateDB2(df['DB1'], N, N2, []) except: print("Error code is: ", code) continue # 5-2 计算 MACD try: df["MACD_DIFF"], df["MACD_DEA"], df["MACD_macdhist"] = \ talib.MACD(df.close, fastperiod=12, slowperiod=26, signalperiod=9) except: print("MACD Error code is: ", code) continue # 5-3 计算 RSI try: df['RSI'] = talib.RSI(df.open, timeperiod=12) except: print("RSI Error code is: ", code) continue # 5-4 计算 CCI try: df["CCI"] = talib.CCI(df.high, df.low, df.close, timeperiod=14) except: print("CCI Error code is: ", code) continue # 5-5 计算 KDJ try: df['Kt'], df['Dt'] = talib.STOCH(df.high, df.low, df.close, fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df['Jt'] = list( map(lambda x, y: 3 * x - 2 * y, df['Kt'], df['Dt'])) except: print("KDJ Error code is: ", code) continue # 5-6 取出指标作为 series 并取出五天内数据 df.index = df['trade_date'] Ratio = (df['close'][-1] - df['close'][-dayNum:].min()) / df['close'][-1] * 100 closeR = pd.Series([Ratio]) # print(type(closeR)) closeR.index = ["%CloseRatio_" + str(df['trade_date'][-1])] closeRow = df['close'][-dayNum:] closeRow.index = df['trade_date'][-dayNum:].map( lambda x: "Close_day_" + str(x)).tolist() highRow = df['high'][-dayNum:] highRow.index = df['trade_date'][-dayNum:].map( lambda x: "High_day_" + str(x)).tolist() newRow = df['DB2'][-dayNum:] newRow.index = df['trade_date'][-dayNum:].map( lambda x: "DB2_day_" + str(x)).tolist() # print(type(newRow)) MACD_Row = df["MACD_macdhist"][-dayNum:] MACD_Row.index = df['trade_date'][-dayNum:].map( lambda x: "MACD_" + str(x)).tolist() RSI_Row = df['RSI'][-dayNum:] RSI_Row.index = df['trade_date'][-dayNum:].map( lambda x: "RSI_" + str(x)).tolist() CCI_Row = df['CCI'][-dayNum:] CCI_Row.index = df['trade_date'][-dayNum:].map( lambda x: "CCI_" + str(x)).tolist() Kt_Row = df['Kt'][-dayNum:] Kt_Row.index = df['trade_date'][-dayNum:].map( lambda x: "Kt_" + str(x)).tolist() Dt_Row = df['Dt'][-dayNum:] Dt_Row.index = df['trade_date'][-dayNum:].map( lambda x: "Dt_" + str(x)).tolist() Jt_Row = df['Jt'][-dayNum:] Jt_Row.index = df['trade_date'][-dayNum:].map( lambda x: "Jt_" + str(x)).tolist() # 6 读入周复权指数 df_week = pro.weekly(ts_code=code, start_date=start_time2) # 6-1 如果数据为空 if df_week is None: print("股票代码", end='') print(code, end='') print("的周数据查询结果为空") continue # 6-2 数据量小于21 if len(df_week) < 21: print("股票代码", end='') print(code, end='') print("的周数据记录数量小于21,不予计算 DB2") continue # 6-3 计算 DB1 和 DB2 df_week['MIN_21'], df_week['MAX_21'], df_week['DB1'] = \ calculateDB1(df_week.high, df_week.low, df_week.close, N, N2, []) df_week['DB2'] = calculateDB2(df_week['DB1'], N, N2, []) # 6-4 取出 DB2 作为 series 并取出五天内数据 newRow_week = df_week[-dayNum:]['DB2'] newRow_week.index = weekName # 7 将数据加入汇总表 # 7-1 合并各指数 allRows = pd.concat([ closeR, closeRow, highRow, newRow, newRow_week, MACD_Row, RSI_Row, CCI_Row, Kt_Row, Dt_Row, Jt_Row, df_basics ]) allRows.name = code # print(allRows) # 7-2 与汇总表 index 对接 newRows = conclude.append(allRows) if concLen != newRows.shape[1]: print("股票代码", code, "读取失误,跳过输出,请重新计算") continue # 7-3 写入 csv 文件 newRows.to_csv(saveFile, mode='a', header=False, encoding="utf_8_sig", float_format='%.2f') print("代码", code, "已记录") # conclude.to_csv(saveFile, encoding="utf_8_sig", float_format='%.2f') print("已将汇总表的结果保存在路径: ", saveFile)
MA = pd.concat([MA, local_MA], axis=1) MACD1, MACD2, XX = ta.MACD(SS.sclose) MACD = pd.concat([MACD1, MACD2], axis=1) ADX = ta.ADX(SS.high, SS.low, SS.sclose) ADXR = ta.ADXR(SS.high, SS.low, SS.sclose) aroondown, aroonup = ta.AROON(SS.high, SS.low) ATR = ta.ATR(SS.high, SS.low, SS.sclose) Bupper, Bmiddle, Blower = ta.BBANDS(SS.sclose) group1 = pd.concat([ SS, MA, MACD, ADX, ADXR, aroondown, aroonup, ATR, Bupper, Bmiddle, Blower ], axis=1) BOP = ta.BOP(SS.sopen, SS.high, SS.low, SS.sclose) CCI = ta.CCI(SS.high, SS.low, SS.sclose) CMO = ta.CMO(SS.sclose) DEMA = ta.DEMA(SS.sclose) DX = ta.DX(SS.high, SS.low, SS.sclose) EMA = ta.EMA(SS.sclose) KAMA = ta.KAMA(SS.sclose) MFI = ta.MFI(SS.high, SS.low, SS.sclose, SS.volumn) MOM = ta.MOM(SS.sclose) RSI = ta.RSI(SS.sclose) group2 = pd.concat([BOP, CCI, CMO, DEMA, DX, EMA, KAMA, MFI, MOM, RSI], axis=1) SAR = ta.SAR(SS.high, SS.low) TEMA = ta.TEMA(SS.sclose) TRANGE = ta.TRANGE(SS.high, SS.low, SS.sclose) TRIMA = ta.TRIMA(SS.sclose) TRIX = ta.TRIX(SS.sclose)
def ta_cci(n, k_data): cci = pd.DataFrame() cci['cci'] = ta.CCI(k_data.high, k_data.low, k_data.close, timeperiod=n) # cci['cci'] = cci.cci.rolling(window=3, min_periods=0, center=False).mean() return cci.round(2)
def factor_calc(self, inst_sym='603032.XSHG', date_time=date(2019, 9, 5), timeperiod=14, fastperiod=12, slowperiod=26, signalperiod=9): factor_csv = os.path.join(self.path_factor_, inst_sym[:6] + ".csv") ohlcvt = self.ohlcvt_data_.load_all_data(inst_sym, date_time, self.count_) datetime = ohlcvt["datetime"] high = ohlcvt["high"] close = ohlcvt["close"] low = ohlcvt["low"] open = ohlcvt["open"] volumn = ohlcvt["volume"] factor = talib.ADX(high, low, close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.DataFrame({"ADX": factor}) #data = pd.concat([data, pd.DataFrame({"ADX": factor})], axis=1) factor = talib.ADXR(high, low, close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"ADXR": factor})], axis=1) factor = talib.APO(close, fastperiod, slowperiod, matype=0) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"APO": factor})], axis=1) aroondown, aroonup = talib.AROON(high, low, timeperiod) aroondown[np.isnan(aroondown)] = 0 aroonup[np.isnan(aroonup)] = 0 data = pd.concat([data, pd.DataFrame({"aroondown": aroondown})], axis=1) data = pd.concat([data, pd.DataFrame({"aroonup": aroonup})], axis=1) factor = talib.AROONOSC(high, low, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"AROONOSC": factor})], axis=1) factor = talib.BOP(open, high, low, close) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"BOP": factor})], axis=1) factor = talib.CCI(high, low, close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"CCI": factor})], axis=1) factor = talib.CMO(close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"CMO": factor})], axis=1) factor = talib.DX(high, low, close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"DX": factor})], axis=1) [macd, macdsignal, macdhist] = talib.MACD(close, fastperiod, slowperiod, signalperiod) macdsignal[np.isnan(macdsignal)] = 0 data = pd.concat([data, pd.DataFrame({"MACD": macdsignal})], axis=1) factor = talib.MFI(high, low, close, volumn, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"MFI": factor})], axis=1) factor = talib.PPO(close, fastperiod, slowperiod, matype=0) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"PPO": factor})], axis=1) factor = talib.ROCP(close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"ROCP": factor})], axis=1) factor = talib.RSI(close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"RSI": factor})], axis=1) slowk, slowd = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) slowk[np.isnan(slowk)] = 0 slowd[np.isnan(slowd)] = 0 data = pd.concat([data, pd.DataFrame({"STOCHslowk": slowk})], axis=1) data = pd.concat([data, pd.DataFrame({"STOCHslowd": slowd})], axis=1) fastk, fastd = talib.STOCHRSI(close, timeperiod, fastk_period=5, fastd_period=3, fastd_matype=0) fastk[np.isnan(fastk)] = 0 fastd[np.isnan(fastd)] = 0 data = pd.concat([data, pd.DataFrame({"STOCHRSIfastk": fastk})], axis=1) data = pd.concat([data, pd.DataFrame({"STOCHRSIfastd": fastd})], axis=1) factor = talib.TRIX(close, timeperiod) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"TRIX": factor})], axis=1) factor = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) factor[np.isnan(factor)] = 0 data = pd.concat([data, pd.DataFrame({"TRIX": factor})], axis=1) price = pd.DataFrame(close) day1 = price.pct_change(periods=1) data1 = day1.shift(-1, axis=0) day2 = price.pct_change(periods=2) day2 = day2.shift(-2, axis=0) data1 = pd.concat([data1, day2], axis=1) day3 = price.pct_change(periods=3) day3 = day3.shift(-3, axis=0) data1 = pd.concat([data1, day3], axis=1) day4 = price.pct_change(periods=4) day4 = day4.shift(-4, axis=0) data1 = pd.concat([data1, day4], axis=1) day5 = price.pct_change(periods=5) day5 = day5.shift(-5, axis=0) data1 = pd.concat([data1, day5], axis=1) data1.columns = ["1day", "2day", "3day", "4day", "5day"] data = pd.concat([data, data1], axis=1) data[np.isnan(data)] = 0 date_index = pd.DataFrame(datetime).applymap( lambda x: pd.to_datetime(str(x), format="%Y%m%d%H%M%S")) date_index.columns = ["datetime"] data = pd.concat([date_index, data], axis=1) data.to_csv(factor_csv, index=False)
def appendAllTAData(df=pd.DataFrame([])): resDF = pd.DataFrame([]) # 函数名:AD名称:ChaikinA/DLine累积/派发线(Accumulation/DistributionLine) # 简介:MarcChaikin提出的一种平衡交易量指标,以当日的收盘价位来估算成交流量,用于估定一段时间内该证券累积的资金流量。 # 计算公式:A/D=昨日A/D+多空对比*今日成交量多空对比=[(收盘价-最低价)-(最高价-收盘价)]/(最高价-最低价) # 若最高价等于最低价:多空对比=(收盘价/昨收盘)-1 # 研判:1、A/D测量资金流向,向上的A/D表明买方占优势,而向下的A/D表明卖方占优势 # 2、A/D与价格的背离可视为买卖信号,即底背离考虑买入,顶背离考虑卖出 # 3、应当注意A/D忽略了缺口的影响,事实上,跳空缺口的意义是不能轻易忽略的 # A/D指标无需设置参数,但在应用时,可结合指标的均线进行分析例子:real=AD(high,low,close,volume) resDF['AD'] = ta.AD(df['max_price'].values, df['min_price'].values, df['price'].values, df['vol'].values) # 函数名:ADOSC名称:Chaikin A/D Oscillator Chaikin震荡指标 # 简介:将资金流动情况与价格行为相对比,检测市场中资金流入和流出的情况 # 计算公式:fastperiod A/D - slowperiod A/D # 研判:1、交易信号是背离:看涨背离做多,看跌背离做空 # 2、股价与90天移动平均结合,与其他指标结合 # 3、由正变负卖出,由负变正买进 # 例子:real = ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10) resDF['ADOSC'] = ta.ADOSC(df['max_price'].values, df['min_price'].values, df['price'].values, df['vol'].values, fastperiod=3, slowperiod=10) resDF['ADX'] = ta.ADX(df['max_price'].values, df['min_price'].values, df['price'].values) resDF['ADXR'] = ta.ADXR(df['max_price'].values, df['min_price'].values, df['price'].values, timeperiod=14) resDF['APO'] = ta.APO(df['price'].values, fastperiod=12, slowperiod=26, matype=0) resDF['aroondown'], resDF['aroonup'] = ta.AROON(df['max_price'].values, df['min_price'].values, timeperiod=14) resDF['AROONOSC'] = ta.AROONOSC(df['max_price'].values, df['min_price'].values, timeperiod=14) resDF['ATR'] = ta.ATR(df['max_price'].values, df['min_price'].values, df['price'].values, timeperiod=14) resDF['AVGPRICE'] = ta.AVGPRICE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) resDF['upperband'], resDF['middleband'], resDF['lowerband'] = ta.BBANDS( df['price'].values, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) resDF['BETA'] = ta.BETA(df['max_price'].values, df['min_price'].values, timeperiod=5) resDF['BOP'] = ta.BOP(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) resDF['CCI'] = ta.CCI(df['max_price'].values, df['min_price'].values, df['price'].values, timeperiod=10)[-1] # 函数名:CDL2CROWS名称:Two Crows 两只乌鸦 # 简介:三日K线模式,第一天长阳,第二天高开收阴,第三天再次高开继续收阴,收盘比前一日收盘价低,预示股价下跌。 # 例子:integer = CDL2CROWS(open, high, low, close) resDF['CDL2CROWS'] = ta.CDL2CROWS(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDL3BLACKCROWS名称:Three Black Crows 三只乌鸦 # 简介:三日K线模式,连续三根阴线,每日收盘价都下跌且接近最低价,每日开盘价都在上根K线实体内,预示股价下跌。 # 例子:integer = CD3BLACKCROWS(open, high, low, close) resDF['CDL3BLACKCROWS'] = ta.CDL3BLACKCROWS(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDL3INSIDE名称: Three Inside Up/Down 三内部上涨和下跌 # 简介:三日K线模式,母子信号+长K线,以三内部上涨为例,K线为阴阳阳,第三天收盘价高于第一天开盘价,第二天K线在第一天K线内部,预示着股价上涨。 # 例子:integer = CDL3INSIDE(open, high, low, close) resDF['CDL3INSIDE'] = ta.CDL3INSIDE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDL3LINESTRIKE名称: Three-Line Strike 三线打击 # 简介:四日K线模式,前三根阳线,每日收盘价都比前一日高,开盘价在前一日实体内,第四日市场高开,收盘价低于第一日开盘价,预示股价下跌。 # 例子:integer = CDL3LINESTRIKE(open, high, low, close) resDF['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDL3OUTSIDE名称:Three Outside Up/Down 三外部上涨和下跌 # 简介:三日K线模式,与三内部上涨和下跌类似,K线为阴阳阳,但第一日与第二日的K线形态相反,以三外部上涨为例,第一日K线在第二日K线内部,预示着股价上涨。 # 例子:integer = CDL3OUTSIDE(open, high, low, close) resDF['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDL3STARSINSOUTH名称:Three Stars In The South 南方三星 # 简介:三日K线模式,与大敌当前相反,三日K线皆阴,第一日有长下影线,第二日与第一日类似,K线整体小于第一日,第三日无下影线实体信号,成交价格都在第一日振幅之内,预示下跌趋势反转,股价上升。 # 例子:integer = CDL3STARSINSOUTH(open, high, low, close) resDF['CDL3STARSINSOUTH'] = ta.CDL3STARSINSOUTH( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDL3WHITESOLDIERS名称:Three Advancing White Soldiers 三个白兵 # 简介:三日K线模式,三日K线皆阳,每日收盘价变高且接近最高价,开盘价在前一日实体上半部,预示股价上升。 # 例子:integer = CDL3WHITESOLDIERS(open, high, low, close) resDF['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLABANDONEDBABY名称:Abandoned Baby 弃婴 # 简介:三日K线模式,第二日价格跳空且收十字星(开盘价与收盘价接近,最高价最低价相差不大),预示趋势反转,发生在顶部下跌,底部上涨。 # 例子:integer = CDLABANDONEDBABY(open, high, low, close, penetration=0) resDF['CDLABANDONEDBABY'] = ta.CDLABANDONEDBABY( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, penetration=0) # 函数名:CDLADVANCEBLOCK名称:Advance Block 大敌当前 # 简介:三日K线模式,三日都收阳,每日收盘价都比前一日高,开盘价都在前一日实体以内,实体变短,上影线变长。 # 例子:integer = CDLADVANCEBLOCK(open, high, low, close) resDF['CDLADVANCEBLOCK'] = ta.CDLADVANCEBLOCK( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLBELTHOLD名称:Belt-hold 捉腰带线 # 简介:两日K线模式,下跌趋势中,第一日阴线,第二日开盘价为最低价,阳线,收盘价接近最高价,预示价格上涨。 # 例子:integer = CDLBELTHOLD(open, high, low, close) resDF['CDLBELTHOLD'] = ta.CDLBELTHOLD(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLBREAKAWAY名称:Breakaway 脱离 # 简介:五日K线模式,以看涨脱离为例,下跌趋势中,第一日长阴线,第二日跳空阴线,延续趋势开始震荡,第五日长阳线,收盘价在第一天收盘价与第二天开盘价之间,预示价格上涨。 # 例子:integer = CDLBREAKAWAY(open, high, low, close) resDF['CDLBREAKAWAY'] = ta.CDLBREAKAWAY(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名: CDLCLOSINGMARUBOZU 名称:Closing Marubozu 收盘缺影线 # 简介:一日K线模式,以阳线为例,最低价低于开盘价,收盘价等于最高价,预示着趋势持续。 # 例子:integer = CDLCLOSINGMARUBOZU(open, high, low, close) resDF['CDLCLOSINGMARUBOZU'] = ta.CDLCLOSINGMARUBOZU( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLCONCEALBABYSWALL名称: Concealing Baby Swallow 藏婴吞没 # 简介:四日K线模式,下跌趋势中,前两日阴线无影线,第二日开盘、收盘价皆低于第二日,第三日倒锤头,第四日开盘价高于前一日最高价,收盘价低于前一日最低价,预示着底部反转。 # 例子:integer = CDLCONCEALBABYSWALL(open, high, low, close) resDF['CDLCONCEALBABYSWALL'] = ta.CDLCONCEALBABYSWALL( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLCOUNTERATTACK # 名称:Counterattack 反击线 # 简介:二日K线模式,与分离线类似。 # 例子:integer = CDLCOUNTERATTACK(open, high, low, close) resDF['CDLCOUNTERATTACK'] = ta.CDLCOUNTERATTACK( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLDARKCLOUDCOVER名称:Dark Cloud Cover 乌云压顶 # 简介:二日K线模式,第一日长阳,第二日开盘价高于前一日最高价,收盘价处于前一日实体中部以下,预示着股价下跌。 # 例子:integer = CDLDARKCLOUDCOVER(open, high, low, close, penetration=0) resDF['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, penetration=0) # 函数名: CDLDOJI # 名称:Doji 十字 # 简介:一日K线模式,开盘价与收盘价基本相同。 # 例子:integer = CDLDOJI(open, high, low, close) resDF['CDLDOJI'] = ta.CDLDOJI(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名: CDLDOJISTAR # 名称:Doji Star 十字星 # 简介:一日K线模式,开盘价与收盘价基本相同,上下影线不会很长,预示着当前趋势反转。 # 例子:integer = CDLDOJISTAR(open, high, low, close) resDF['CDLDOJISTAR'] = ta.CDLDOJISTAR(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLDRAGONFLYDOJI名称:Dragonfly Doji 蜻蜓十字/T形十字 # 简介:一日K线模式,开盘后价格一路走低,之后收复,收盘价与开盘价相同,预示趋势反转。 # 例子:integer = CDLDRAGONFLYDOJI(open, high, low, close) resDF['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLENGULFING名称:Engulfing Pattern 吞噬模式 # 简介:两日K线模式,分多头吞噬和空头吞噬,以多头吞噬为例,第一日为阴线,第二日阳线,第一日的开盘价和收盘价在第二日开盘价收盘价之内,但不能完全相同。 # 例子:integer = CDLENGULFING(open, high, low, close) resDF['CDLENGULFING'] = ta.CDLENGULFING(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLEVENINGDOJISTAR名称:Evening Doji Star 十字暮星 # 简介:三日K线模式,基本模式为暮星,第二日收盘价和开盘价相同,预示顶部反转。 # 例子:integer = CDLEVENINGDOJISTAR(open, high, low, close, penetration=0) resDF['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, penetration=0) # 函数名:CDLEVENINGSTAR名称:Evening Star 暮星 # 简介:三日K线模式,与晨星相反,上升趋势中,第一日阳线,第二日价格振幅较小,第三日阴线,预示顶部反转。 # 例子:integer = CDLEVENINGSTAR(open, high, low, close, penetration=0) resDF['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, penetration=0) # 函数名:CDLGAPSIDESIDEWHITE名称:Up/Down-gap side-by-side white lines 向上/下跳空并列阳线 # 简介:二日K线模式,上升趋势向上跳空,下跌趋势向下跳空,第一日与第二日有相同开盘价,实体长度差不多,则趋势持续。 # 例子:integer = CDLGAPSIDESIDEWHITE(open, high, low, close) resDF['CDLGAPSIDESIDEWHITE'] = ta.CDLGAPSIDESIDEWHITE( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLGRAVESTONEDOJI名称:Gravestone Doji 墓碑十字/倒T十字 # 简介:一日K线模式,开盘价与收盘价相同,上影线长,无下影线,预示底部反转。 # 例子:integer = CDLGRAVESTONEDOJI(open, high, low, close) resDF['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHAMMER # 名称:Hammer 锤头 # 简介:一日K线模式,实体较短,无上影线,下影线大于实体长度两倍,处于下跌趋势底部,预示反转。 # 例子:integer = CDLHAMMER(open, high, low, close) resDF['CDLHAMMER'] = ta.CDLHAMMER(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHANGINGMAN # 名称:Hanging Man 上吊线 # 简介:一日K线模式,形状与锤子类似,处于上升趋势的顶部,预示着趋势反转。 # 例子:integer = CDLHANGINGMAN(open, high, low, close) resDF['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHARAMI名称:Harami Pattern 母子线 # 简介:二日K线模式,分多头母子与空头母子,两者相反,以多头母子为例,在下跌趋势中,第一日K线长阴,第二日开盘价收盘价在第一日价格振幅之内,为阳线,预示趋势反转,股价上升。 # 例子:integer = CDLHARAMI(open, high, low, close) resDF['CDLHARAMI'] = ta.CDLHARAMI(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHARAMICROSS名称:Harami Cross Pattern 十字孕线 # 简介:二日K线模式,与母子县类似,若第二日K线是十字线,便称为十字孕线,预示着趋势反转。 # 例子:integer = CDLHARAMICROSS(open, high, low, close) resDF['CDLHARAMICROSS'] = ta.CDLHARAMICROSS(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHIGHWAVE # 名称:High-Wave Candle 风高浪大线 # 简介:三日K线模式,具有极长的上/下影线与短的实体,预示着趋势反转。 # 例子:integer = CDLHIGHWAVE(open, high, low, close) resDF['CDLHIGHWAVE'] = ta.CDLHIGHWAVE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHIKKAKE名称:Hikkake Pattern 陷阱 # 简介:三日K线模式,与母子类似,第二日价格在前一日实体范围内,第三日收盘价高于前两日,反转失败,趋势继续。 # 例子:integer = CDLHIKKAKE(open, high, low, close) resDF['CDLHIKKAKE'] = ta.CDLHIKKAKE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHIKKAKEMOD名称:Modified Hikkake Pattern 修正陷阱 # 简介:三日K线模式,与陷阱类似,上升趋势中,第三日跳空高开;下跌趋势中,第三日跳空低开,反转失败,趋势继续。 # 例子:integer = CDLHIKKAKEMOD(open, high, low, close) resDF['CDLHIKKAKEMOD'] = ta.CDLHIKKAKEMOD(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLHOMINGPIGEON名称:Homing Pigeon 家鸽 # 简介:二日K线模式,与母子线类似,不同的的是二日K线颜色相同,第二日最高价、最低价都在第一日实体之内,预示着趋势反转。 # 例子:integer = CDLHOMINGPIGEON(open, high, low, close) resDF['CDLHOMINGPIGEON'] = ta.CDLHOMINGPIGEON( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLIDENTICAL3CROWS名称:Identical Three Crows 三胞胎乌鸦 # 简介:三日K线模式,上涨趋势中,三日都为阴线,长度大致相等,每日开盘价等于前一日收盘价,收盘价接近当日最低价,预示价格下跌。 # 例子:integer = CDLIDENTICAL3CROWS(open, high, low, close) resDF['CDLIDENTICAL3CROWS'] = ta.CDLIDENTICAL3CROWS( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLINNECK名称:In-Neck Pattern 颈内线 # 简介:二日K线模式,下跌趋势中,第一日长阴线,第二日开盘价较低,收盘价略高于第一日收盘价,阳线,实体较短,预示着下跌继续。 # 例子:integer = CDLINNECK(open, high, low, close) resDF['CDLINNECK'] = ta.CDLINNECK(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLINVERTEDHAMMER名称:Inverted Hammer 倒锤头 # 简介:一日K线模式,上影线较长,长度为实体2倍以上,无下影线,在下跌趋势底部,预示着趋势反转。 # 例子:integer = CDLINVERTEDHAMMER(open, high, low, close) resDF['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLKICKING # 名称:Kicking 反冲形态 # 简介:二日K线模式,与分离线类似,两日K线为秃线,颜色相反,存在跳空缺口。 # 例子:integer = CDLKICKING(open, high, low, close) resDF['CDLKICKING'] = ta.CDLKICKING(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLKICKINGBYLENGTH名称:Kicking - bull/bear determined by the longer marubozu 由较长缺影线决定的反冲形态 # 简介:二日K线模式,与反冲形态类似,较长缺影线决定价格的涨跌。 # 例子:integer = CDLKICKINGBYLENGTH(open, high, low, close) resDF['CDLKICKINGBYLENGTH'] = ta.CDLKICKINGBYLENGTH( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLLADDERBOTTOM名称:Ladder Bottom 梯底 # 简介:五日K线模式,下跌趋势中,前三日阴线,开盘价与收盘价皆低于前一日开盘、收盘价,第四日倒锤头,第五日开盘价高于前一日开盘价,阳线,收盘价高于前几日价格振幅,预示着底部反转。 # 例子:integer = CDLLADDERBOTTOM(open, high, low, close) resDF['CDLLADDERBOTTOM'] = ta.CDLLADDERBOTTOM( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLLONGLEGGEDDOJI名称:Long Legged Doji 长脚十字 # 简介:一日K线模式,开盘价与收盘价相同居当日价格中部,上下影线长,表达市场不确定性。 # 例子:integer = CDLLONGLEGGEDDOJI(open, high, low, close) resDF['CDLLONGLEGGEDDOJI'] = ta.CDLLONGLEGGEDDOJI( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLLONGLINE # 名称:Long Line Candle 长蜡烛 # 简介:一日K线模式,K线实体长,无上下影线。 # 例子:integer = CDLLONGLINE(open, high, low, close) resDF['CDLLONGLINE'] = ta.CDLLONGLINE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLMARUBOZU # 名称:Marubozu 光头光脚/缺影线 # 简介:一日K线模式,上下两头都没有影线的实体,阴线预示着熊市持续或者牛市反转,阳线相反。 # 例子:integer = CDLMARUBOZU(open, high, low, close) resDF['CDLMARUBOZU'] = ta.CDLMARUBOZU(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLMATCHINGLOW名称:Matching Low 相同低价 # 简介:二日K线模式,下跌趋势中,第一日长阴线,第二日阴线,收盘价与前一日相同,预示底部确认,该价格为支撑位。 # 例子:integer = CDLMATCHINGLOW(open, high, low, close) resDF['CDLMATCHINGLOW'] = ta.CDLMATCHINGLOW(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLMATHOLD名称:Mat Hold 铺垫 # 简介:五日K线模式,上涨趋势中,第一日阳线,第二日跳空高开影线,第三、四日短实体影线,第五日阳线,收盘价高于前四日,预示趋势持续。 # 例子:integer = CDLMATHOLD(open, high, low, close, penetration=0) resDF['CDLMATHOLD'] = ta.CDLMATHOLD(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, penetration=0) # 函数名:CDLMORNINGDOJISTAR名称:Morning Doji Star 十字晨星 # 简介:三日K线模式,基本模式为晨星,第二日K线为十字星,预示底部反转。 # 例子:integer = CDLMORNINGDOJISTAR(open, high, low, close, penetration=0) resDF['CDLMORNINGDOJISTAR'] = ta.CDLMORNINGDOJISTAR( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, penetration=0) # 函数名:CDLMORNINGSTAR名称:Morning Star 晨星 # 简介:三日K线模式,下跌趋势,第一日阴线,第二日价格振幅较小,第三天阳线,预示底部反转。 # 例子:integer = CDLMORNINGSTAR(open, high, low, close, penetration=0) resDF['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, penetration=0) # 函数名:CDLONNECK名称:On-Neck Pattern 颈上线 # 简介:二日K线模式,下跌趋势中,第一日长阴线,第二日开盘价较低,收盘价与前一日最低价相同,阳线,实体较短,预示着延续下跌趋势。 # 例子:integer = CDLONNECK(open, high, low, close) resDF['CDLONNECK'] = ta.CDLONNECK(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLPIERCING名称:Piercing Pattern 刺透形态 # 简介:两日K线模式,下跌趋势中,第一日阴线,第二日收盘价低于前一日最低价,收盘价处在第一日实体上部,预示着底部反转。 # 例子:integer = CDLPIERCING(open, high, low, close) resDF['CDLPIERCING'] = ta.CDLPIERCING(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLRICKSHAWMAN名称:Rickshaw Man 黄包车夫 # 简介:一日K线模式,与长腿十字线类似,若实体正好处于价格振幅中点,称为黄包车夫。 # 例子:integer = CDLRICKSHAWMAN(open, high, low, close) resDF['CDLRICKSHAWMAN'] = ta.CDLRICKSHAWMAN(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLRISEFALL3METHODS名称:Rising/Falling Three Methods 上升/下降三法 # 简介: 五日K线模式,以上升三法为例,上涨趋势中,第一日长阳线,中间三日价格在第一日范围内小幅震荡,第五日长阳线,收盘价高于第一日收盘价,预示股价上升。 # 例子:integer = CDLRISEFALL3METHODS(open, high, low, close) resDF['CDLRISEFALL3METHODS'] = ta.CDLRISEFALL3METHODS( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLSEPARATINGLINES名称:Separating Lines 分离线 # 简介:二日K线模式,上涨趋势中,第一日阴线,第二日阳线,第二日开盘价与第一日相同且为最低价,预示着趋势继续。 # 例子:integer = CDLSEPARATINGLINES(open, high, low, close) resDF['CDLSEPARATINGLINES'] = ta.CDLSEPARATINGLINES( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLSHOOTINGSTAR名称:Shooting Star 射击之星 # 简介:一日K线模式,上影线至少为实体长度两倍,没有下影线,预示着股价下跌 # 例子:integer = CDLSHOOTINGSTAR(open, high, low, close) resDF['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLSHORTLINE # 名称:Short Line Candle 短蜡烛 # 简介:一日K线模式,实体短,无上下影线。 # 例子:integer = CDLSHORTLINE(open, high, low, close) resDF['CDLSHORTLINE'] = ta.CDLSHORTLINE(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLSPINNINGTOP # 名称:Spinning Top 纺锤 # 简介:一日K线,实体小。 # 例子:integer = CDLSPINNINGTOP(open, high, low, close) resDF['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLSTALLEDPATTERN名称:Stalled Pattern 停顿形态 # 简介:三日K线模式,上涨趋势中,第二日长阳线,第三日开盘于前一日收盘价附近,短阳线,预示着上涨结束。 # 例子:integer = CDLSTALLEDPATTERN(open, high, low, close) resDF['CDLSTALLEDPATTERN'] = ta.CDLSTALLEDPATTERN( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLSTICKSANDWICH名称:Stick Sandwich 条形三明治 # 简介:三日K线模式,第一日长阴线,第二日阳线,开盘价高于前一日收盘价,第三日开盘价高于前两日最高价,收盘价于第一日收盘价相同。 # 例子:integer = CDLSTICKSANDWICH(open, high, low, close) resDF['CDLSTICKSANDWICH'] = ta.CDLSTICKSANDWICH( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLTAKURI名称:Takuri (Dragonfly Doji with very long lower shadow) 探水竿 # 简介:一日K线模式,大致与蜻蜓十字相同,下影线长度长。 # 例子:integer = CDLTAKURI(open, high, low, close) resDF['CDLTAKURI'] = ta.CDLTAKURI(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLTASUKIGAP名称:Tasuki Gap 跳空并列阴阳线 # 简介:三日K线模式,分上涨和下跌,以上升为例,前两日阳线,第二日跳空,第三日阴线,收盘价于缺口中,上升趋势持续。 # 例子:integer = CDLTASUKIGAP(open, high, low, close) resDF['CDLTASUKIGAP'] = ta.CDLTASUKIGAP(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLTHRUSTING名称:Thrusting Pattern 插入 # 简介:二日K线模式,与颈上线类似,下跌趋势中,第一日长阴线,第二日开盘价跳空,收盘价略低于前一日实体中部,与颈上线相比实体较长,预示着趋势持续。 # 例子:integer = CDLTHRUSTING(open, high, low, close) resDF['CDLTHRUSTING'] = ta.CDLTHRUSTING(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLTRISTAR # 名称:Tristar Pattern 三星 # 简介:三日K线模式,由三个十字组成,第二日十字必须高于或者低于第一日和第三日,预示着反转。 # 例子:integer = CDLTRISTAR(open, high, low, close) resDF['CDLTRISTAR'] = ta.CDLTRISTAR(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLUNIQUE3RIVER名称:Unique 3 River 奇特三河床 # 简介:三日K线模式,下跌趋势中,第一日长阴线,第二日为锤头,最低价创新低,第三日开盘价低于第二日收盘价,收阳线,收盘价不高于第二日收盘价,预示着反转,第二日下影线越长可能性越大。 # 例子:integer = CDLUNIQUE3RIVER(open, high, low, close) resDF['CDLUNIQUE3RIVER'] = ta.CDLUNIQUE3RIVER( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLUPSIDEGAP2CROWS名称:Upside Gap Two Crows 向上跳空的两只乌鸦 # 简介:三日K线模式,第一日阳线,第二日跳空以高于第一日最高价开盘,收阴线,第三日开盘价高于第二日,收阴线,与第一日比仍有缺口。 # 例子:integer = CDLUPSIDEGAP2CROWS(open, high, low, close) resDF['CDLUPSIDEGAP2CROWS'] = ta.CDLUPSIDEGAP2CROWS( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) # 函数名:CDLXSIDEGAP3METHODS名称:Upside/Downside Gap Three Methods 上升/下降跳空三法 # 简介:五日K线模式,以上升跳空三法为例,上涨趋势中,第一日长阳线,第二日短阳线,第三日跳空阳线,第四日阴线,开盘价与收盘价于前两日实体内,第五日长阳线,收盘价高于第一日收盘价,预示股价上升。 # 例子:integer = CDLXSIDEGAP3METHODS(open, high, low, close) resDF['CDLXSIDEGAP3METHODS'] = ta.CDLXSIDEGAP3METHODS( df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values) resDF['CMO'] = ta.CMO(df['price'].values, timeperiod=14) resDF['CORREL'] = ta.CORREL(df['max_price'].values, df['min_price'].values, timeperiod=30) resDF['DEMA'] = ta.DEMA(df['price'].values, timeperiod=30) resDF['DX'] = ta.DX(df['max_price'].values, df['min_price'].values, df['price'].values, timeperiod=14) resDF['EMA'] = ta.EMA(df['price'].values, timeperiod=30) resDF['HT_DCPERIOD'] = ta.HT_DCPERIOD(df['price'].values) resDF['HT_DCPHASE'] = ta.HT_DCPHASE(df['price'].values) resDF['inphase'], resDF['quadrature'] = ta.HT_PHASOR(df['price'].values) resDF['sine'], resDF['leadsine'] = ta.HT_SINE(df['price'].values) resDF['HT_TRENDLINE'] = ta.HT_TRENDLINE(df['price'].values) resDF['HT_TRENDMODE'] = ta.HT_TRENDMODE(df['price'].values) resDF['KAMA'] = ta.KAMA(df['price'].values, timeperiod=30) resDF['LINEARREG'] = ta.LINEARREG(df['price'].values, timeperiod=14) resDF['LINEARREG_ANGLE'] = ta.LINEARREG_ANGLE(df['price'].values, timeperiod=14) resDF['LINEARREG_INTERCEPT'] = ta.LINEARREG_INTERCEPT(df['price'].values, timeperiod=14) resDF['LINEARREG_SLOPE'] = ta.LINEARREG_SLOPE(df['price'].values, timeperiod=14) resDF['MA'] = ta.MA(df['price'].values, timeperiod=30, matype=0) resDF['macd'], resDF['macdsignal'], resDF['macdhist'] = ta.MACD( df['price'].values, fastperiod=12, slowperiod=26, signalperiod=9) resDF['macd'], resDF['macdsignal'], resDF['macdhist'] = ta.MACDEXT( df['price'].values, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) resDF['macd'], resDF['macdsignal'], resDF['macdhist'] = ta.MACDFIX( df['price'].values, signalperiod=9) #resDF['mama'], resDF['fama'] = ta.MAMA (df['price'].values, fastlimit=0, slowlimit=0) resDF['MAX'] = ta.MAX(df['price'].values, timeperiod=30) resDF['MAXINDEX'] = ta.MAXINDEX(df['price'].values, timeperiod=30) resDF['MEDPRICE'] = ta.MEDPRICE(df['max_price'].values, df['min_price'].values) resDF['MFI'] = ta.MFI(df['price_today_open'].values, df['max_price'].values, df['min_price'].values, df['price'].values, timeperiod=14) resDF['MIDPOINT'] = ta.MIDPOINT(df['price'].values, timeperiod=14) resDF['MIDPRICE'] = ta.MIDPRICE(df['max_price'].values, df['min_price'].values, timeperiod=14) resDF['MIN'] = ta.MIN(df['price'].values, timeperiod=30) resDF['MININDEX'] = ta.MININDEX(df['price'].values, timeperiod=30) resDF['min'], resDF['max'] = ta.MINMAX(df['price'].values, timeperiod=30) resDF['minidx'], resDF['maxidx'] = ta.MINMAXINDEX(df['price'].values, timeperiod=30) resDF['MINUS_DI'] = ta.MINUS_DI(df['max_price'].values, df['min_price'].values, df['price'].values, timeperiod=14) resDF['MINUS_DM'] = ta.MINUS_DM(df['max_price'].values, df['min_price'].values, timeperiod=14) resDF['MOM'] = ta.MOM(df['max_price'].values, timeperiod=10) resDF['NATR'] = ta.NATR(df['max_price'].values, df['min_price'].values, df['price'].values, timeperiod=14) # 函数名:OBV 名称:On Balance Volume 能量潮 # 简介:Joe Granville提出,通过统计成交量变动的趋势推测股价趋势计算公式:以某日为基期,逐日累计每日上市股票总成交量,若隔日指数或股票上涨,则基期OBV加上本日成交量为本日OBV。隔日指数或股票下跌,则基期OBV减去本日成交量为本日OBV # 研判:1、以“N”字型为波动单位,一浪高于一浪称“上升潮”,下跌称“跌潮”;上升潮买进,跌潮卖出 # 2、须配合K线图走势 # 3、用多空比率净额法进行修正,但不知TA-Lib采用哪种方法 # 多空比率净额= [(收盘价-最低价)-(最高价-收盘价)] ÷( 最高价-最低价)×成交量 # 例子:real = OBV(close, volume) resDF['OBV'] = ta.OBV(df['price'].values, df['vol'].values) # resDF['PLUS_DI'] = ta.PLUS_DI # resDF['PLUS_DM'] = ta.PLUS_DM resDF['PPO'] = ta.PPO(df['price'].values, fastperiod=12, slowperiod=26, matype=0) resDF['ROC'] = ta.ROC(df['price'].values, timeperiod=10) resDF['ROCP'] = ta.ROCP(df['price'].values, timeperiod=10) resDF['ROCR'] = ta.ROCR(df['price'].values, timeperiod=10) resDF['ROCR100'] = ta.ROCR100(df['price'].values, timeperiod=10) resDF['RSI'] = ta.RSI(df['price'].values, timeperiod=14) resDF['SAR'] = ta.SAR(df['max_price'].values, df['min_price'].values, acceleration=0, maximum=0) resDF['SAREXT'] = ta.SAREXT(df['max_price'].values, df['min_price'].values, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0) resDF['SMA'] = ta.SMA(df['price'].values, timeperiod=30) resDF['STDDEV'] = ta.STDDEV(df['price'].values, timeperiod=5, nbdev=1) # resDF['STOCH'] = ta.STOCH # resDF['STOCHF'] = ta.STOCHF resDF['fastk'], resDF['fastd'] = ta.STOCHRSI(df['price'].values, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) resDF['SUM'] = ta.SUM(df['price'].values, timeperiod=30) resDF['T3'] = ta.T3(df['price'].values, timeperiod=5, vfactor=0) resDF['TEMA'] = ta.TEMA(df['price'].values, timeperiod=30) resDF['TRANGE'] = ta.TRANGE(df['max_price'].values, df['min_price'].values, df['price'].values) resDF['TRIMA'] = ta.TRIMA(df['price'].values, timeperiod=30) resDF['TRIX'] = ta.TRIX(df['price'].values, timeperiod=30) resDF['TSF'] = ta.TSF(df['price'].values, timeperiod=14) resDF['TYPPRICE'] = ta.TYPPRICE(df['max_price'].values, df['min_price'].values, df['price'].values) # resDF['ULTOSC'] = ta.ULTOSC resDF['VAR'] = ta.VAR(df['price'].values, timeperiod=5, nbdev=1) resDF['WCLPRICE'] = ta.WCLPRICE(df['max_price'].values, df['min_price'].values, df['price'].values) # resDF['WILLR'] = ta.WILLR resDF['WMA'] = ta.WMA(df['price'].values, timeperiod=30) return resDF
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
for file in file_list: path = "C:/Users/pmsk9/Desktop/2021년도 연구/시가총액100위" df.append(pd.read_csv(path+"/"+file)) for i in df: ADX=talib.ADX(i.High,i.Low,i.Close,timeperiod=14) aroondown,aroonup =talib.AROON(i.High, i.Low, timeperiod=14) AROONOSC=talib.AROONOSC(i.High,i.Low,timeperiod=14) BOP=talib.BOP(i.Open,i.High,i.Low,i.Close) CCI=talib.CCI(i.High,i.Low,i.Close,timeperiod=9) CMO=talib.CMO(i.Close,timeperiod=14) DX=talib.DX(i.High,i.Low,i.Close,timeperiod=14) MFI=talib.MFI(i.High, i.Low,i.Close, i.Volume, timeperiod=14) PPO=talib.PPO(i.Close, fastperiod=12, slowperiod=26, matype=0) ROC=talib.ROC(i.Close,timeperiod=10) RSI=talib.RSI(i.Close,timeperiod=14) slowk, slowd = talib.STOCH(i.High, i.Low, i.Close, fastk_period=12.5, slowk_period=5, slowk_matype=0, slowd_period=3, slowd_matype=0)
def cci(self): df = self.select_k() cci = talib.CCI(df.high, df.low, df.close, timeperiod=14) #print(len(cci)) return cci
def generate_tech_data(stock, open_name, close_name, high_name, low_name, max_time_window=10): open_price = stock[open_name].values close_price = stock[close_name].values low_price = stock[low_name].values high_price = stock[high_name].values data = pd.DataFrame(stock) data['MOM'] = talib.MOM(close_price, timeperiod=max_time_window) # data['_SMA'] = talib.SMA(close_price) data['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price) data['HT_DCPHASE'] = talib.HT_DCPHASE(close_price) data['sine'], data['leadsine'] = talib.HT_SINE(close_price) data['inphase'], data['quadrature'] = talib.HT_PHASOR(close_price) # data['_HT_TRENDMODE'] = talib.HT_TRENDMODE(close_price) # data['_SAREXT'] = talib.SAREXT(high_price, low_price) # data['_ADX'] = talib.ADX(high_price, low_price, close_price) data['ADXR'] = talib.ADXR(high_price, low_price, close_price, timeperiod=max_time_window) data['APO'] = talib.APO(close_price, fastperiod=max_time_window // 2, slowperiod=max_time_window) data['AROON_UP'], _ = talib.AROON(high_price, low_price, timeperiod=max_time_window) # data['_BOP'] = talib.BOP(open_price, high_price, low_price, close_price) data['CCI'] = talib.CCI(high_price, low_price, close_price, timeperiod=max_time_window) data['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price, timeperiod=max_time_window) # data['_PLUS_DM'] = talib.PLUS_DM(high_price, low_price) data['PPO'] = talib.PPO(close_price, fastperiod=max_time_window // 2, slowperiod=max_time_window) data['macd'], data['macd_sig'], data['macd_hist'] = talib.MACD( close_price, fastperiod=max_time_window // 2, slowperiod=max_time_window, signalperiod=max_time_window // 2) data['CMO'] = talib.CMO(close_price, timeperiod=max_time_window) # data['ROC'] = talib.ROC(close_price) data['ROCP'] = talib.ROCP(close_price, timeperiod=max_time_window) # data['ROCR'] = talib.ROCR(close_price) # data['slowk'], data['slowd'] = talib.STOCH(high_price, low_price, close_price) data['fastk'], data['fastd'] = talib.STOCHF(high_price, low_price, close_price) data['TRIX'] = talib.TRIX(close_price, timeperiod=max_time_window) data['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price, timeperiod1=max_time_window // 2, timeperiod2=max_time_window, timeperiod3=max_time_window * 2) data['WILLR'] = talib.WILLR(high_price, low_price, close_price, timeperiod=max_time_window) data['NATR'] = talib.NATR(high_price, low_price, close_price, timeperiod=max_time_window) # data['_TRANGE'] = talib.TRANGE(high_price, low_price, close_price) data = data.drop([open_name, close_name, high_name, low_name], axis=1) # data.columns=data.columns.map(lambda x:x[1:]) data = data.dropna().astype(np.float32) return data
def hsmadata_ta(self): marketdata = pd.read_csv("raw data\\stock\\" + self.dataset + ".csv") codelist = pd.Series(marketdata['code'].unique()).sort_values() hsmadata = pd.DataFrame() for code in codelist: hsma0 = marketdata[marketdata['code'] == code].copy() hsma0 = hsma0[hsma0['vol'] > 0].sort_values(by='date') hsma0['ratio'] = hsma0['open'].shift( -(self.day + 1)) / hsma0['open'].shift(-1) - 1 hsma0['ocratio'] = hsma0['open'].shift(-1) / hsma0['close'] - 1 hsma = hsma0[['date', 'code', 'ratio']].copy() for l in self.length: hsma['ROC_' + str(l)] = talib.ROC( hsma0.close.values, timeperiod=l) if l == 1: hsma['freeturn_1'] = hsma0['free_turn'] else: hsma['freeturn_' + str(l)] = talib.SUM( hsma0.free_turn.values, timeperiod=l) if l > 2: hsma['CCI_' + str(l)] = talib.CCI( hsma0.high.values, hsma0.low.values, hsma0.close.values, timeperiod=l) hsma['ADX_' + str(l)] = talib.ADX( hsma0.high.values, hsma0.low.values, hsma0.close.values, timeperiod=l) hsma['MFI_' + str(l)] = talib.MFI( hsma0.high.values, hsma0.low.values, hsma0.close.values, hsma0.amt.values, timeperiod=l) hsma['RSI_' + str(l)] = talib.RSI( hsma0.close.values, timeperiod=l) hsma['VAR_' + str(l)] = talib.VAR( hsma0.close.values, timeperiod=l) hsma = hsma[hsma0['ocratio'] < 0.09] hsmadata = pd.concat([hsmadata, hsma], ignore_index=True) hsmadata = hsmadata.dropna() hsmamean = pd.DataFrame() for i in range(3, hsmadata.shape[1]): temp = hsmadata.ix[:, [0, i]] temp = temp.groupby(['date'], as_index=False)[[temp.columns[1]]].mean() temp = temp.sort_values(by='date') temp = temp.rename( columns={temp.columns[1]: (temp.columns[1] + '_mean')}) if hsmamean.shape[0] == 0: hsmamean = temp else: hsmamean = pd.merge(hsmamean, temp) hsmadata = pd.merge(hsmadata, hsmamean) if not self.absratio: temp = hsmadata[['date', 'ratio']] ratio_mean = temp.groupby(['date'], as_index=False)[['ratio']].mean() ratio_mean = ratio_mean.sort_values(by='date') ratio_mean = ratio_mean.rename(columns={'ratio': ('ratio_mean')}) hsmadata = pd.merge(hsmadata, ratio_mean) hsmadata.ratio = hsmadata.ratio - hsmadata.ratio_mean hsmadata = hsmadata.drop(['ratio_mean'], 1) else: ratio_mean = 0 return hsmadata, ratio_mean
def CCI(DataFrame, N=14): res = talib.CCI(DataFrame.high.values, DataFrame.low.values, DataFrame.close.values, N) return pd.DataFrame({'CCI': res}, index=DataFrame.index)
def get_cci(df, timeperiod=14): return talib.CCI(df.high, df.low, df.close, timeperiod=timeperiod)