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 """ upperlength = 18 # lowerlength=30 dataframe['max'] = ta.MAX(dataframe, timeperiod=upperlength) dataframe['upper'] = dataframe['max'].shift(periods=1) dataframe['min'] = ta.MIN(dataframe, timeperiod=upperlength) dataframe['lower'] = dataframe['min'].shift(periods=1) dataframe['middle'] = dataframe['min'] + (dataframe['upper'] - dataframe['min']) / 2 return dataframe
def get_dataset(test=False): df = pickle.load(open(p.file, "rb")) # Add features to dataframe # Typical Features: close/sma, bollinger band, holding stock, return since entry df['dr'] = df.close / df.close.shift(1) - 1 # daily return df['adr'] = ta.SMA(df, price='dr', timeperiod=p.adr_period) df['sma'] = ta.SMA(df, price='close', timeperiod=p.sma_period) df['dsma'] = df.sma / df.sma.shift(1) - 1 df['rsma'] = df.close / df.sma df['rsi'] = ta.RSI(df, price='close', timeperiod=p.rsi_period) df['hh'] = df.high / ta.MAX(df, price='high', timeperiod=p.hh_period) df['ll'] = df.low / ta.MIN(df, price='low', timeperiod=p.ll_period) df['hhll'] = (df.high + df.low) / (df.high / df.hh + df.low / df.ll) df = df.dropna() # Map features to bins df = df.assign(binrsi=bin_feature(df.rsi)) if p.version == 1: df = df.assign(binadr=bin_feature(df.adr)) df = df.assign(binhh=bin_feature(df.hh)) df = df.assign(binll=bin_feature(df.ll)) elif p.version == 2: df = df.assign(bindsma=bin_feature(df.dsma)) df = df.assign(binrsma=bin_feature(df.rsma)) df = df.assign(binhhll=bin_feature(df.hhll)) if p.max_bars > 0: df = df.tail(p.max_bars).reset_index(drop=True) # Separate Train / Test Datasets using train_pct number of rows if test: rows = int(len(df) * p.test_pct) return df.tail(rows).reset_index(drop=True) else: rows = int(len(df) * p.train_pct) return df.head(rows).reset_index(drop=True)
def populate_indicators(self, dataframe: DataFrame) -> DataFrame: """ Adds several different TA indicators to the given DataFrame """ dataframe['ema_{}'.format(self.EMA_SHORT_TERM)] = ta.EMA( dataframe, timeperiod=self.EMA_SHORT_TERM ) dataframe['ema_{}'.format(self.EMA_MEDIUM_TERM)] = ta.EMA( dataframe, timeperiod=self.EMA_MEDIUM_TERM ) dataframe['ema_{}'.format(self.EMA_LONG_TERM)] = ta.EMA( dataframe, timeperiod=self.EMA_LONG_TERM ) 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['min'] = ta.MIN(dataframe, timeperiod=self.EMA_MEDIUM_TERM) dataframe['max'] = ta.MAX(dataframe, timeperiod=self.EMA_MEDIUM_TERM) return dataframe
def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame: """ Add several indicators needed for buy and sell strategies defined below. """ upperlength = 70 dataframe['max'] = ta.MAX(dataframe, timeperiod=upperlength) dataframe['upper'] = dataframe['max'].shift(periods=1) dataframe['min'] = ta.MIN(dataframe, timeperiod=upperlength) dataframe['lower'] = dataframe['min'].shift(periods=1) dataframe['middle'] = dataframe['min'] + (dataframe['upper'] - dataframe['min']) / 2 return dataframe
def resample(self, dataframe: DataFrame, period: int) -> DataFrame: df = dataframe.copy() df = df.set_index(DatetimeIndex(df['date'])) ohlc_dict = { 'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last', 'volume': 'sum', } #df = df.resample(str(int(period)) + 'min', how=ohlc_dict).dropna(how='any') df = df.resample(str(int(period)) + 'min').apply(ohlc_dict).dropna(how='any') df['{}_min'.format(period)] = ta.MIN(df, timeperiod=self.channel_period, price='low').shift(1) df['{}_max'.format(period)] = ta.MAX(df, timeperiod=self.channel_period, price='high').shift(1) df['{}_ma_high'.format(period)] = ta.EMA(df, timeperiod=self.ma_period, price='high') df['{}_ma_low'.format(period)] = ta.EMA(df, timeperiod=self.ma_period, price='low') df['{}_ma_close'.format(period)] = ta.EMA(df, timeperiod=self.ma_period, price='close') heikinashi = qtpylib.heikinashi(df) df['{}_ha_open'.format(period)] = heikinashi['open'] df['{}_ha_close'.format(period)] = heikinashi['close'] df['{}_ha_high'.format(period)] = heikinashi['high'] df['{}_ha_low'.format(period)] = heikinashi['low'] df['{}_stoploss'.format(period)] = qtpylib.rolling_min( df['{}_ha_low'.format(period)], 2).shift(1) df = df.drop(columns=['open', 'high', 'low', 'close', 'volume']) df = df.resample(str(self.timeframe) + 'min') df = df.interpolate(method='time') df['date'] = df.index df.index = range(len(df)) dataframe = merge(dataframe, df, on='date', how='left') return dataframe
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe = self.resample(dataframe, self.ticker_interval, self.resample_factor) ################################################################################## # buy and sell indicators dataframe['ema_{}'.format(self.EMA_SHORT_TERM)] = ta.EMA( dataframe, timeperiod=self.EMA_SHORT_TERM) dataframe['ema_{}'.format(self.EMA_MEDIUM_TERM)] = ta.EMA( dataframe, timeperiod=self.EMA_MEDIUM_TERM) dataframe['ema_{}'.format(self.EMA_LONG_TERM)] = ta.EMA( dataframe, timeperiod=self.EMA_LONG_TERM) 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['min'] = ta.MIN(dataframe, timeperiod=self.EMA_MEDIUM_TERM) dataframe['max'] = ta.MAX(dataframe, timeperiod=self.EMA_MEDIUM_TERM) dataframe['cci'] = ta.CCI(dataframe) dataframe['mfi'] = ta.MFI(dataframe) dataframe['rsi'] = ta.RSI(dataframe, timeperiod=7) dataframe['average'] = (dataframe['close'] + dataframe['open'] + dataframe['high'] + dataframe['low']) / 4 ################################################################################## # required for graphing bollinger = qtpylib.bollinger_bands(dataframe['close'], window=20, stds=2) dataframe['bb_lowerband'] = bollinger['lower'] dataframe['bb_upperband'] = bollinger['upper'] dataframe['bb_middleband'] = bollinger['mid'] macd = ta.MACD(dataframe) dataframe['macd'] = macd['macd'] dataframe['macdsignal'] = macd['macdsignal'] dataframe['macdhist'] = macd['macdhist'] return dataframe
def __init__(self, feed: pd.DataFrame): super().__init__(feed) if self.has_enough_feed(): rsi = abstract.RSI(self.feed, period=self.period) maxrsi = abstract.MAX(rsi, period=self.period) minrsi = abstract.MIN(rsi, period=self.period) srsi = (rsi - minrsi) / (maxrsi - minrsi) self.feed["srsi"] = srsi prev_srsi = self.feed["srsi"].shift(1) self.feed["cross_up"] = (self.feed["srsi"] > self.buy_threshold) & ( prev_srsi <= self.buy_threshold ) self.feed["cross_down"] = (self.feed["srsi"] < self.sell_threshold) & ( prev_srsi >= self.sell_threshold )
def calc_indicators(self, dataframe: DataFrame) -> DataFrame: dataframe['min'] = ta.MIN(dataframe, timeperiod=self.channel_period, price='low').shift(1) dataframe['max'] = ta.MAX(dataframe, timeperiod=self.channel_period, price='high').shift(1) dataframe['ma_high'] = ta.EMA(dataframe, timeperiod=self.ma_period, price='high') dataframe['ma_low'] = ta.EMA(dataframe, timeperiod=self.ma_period, price='low') dataframe['ma_close'] = ta.EMA(dataframe, timeperiod=self.ma_period, price='close') dataframe.loc[(dataframe['ma_close'].shift(1) < dataframe['ma_close']), 'ma_trend'] = 1 dataframe.loc[(dataframe['ma_close'].shift(1) > dataframe['ma_close']), 'ma_trend'] = -1 dataframe.loc[( dataframe['ma_close'].shift(1) == dataframe['ma_close']), 'ma_trend'] = 0 heikinashi = qtpylib.heikinashi(dataframe) dataframe['ha_open'] = heikinashi['open'] dataframe['ha_close'] = heikinashi['close'] dataframe['ha_high'] = heikinashi['high'] dataframe['ha_low'] = heikinashi['low'] dataframe['atr'] = qtpylib.atr(dataframe, self.atr_period) dataframe['volume_mean'] = qtpylib.rolling_mean( dataframe['volume'], self.atr_period).shift(1) dataframe['stoploss'] = qtpylib.rolling_min(dataframe['ha_low'], 2).shift(1) bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) dataframe['bb_lowerband'] = bollinger['lower'] dataframe['bb_upperband'] = bollinger['upper'] dataframe['bb_middleband'] = bollinger['mid'] return dataframe
def __init__(self, feed: pd.DataFrame): super().__init__(feed) if self.has_enough_feed(): dch = abstract.MAX(self.feed["close"], timeperiod=self.high_period) dcl = abstract.MIN(self.feed["close"], timeperiod=self.low_period) self.feed["dch"] = dch self.feed["dcl"] = dcl prev_close = self.feed["close"].shift(1) prev_dch = self.feed["dch"].shift(1) prev_dcl = self.feed["dcl"].shift(1) self.feed["cross_up"] = (self.feed["close"] >= self.feed["dch"]) & ( prev_close < prev_dch ) self.feed["cross_down"] = (self.feed["close"] <= self.feed["dcl"]) & ( prev_close > prev_dcl )
def __init__(self, feed: pd.DataFrame): super().__init__(feed) if self.has_enough_feed(): rsi = abstract.RSI(self.feed, period=self.period) maxrsi = abstract.MAX(rsi, period=self.period) minrsi = abstract.MIN(rsi, period=self.period) srsi = (rsi - minrsi) / (maxrsi - minrsi) self.feed["srsi"] = srsi bb = abstract.BBANDS(self.feed, matype=MA_Type.T3, period=self.period) self.feed["bbh"] = bb["upperband"] prev_close = self.feed["close"].shift(1) prev_bbh = self.feed["bbh"].shift(1) self.feed["bbh_cross_up"] = (self.feed["close"] > self.feed["bbh"] ) & (prev_close <= prev_bbh)