def gen_ta(path): df = pd.read_csv('h/%s.csv' % path) df = df.dropna() df = df.iloc[::-1] df = df.reset_index(drop=True) #生成技術指標 sma5 = indicator(talib.MA(df['close'], 5), 'SMA_5') sma10 = indicator(talib.MA(df['close'], 10), 'SMA_10') sma50 = indicator(talib.MA(df['close'], 50), 'SMA_50') rsi = indicator(talib.RSI(df['close']), 'RSI_14') sma5u = sma5.diff().rename(columns={'SMA_5': 'SMA_5_u'}) sma10u = sma10.diff().rename(columns={'SMA_10': 'SMA_10_u'}) sma50u = sma50.diff().rename(columns={'SMA_50': 'SMA_50_u'}) CDL2CROWS = indicator(abstract.CDL2CROWS(df), 'CDL2CROWS') CDL3BLACKCROWS = indicator(abstract.CDL3BLACKCROWS(df), 'CDL3BLACKCROWS') CDL3INSIDE = indicator(abstract.CDL3INSIDE(df), 'CDL3INSIDE') CDL3LINESTRIKE = indicator(abstract.CDL3LINESTRIKE(df), 'CDL3LINESTRIKE') CDL3OUTSIDE = indicator(abstract.CDL3OUTSIDE(df), 'CDL3OUTSIDE') CDL3STARSINSOUTH = indicator(abstract.CDL3STARSINSOUTH(df), 'CDL3STARSINSOUTH') CDL3WHITESOLDIERS = indicator(abstract.CDL3WHITESOLDIERS(df), 'CDL3WHITESOLDIERS') CDLABANDONEDBABY = indicator(abstract.CDLABANDONEDBABY(df), 'CDLABANDONEDBABY') CDLADVANCEBLOCK = indicator(abstract.CDLADVANCEBLOCK(df), 'CDLADVANCEBLOCK') CDLBELTHOLD = indicator(abstract.CDLBELTHOLD(df), 'CDLBELTHOLD') CDLBREAKAWAY = indicator(abstract.CDLBREAKAWAY(df), 'CDLBREAKAWAY') CDLCLOSINGMARUBOZU = indicator(abstract.CDLCLOSINGMARUBOZU(df), 'CDLCLOSINGMARUBOZU') CDLCONCEALBABYSWALL = indicator(abstract.CDLCONCEALBABYSWALL(df), 'CDLCONCEALBABYSWALL') CDLCOUNTERATTACK = indicator(abstract.CDLCOUNTERATTACK(df), 'CDLCOUNTERATTACK') CDLDARKCLOUDCOVER = indicator(abstract.CDLDARKCLOUDCOVER(df), 'CDLDARKCLOUDCOVER') CDLDOJI = indicator(abstract.CDLDOJI(df), 'CDLDOJI') CDLDOJISTAR = indicator(abstract.CDLDOJISTAR(df), 'CDLDOJISTAR') CDLDRAGONFLYDOJI = indicator(abstract.CDLDRAGONFLYDOJI(df), 'CDLDRAGONFLYDOJI') CDLENGULFING = indicator(abstract.CDLENGULFING(df), 'CDLENGULFING') CDLEVENINGDOJISTAR = indicator(abstract.CDLEVENINGDOJISTAR(df), 'CDLEVENINGDOJISTAR') CDLEVENINGSTAR = indicator(abstract.CDLEVENINGSTAR(df), 'CDLEVENINGSTAR') CDLGAPSIDESIDEWHITE = indicator(abstract.CDLGAPSIDESIDEWHITE(df), 'CDLGAPSIDESIDEWHITE') CDLGRAVESTONEDOJI = indicator(abstract.CDLGRAVESTONEDOJI(df), 'CDLGRAVESTONEDOJI') CDLHAMMER = indicator(abstract.CDLHAMMER(df), 'CDLHAMMER') CDLHANGINGMAN = indicator(abstract.CDLHANGINGMAN(df), 'CDLHANGINGMAN') CDLHARAMI = indicator(abstract.CDLHARAMI(df), 'CDLHARAMI') CDLHARAMICROSS = indicator(abstract.CDLHARAMICROSS(df), 'CDLHARAMICROSS') CDLHIGHWAVE = indicator(abstract.CDLHIGHWAVE(df), 'CDLHIGHWAVE') CDLHIKKAKE = indicator(abstract.CDLHIKKAKE(df), 'CDLHIKKAKE') CDLHIKKAKEMOD = indicator(abstract.CDLHIKKAKEMOD(df), 'CDLHIKKAKEMOD') CDLHOMINGPIGEON = indicator(abstract.CDLHOMINGPIGEON(df), 'CDLHOMINGPIGEON') CDLIDENTICAL3CROWS = indicator(abstract.CDLIDENTICAL3CROWS(df), 'CDLIDENTICAL3CROWS') CDLINNECK = indicator(abstract.CDLINNECK(df), 'CDLINNECK') CDLINVERTEDHAMMER = indicator(abstract.CDLINVERTEDHAMMER(df), 'CDLINVERTEDHAMMER') CDLKICKING = indicator(abstract.CDLKICKING(df), 'CDLKICKING') CDLKICKINGBYLENGTH = indicator(abstract.CDLKICKINGBYLENGTH(df), 'CDLKICKINGBYLENGTH') CDLLADDERBOTTOM = indicator(abstract.CDLLADDERBOTTOM(df), 'CDLLADDERBOTTOM') CDLLONGLEGGEDDOJI = indicator(abstract.CDLLONGLEGGEDDOJI(df), 'CDLLONGLEGGEDDOJI') CDLLONGLINE = indicator(abstract.CDLLONGLINE(df), 'CDLLONGLINE') CDLMARUBOZU = indicator(abstract.CDLMARUBOZU(df), 'CDLMARUBOZU') CDLMATCHINGLOW = indicator(abstract.CDLMATCHINGLOW(df), 'CDLMATCHINGLOW') CDLMATHOLD = indicator(abstract.CDLMATHOLD(df), 'CDLMATHOLD') CDLMORNINGDOJISTAR = indicator(abstract.CDLMORNINGDOJISTAR(df), 'CDLMORNINGDOJISTAR') CDLMORNINGSTAR = indicator(abstract.CDLMORNINGSTAR(df), 'CDLMORNINGSTAR') CDLONNECK = indicator(abstract.CDLONNECK(df), 'CDLONNECK') CDLPIERCING = indicator(abstract.CDLPIERCING(df), 'CDLPIERCING') CDLRICKSHAWMAN = indicator(abstract.CDLRICKSHAWMAN(df), 'CDLRICKSHAWMAN') CDLRISEFALL3METHODS = indicator(abstract.CDLRISEFALL3METHODS(df), 'CDLRISEFALL3METHODS') CDLSEPARATINGLINES = indicator(abstract.CDLSEPARATINGLINES(df), 'CDLSEPARATINGLINES') CDLSHOOTINGSTAR = indicator(abstract.CDLSHOOTINGSTAR(df), 'CDLSHOOTINGSTAR') CDLSHORTLINE = indicator(abstract.CDLSHORTLINE(df), 'CDLSHORTLINE') CDLSPINNINGTOP = indicator(abstract.CDLSPINNINGTOP(df), 'CDLSPINNINGTOP') CDLSTALLEDPATTERN = indicator(abstract.CDLSTALLEDPATTERN(df), 'CDLSTALLEDPATTERN') CDLSTICKSANDWICH = indicator(abstract.CDLSTICKSANDWICH(df), 'CDLSTICKSANDWICH') CDLTAKURI = indicator(abstract.CDLTAKURI(df), 'CDLTAKURI') CDLTASUKIGAP = indicator(abstract.CDLTASUKIGAP(df), 'CDLTASUKIGAP') CDLTHRUSTING = indicator(abstract.CDLTHRUSTING(df), 'CDLTHRUSTING') CDLTRISTAR = indicator(abstract.CDLTRISTAR(df), 'CDLTRISTAR') CDLUNIQUE3RIVER = indicator(abstract.CDLUNIQUE3RIVER(df), 'CDLUNIQUE3RIVER') CDLUPSIDEGAP2CROWS = indicator(abstract.CDLUPSIDEGAP2CROWS(df), 'CDLUPSIDEGAP2CROWS') CDLXSIDEGAP3METHODS = indicator(abstract.CDLXSIDEGAP3METHODS(df), 'CDLXSIDEGAP3METHODS') macd, macdsignal, macdhist = talib.MACD(df['close']) macd = indicator(macd, 'macd') macdsignal = indicator(macdsignal, 'macdsignal') macdhist = indicator(macdhist, 'macdhist') df = pd.concat([ df, sma5, sma5u, sma10, sma10u, sma50, sma50u, rsi, CDL2CROWS, CDL3BLACKCROWS, CDL3INSIDE, CDL3LINESTRIKE, CDL3OUTSIDE, CDL3STARSINSOUTH, CDL3WHITESOLDIERS, CDLABANDONEDBABY, CDLADVANCEBLOCK, CDLBELTHOLD, CDLBREAKAWAY, CDLCLOSINGMARUBOZU, CDLCONCEALBABYSWALL, CDLCOUNTERATTACK, CDLDARKCLOUDCOVER, CDLDOJI, CDLDOJISTAR, CDLDRAGONFLYDOJI, CDLENGULFING, CDLEVENINGDOJISTAR, CDLEVENINGSTAR, CDLGAPSIDESIDEWHITE, CDLGRAVESTONEDOJI, CDLHAMMER, CDLHANGINGMAN, CDLHARAMI, CDLHARAMICROSS, CDLHIGHWAVE, CDLHIKKAKE, CDLHIKKAKEMOD, CDLHOMINGPIGEON, CDLIDENTICAL3CROWS, CDLINNECK, CDLINVERTEDHAMMER, CDLKICKING, CDLKICKINGBYLENGTH, CDLLADDERBOTTOM, CDLLONGLEGGEDDOJI, CDLLONGLINE, CDLMARUBOZU, CDLMATCHINGLOW, CDLMATHOLD, CDLMORNINGDOJISTAR, CDLMORNINGSTAR, CDLONNECK, CDLPIERCING, CDLRICKSHAWMAN, CDLRISEFALL3METHODS, CDLSEPARATINGLINES, CDLSHOOTINGSTAR, CDLSHORTLINE, CDLSPINNINGTOP, CDLSTALLEDPATTERN, CDLSTICKSANDWICH, CDLTAKURI, CDLTASUKIGAP, CDLTHRUSTING, CDLTRISTAR, CDLUNIQUE3RIVER, CDLUPSIDEGAP2CROWS, CDLXSIDEGAP3METHODS, macd, macdsignal, macdhist ], axis=1) df['next_open'] = df['open'].shift(-1) df = df[:-1] df = df[(df['SMA_50'] > 0)] df = df.reset_index(drop=True) return df
def populate_indicators(dataframe: DataFrame) -> DataFrame: """ Adds several different TA indicators to the given DataFrame """ dataframe['sar'] = ta.SAR(dataframe) dataframe['adx'] = ta.ADX(dataframe) stoch = ta.STOCHF(dataframe) dataframe['fastd'] = stoch['fastd'] dataframe['fastk'] = stoch['fastk'] dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) dataframe['mfi'] = ta.MFI(dataframe) dataframe['cci'] = ta.CCI(dataframe) dataframe['rsi'] = ta.RSI(dataframe) dataframe['mom'] = ta.MOM(dataframe) dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) dataframe['ao'] = awesome_oscillator(dataframe) macd = ta.MACD(dataframe) dataframe['macd'] = macd['macd'] dataframe['macdsignal'] = macd['macdsignal'] dataframe['macdhist'] = macd['macdhist'] # add volatility indicators dataframe['natr'] = ta.NATR(dataframe) # add volume indicators dataframe['obv'] = ta.OBV(dataframe) # add more momentum indicators dataframe['rocp'] = ta.ROCP(dataframe) # add some pattern recognition dataframe['CDL2CROWS'] = ta.CDL2CROWS(dataframe) dataframe['CDL3BLACKCROWS'] = ta.CDL3BLACKCROWS(dataframe) dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) dataframe['CDL3STARSINSOUTH'] = ta.CDL3STARSINSOUTH(dataframe) dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) dataframe['CDLADVANCEBLOCK'] = ta.CDLADVANCEBLOCK(dataframe) dataframe['CDLBELTHOLD'] = ta.CDLBELTHOLD(dataframe) dataframe['CDLBREAKAWAY'] = ta.CDLBREAKAWAY(dataframe) dataframe['CDLDOJI'] = ta.CDLDOJI(dataframe) dataframe['CDLDOJISTAR'] = ta.CDLDOJISTAR(dataframe) dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) dataframe['CDLBREAKAWAY'] = ta.CDLBREAKAWAY(dataframe) dataframe['CDLBREAKAWAY'] = ta.CDLBREAKAWAY(dataframe) # enter categorical time hour = datetime.strptime(str(dataframe['date'][len(dataframe) - 1]), "%Y-%m-%d %H:%M:%S").hour for h in range(24): dataframe['hour_{0:02}'.format(h)] = int(h == hour) return dataframe
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Adds several different TA indicators to the given DataFrame Performance Note: For the best performance be frugal on the number of indicators you are using. Let uncomment only the indicator you are using in your strategies or your hyperopt configuration, otherwise you will waste your memory and CPU usage. :param dataframe: Raw data from the exchange and parsed by parse_ticker_dataframe() :param metadata: Additional information, like the currently traded pair :return: a Dataframe with all mandatory indicators for the strategies """ # Momentum Indicators # ------------------------------------ # ADX dataframe['adx'] = ta.ADX(dataframe) # Plus Directional Indicator / Movement dataframe['plus_dm'] = ta.PLUS_DM(dataframe) dataframe['plus_di'] = ta.PLUS_DI(dataframe) # # Minus Directional Indicator / Movement dataframe['minus_dm'] = ta.MINUS_DM(dataframe) dataframe['minus_di'] = ta.MINUS_DI(dataframe) # Aroon, Aroon Oscillator aroon = ta.AROON(dataframe) dataframe['aroonup'] = aroon['aroonup'] dataframe['aroondown'] = aroon['aroondown'] dataframe['aroonosc'] = ta.AROONOSC(dataframe) # Awesome Oscillator dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) # Keltner Channel keltner = qtpylib.keltner_channel(dataframe) dataframe["kc_upperband"] = keltner["upper"] dataframe["kc_lowerband"] = keltner["lower"] dataframe["kc_middleband"] = keltner["mid"] dataframe["kc_percent"] = ( (dataframe["close"] - dataframe["kc_lowerband"]) / (dataframe["kc_upperband"] - dataframe["kc_lowerband"])) dataframe["kc_width"] = ( (dataframe["kc_upperband"] - dataframe["kc_lowerband"]) / dataframe["kc_middleband"]) # Ultimate Oscillator dataframe['uo'] = ta.ULTOSC(dataframe) # Commodity Channel Index: values [Oversold:-100, Overbought:100] dataframe['cci'] = ta.CCI(dataframe) # RSI dataframe['rsi'] = ta.RSI(dataframe) # Inverse Fisher transform on RSI: values [-1.0, 1.0] (https://goo.gl/2JGGoy) rsi = 0.1 * (dataframe['rsi'] - 50) dataframe['fisher_rsi'] = (np.exp(2 * rsi) - 1) / (np.exp(2 * rsi) + 1) # Inverse Fisher transform on RSI normalized: values [0.0, 100.0] (https://goo.gl/2JGGoy) dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) # Stochastic Slow stoch = ta.STOCH(dataframe) dataframe['slowd'] = stoch['slowd'] dataframe['slowk'] = stoch['slowk'] # Stochastic Fast stoch_fast = ta.STOCHF(dataframe) dataframe['fastd'] = stoch_fast['fastd'] dataframe['fastk'] = stoch_fast['fastk'] # Stochastic RSI stoch_rsi = ta.STOCHRSI(dataframe) dataframe['fastd_rsi'] = stoch_rsi['fastd'] dataframe['fastk_rsi'] = stoch_rsi['fastk'] # MACD macd = ta.MACD(dataframe) dataframe['macd'] = macd['macd'] dataframe['macdsignal'] = macd['macdsignal'] dataframe['macdhist'] = macd['macdhist'] # MFI dataframe['mfi'] = ta.MFI(dataframe) # # ROC dataframe['roc'] = ta.ROC(dataframe) # Overlap Studies # ------------------------------------ # Bollinger Bands bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) dataframe['bb_lowerband'] = bollinger['lower'] dataframe['bb_middleband'] = bollinger['mid'] dataframe['bb_upperband'] = bollinger['upper'] dataframe["bb_percent"] = ( (dataframe["close"] - dataframe["bb_lowerband"]) / (dataframe["bb_upperband"] - dataframe["bb_lowerband"])) dataframe["bb_width"] = ( (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe["bb_middleband"]) # Bollinger Bands - Weighted (EMA based instead of SMA) weighted_bollinger = qtpylib.weighted_bollinger_bands( qtpylib.typical_price(dataframe), window=20, stds=2) dataframe["wbb_upperband"] = weighted_bollinger["upper"] dataframe["wbb_lowerband"] = weighted_bollinger["lower"] dataframe["wbb_middleband"] = weighted_bollinger["mid"] dataframe["wbb_percent"] = ( (dataframe["close"] - dataframe["wbb_lowerband"]) / (dataframe["wbb_upperband"] - dataframe["wbb_lowerband"])) dataframe["wbb_width"] = ( (dataframe["wbb_upperband"] - dataframe["wbb_lowerband"]) / dataframe["wbb_middleband"]) # EMA - Exponential Moving Average dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) dataframe['ema21'] = ta.EMA(dataframe, timeperiod=21) dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) # SMA - Simple Moving Average dataframe['sma3'] = ta.SMA(dataframe, timeperiod=3) dataframe['sma5'] = ta.SMA(dataframe, timeperiod=5) dataframe['sma10'] = ta.SMA(dataframe, timeperiod=10) dataframe['sma21'] = ta.SMA(dataframe, timeperiod=21) dataframe['sma50'] = ta.SMA(dataframe, timeperiod=50) dataframe['sma100'] = ta.SMA(dataframe, timeperiod=100) # Parabolic SAR dataframe['sar'] = ta.SAR(dataframe) # TEMA - Triple Exponential Moving Average dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) # Cycle Indicator # ------------------------------------ # Hilbert Transform Indicator - SineWave hilbert = ta.HT_SINE(dataframe) dataframe['htsine'] = hilbert['sine'] dataframe['htleadsine'] = hilbert['leadsine'] # Pattern Recognition - Bullish candlestick patterns # ------------------------------------ # Hammer: values [0, 100] dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) # Inverted Hammer: values [0, 100] dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) # Dragonfly Doji: values [0, 100] dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) # Piercing Line: values [0, 100] dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] # Morningstar: values [0, 100] dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR( dataframe) # values [0, 100] # Three White Soldiers: values [0, 100] dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS( dataframe) # values [0, 100] # Pattern Recognition - Bearish candlestick patterns # ------------------------------------ # Hanging Man: values [0, 100] dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) # Shooting Star: values [0, 100] dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) # Gravestone Doji: values [0, 100] dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) # Dark Cloud Cover: values [0, 100] dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) # Evening Doji Star: values [0, 100] dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) # Evening Star: values [0, 100] dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) # Pattern Recognition - Bullish/Bearish candlestick patterns # ------------------------------------ # Three Line Strike: values [0, -100, 100] dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) # Spinning Top: values [0, -100, 100] dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP( dataframe) # values [0, -100, 100] # Engulfing: values [0, -100, 100] dataframe['CDLENGULFING'] = ta.CDLENGULFING( dataframe) # values [0, -100, 100] # Harami: values [0, -100, 100] dataframe['CDLHARAMI'] = ta.CDLHARAMI( dataframe) # values [0, -100, 100] # Three Outside Up/Down: values [0, -100, 100] dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE( dataframe) # values [0, -100, 100] # Three Inside Up/Down: values [0, -100, 100] dataframe['CDL3INSIDE'] = ta.CDL3INSIDE( dataframe) # values [0, -100, 100] # Chart type # ------------------------------------ # Heikin Ashi Strategy heikinashi = qtpylib.heikinashi(dataframe) dataframe['ha_open'] = heikinashi['open'] dataframe['ha_close'] = heikinashi['close'] dataframe['ha_high'] = heikinashi['high'] dataframe['ha_low'] = heikinashi['low'] # Retrieve best bid and best ask from the orderbook # ------------------------------------ """ # first check if dataprovider is available if self.dp: if self.dp.runmode in ('live', 'dry_run'): ob = self.dp.orderbook(metadata['pair'], 1) dataframe['best_bid'] = ob['bids'][0][0] dataframe['best_ask'] = ob['asks'][0][0] """ return dataframe