示例#1
0
    def populate_indicators(self, dataframe: DataFrame,
                            metadata: dict) -> DataFrame:
        # # Commodity Channel Index: values [Oversold:-100, Overbought:100]
        dataframe['buy-cci'] = ta.CCI(dataframe,
                                      timeperiod=self.buy_params['cci-period'])
        dataframe['sell-cci'] = ta.CCI(
            dataframe, timeperiod=self.sell_params['sell-cci-period'])

        # RSI
        dataframe['buy-rsi'] = ta.RSI(dataframe,
                                      timeperiod=self.buy_params['rsi-period'])
        dataframe['sell-rsi'] = ta.RSI(
            dataframe, timeperiod=self.sell_params['sell-rsi-period'])

        # KAMA - Kaufman Adaptive Moving Average
        dataframe['buy-kama-short'] = ta.KAMA(
            dataframe, timeperiod=self.buy_params['kama-short-period'])
        dataframe['buy-kama-long'] = ta.KAMA(
            dataframe, timeperiod=self.buy_params['kama-long-period'])
        dataframe['buy-kama-long-slope'] = (dataframe['buy-kama-long'] /
                                            dataframe['buy-kama-long'].shift())

        dataframe['sell-kama-short'] = ta.KAMA(
            dataframe, timeperiod=self.sell_params['sell-kama-short-period'])
        dataframe['sell-kama-long'] = ta.KAMA(
            dataframe, timeperiod=self.sell_params['sell-kama-long-period'])
        dataframe['sell-kama-long-slope'] = (
            dataframe['sell-kama-long'] / dataframe['sell-kama-long'].shift())

        return dataframe
    def populate_indicators(self, dataframe: DataFrame,
                            metadata: dict) -> DataFrame:
        """
        Dynamic TA indicators
        Used so hyperopt can optimized around the period of various indicators
        """
        dataframe['kama-short'] = ta.KAMA(dataframe, timeperiod=5)
        dataframe['kama-long'] = ta.KAMA(dataframe, timeperiod=20)

        dataframe['cci'] = ta.CCI(dataframe, timeperiod=21)
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)

        return dataframe
示例#3
0
    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:        

        dataframe['sar'] = ta.SAR(dataframe)
        dataframe['rmi'] = RMI(dataframe)
        dataframe['kama-3'] = ta.KAMA(dataframe, timeperiod=3)
        dataframe['kama-21'] = ta.KAMA(dataframe, timeperiod=21)

        macd = ta.MACD(dataframe)
        dataframe['macd'] = macd['macd']
        dataframe['macdsignal'] = macd['macdsignal']
        dataframe['macdhist'] = macd['macdhist']

        dataframe['volume_ma'] = dataframe['volume'].rolling(window=24).mean()

        
        return dataframe
示例#4
0
def KAMA(ohlcv, kw):
    """ :return Kaufman Adaptive Moving Average (kama) """
    params = {'timeperiod': 30}
    timeperiod = _get_params(kw, params, ['timeperiod'])[0]
    result = talib.KAMA(ohlcv, timeperiod)
    return {
        'kama': result
    }
示例#5
0
    def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Dynamic TA indicators
        Used so hyperopt can optimized around the period of various indicators
        """
        for kshort in range(kshortStart, (kshortEnd + 1)):
            dataframe[f'kama-short({kshort})'] = ta.KAMA(dataframe,
                                                         timeperiod=kshort)

        for klong in range(klongStart, (klongEnd + 1)):
            dataframe[f'kama-long({klong})'] = ta.KAMA(dataframe,
                                                       timeperiod=klong)

        for klong in range(klongStart, (klongEnd + 1)):
            dataframe[f'kama-long-slope({klong})'] = (
                dataframe[f'kama-long({klong})'] /
                dataframe[f'kama-long({klong})'].shift())

        for ccip in range(cciStart, (cciEnd + 1)):
            dataframe[f'cci({ccip})'] = ta.CCI(dataframe, timeperiod=ccip)

        for rsip in range(rsiStart, (rsiEnd + 1)):
            dataframe[f'rsi({rsip})'] = ta.RSI(dataframe, timeperiod=rsip)
        """
        Static TA indicators.
        Only used when --spaces does not include buy or sell
        """
        dataframe['cci'] = ta.CCI(dataframe, timeperiod=cciStatic)

        # RSI
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=rsiStatic)

        # KAMA - Kaufman Adaptive Moving Average
        dataframe['kama-short'] = ta.KAMA(dataframe,
                                          timeperiod=kamaShortStatic)
        dataframe['kama-long'] = ta.KAMA(dataframe, timeperiod=kamaLongStatic)
        dataframe['kama-long-slope'] = (dataframe['kama-long'] /
                                        dataframe['kama-long'].shift())

        return dataframe
    def populate_indicators(self, dataframe: DataFrame,
                            metadata: dict) -> DataFrame:
        if not metadata['pair'] in self.custom_trade_info:
            self.custom_trade_info[metadata['pair']] = {}

        ## Base Timeframe / Pair

        dataframe['kama'] = ta.KAMA(dataframe, length=233)

        # RMI: https://www.tradingview.com/script/kwIt9OgQ-Relative-Momentum-Index/
        dataframe['rmi'] = cta.RMI(dataframe, length=24, mom=5)

        # Momentum Pinball: https://www.tradingview.com/script/fBpVB1ez-Momentum-Pinball-Indicator/
        dataframe['roc-mp'] = ta.ROC(dataframe, timeperiod=1)
        dataframe['mp'] = ta.RSI(dataframe['roc-mp'], timeperiod=3)

        # MA Streak: https://www.tradingview.com/script/Yq1z7cIv-MA-Streak-Can-Show-When-a-Run-Is-Getting-Long-in-the-Tooth/
        dataframe['mastreak'] = cta.mastreak(dataframe, period=4)

        # Percent Change Channel: https://www.tradingview.com/script/6wwAWXA1-MA-Streak-Change-Channel/
        upper, mid, lower = cta.pcc(dataframe, period=40, mult=3)
        dataframe['pcc-lowerband'] = lower
        dataframe['pcc-upperband'] = upper

        lookup_idxs = dataframe.index.values - (
            abs(dataframe['mastreak'].values) + 1)
        valid_lookups = lookup_idxs >= 0
        dataframe['sbc'] = np.nan
        dataframe.loc[valid_lookups, 'sbc'] = dataframe['close'].to_numpy()[
            lookup_idxs[valid_lookups].astype(int)]

        dataframe['streak-roc'] = 100 * (dataframe['close'] -
                                         dataframe['sbc']) / dataframe['sbc']

        # Trends, Peaks and Crosses
        dataframe['candle-up'] = np.where(
            dataframe['close'] >= dataframe['close'].shift(), 1, 0)
        dataframe['candle-up-trend'] = np.where(
            dataframe['candle-up'].rolling(5).sum() >= 3, 1, 0)

        dataframe['rmi-up'] = np.where(
            dataframe['rmi'] >= dataframe['rmi'].shift(), 1, 0)
        dataframe['rmi-up-trend'] = np.where(
            dataframe['rmi-up'].rolling(5).sum() >= 3, 1, 0)

        dataframe['rmi-dn'] = np.where(
            dataframe['rmi'] <= dataframe['rmi'].shift(), 1, 0)
        dataframe['rmi-dn-count'] = dataframe['rmi-dn'].rolling(8).sum()

        dataframe['streak-bo'] = np.where(
            dataframe['streak-roc'] < dataframe['pcc-lowerband'], 1, 0)
        dataframe['streak-bo-count'] = dataframe['streak-bo'].rolling(8).sum()

        # Indicators used only for ROI and Custom Stoploss
        ssldown, sslup = cta.SSLChannels_ATR(dataframe, length=21)
        dataframe['sroc'] = cta.SROC(dataframe,
                                     roclen=21,
                                     emalen=13,
                                     smooth=21)
        dataframe['ssl-dir'] = np.where(sslup > ssldown, 'up', 'down')

        # Base pair informative timeframe indicators
        informative = self.dp.get_pair_dataframe(pair=metadata['pair'],
                                                 timeframe=self.inf_timeframe)

        # Get the "average day range" between the 1d high and 1d low to set up guards
        informative['1d-high'] = informative['close'].rolling(24).max()
        informative['1d-low'] = informative['close'].rolling(24).min()
        informative['adr'] = informative['1d-high'] - informative['1d-low']

        dataframe = merge_informative_pair(dataframe,
                                           informative,
                                           self.timeframe,
                                           self.inf_timeframe,
                                           ffill=True)

        # Other stake specific informative indicators
        # e.g if stake is BTC and current coin is XLM (pair: XLM/BTC)
        if self.config['stake_currency'] in ('BTC', 'ETH'):
            coin, stake = metadata['pair'].split('/')
            fiat = self.custom_fiat
            coin_fiat = f"{coin}/{fiat}"
            stake_fiat = f"{stake}/{fiat}"

            # Informative COIN/FIAT e.g. XLM/USD - Base Timeframe
            coin_fiat_tf = self.dp.get_pair_dataframe(pair=coin_fiat,
                                                      timeframe=self.timeframe)
            dataframe[f"{fiat}_rmi"] = cta.RMI(coin_fiat_tf, length=55, mom=5)

            # Informative STAKE/FIAT e.g. BTC/USD - Base Timeframe
            stake_fiat_tf = self.dp.get_pair_dataframe(
                pair=stake_fiat, timeframe=self.timeframe)
            dataframe[f"{stake}_rmi"] = cta.RMI(stake_fiat_tf,
                                                length=55,
                                                mom=5)

        # Informatives for BTC/STAKE if not in whitelist
        else:
            pairs = self.dp.current_whitelist()
            btc_stake = f"BTC/{self.config['stake_currency']}"
            if not btc_stake in pairs:
                self.custom_btc_inf = True
                # BTC/STAKE - Base Timeframe
                btc_stake_tf = self.dp.get_pair_dataframe(
                    pair=btc_stake, timeframe=self.timeframe)
                dataframe['BTC_rmi'] = cta.RMI(btc_stake_tf, length=55, mom=5)
                dataframe['BTC_close'] = btc_stake_tf['close']
                dataframe['BTC_kama'] = ta.KAMA(btc_stake_tf, length=144)

        # Slam some indicators into the trade_info dict so we can dynamic roi and custom stoploss in backtest
        if self.dp.runmode.value in ('backtest', 'hyperopt'):
            self.custom_trade_info[metadata['pair']]['sroc'] = dataframe[[
                'date', 'sroc'
            ]].copy().set_index('date')
            self.custom_trade_info[metadata['pair']]['ssl-dir'] = dataframe[[
                'date', 'ssl-dir'
            ]].copy().set_index('date')
            self.custom_trade_info[
                metadata['pair']]['rmi-up-trend'] = dataframe[[
                    'date', 'rmi-up-trend'
                ]].copy().set_index('date')
            self.custom_trade_info[
                metadata['pair']]['candle-up-trend'] = dataframe[[
                    'date', 'candle-up-trend'
                ]].copy().set_index('date')

        return dataframe
示例#7
0
],
                     axis=1)
input_df.columns = map(lambda x: x.lower(), data.columns.tolist())
input_array = dict(zip(input_df.columns, input_df.as_matrix().T))

#####################################
# Overlap

# SMA (for MA's, by default: timeperiod==30, price='close'
log_close_sma = abstract.SMA(input_df, timeperiod=30, price='close')
# EMA
log_close_ema = abstract.EMA(input_df)
# WMA
log_close_wma = abstract.EMA(input_df)
# KAMA - Kaufman Adaptive Moving Average
log_close_kama = abstract.KAMA(input_df)
# MIDPRICE - avg(highest high - lowest low) within the lookback period (default 14)
log_midpr = abstract.MIDPRICE(input_df)
# MIDPOINT - avg(highest close - lowest close) within the lookback period (default 14)
log_midpo = abstract.MIDPOINT(input_df)
# Parabolic Stop and Reverse (SAR) - calculate trailing stop points for long and short positions. SAR = P + A(H-P)
log_sar = abstract.SAR(input_df)
# Bollinger Bands
log_bband_upper, log_bband_middle, log_bband_lower = abstract.BBANDS(
    input_array, 20, 3, 3)
log_bband_upper = pd.DataFrame(log_bband_upper, index=input_df.index)
log_bband_middle = pd.DataFrame(log_bband_middle, index=input_df.index)
log_bband_lower = pd.DataFrame(log_bband_lower, index=input_df.index)

# plot
fig1 = plt.figure(figsize=(12, 9))
    def populate_indicators(self, dataframe: DataFrame,
                            metadata: dict) -> DataFrame:
        # Populate/update the trade data if there is any, set trades to false if not live/dry
        self.custom_trade_info[metadata['pair']] = self.populate_trades(
            metadata['pair'])

        # Set up primary indicators
        dataframe['rmi-slow'] = RMI(dataframe, length=20, mom=5)
        dataframe['rmi-fast'] = RMI(dataframe, length=9, mom=3)
        dataframe['vidya'] = VIDYA(dataframe)

        macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9)
        dataframe['macd'] = macd['macd']
        dataframe['macdsignal'] = macd['macdsignal']
        dataframe['macdhist'] = macd['macdhist']

        dataframe['kama-fast'] = ta.KAMA(dataframe, timeperiod=5)
        dataframe['kama-slow'] = ta.KAMA(dataframe, timeperiod=13)

        # Trend Calculations
        dataframe['rmi-up'] = np.where(
            dataframe['rmi-slow'] >= dataframe['rmi-slow'].shift(), 1, 0)
        dataframe['rmi-dn'] = np.where(
            dataframe['rmi-slow'] <= dataframe['rmi-slow'].shift(), 1, 0)
        dataframe['rmi-up-trend'] = np.where(
            dataframe['rmi-up'].rolling(3, min_periods=1).sum() >= 2, 1, 0)
        dataframe['rmi-dn-trend'] = np.where(
            dataframe['rmi-dn'].rolling(3, min_periods=1).sum() >= 2, 1, 0)

        dataframe['vdy-up'] = np.where(
            dataframe['vidya'] >= dataframe['vidya'].shift(), 1, 0)
        dataframe['vdy-dn'] = np.where(
            dataframe['vidya'] <= dataframe['vidya'].shift(), 1, 0)
        dataframe['vdy-up-trend'] = np.where(
            dataframe['vdy-up'].rolling(3, min_periods=1).sum() >= 2, 1, 0)
        dataframe['vdy-dn-trend'] = np.where(
            dataframe['vdy-dn'].rolling(3, min_periods=1).sum() >= 2, 1, 0)

        # Informative for STAKE/FIAT and COIN/FIAT on default timeframe, only relevant if stake currency is BTC or ETH
        if self.config['stake_currency'] in ('BTC', 'ETH'):
            coin, stake = metadata['pair'].split('/')
            coin_fiat = f"{coin}/{self.custom_fiat}"
            stake_fiat = f"{self.config['stake_currency']}/{self.custom_fiat}"

            coin_fiat_inf = self.dp.get_pair_dataframe(
                pair=coin_fiat, timeframe=self.timeframe)
            stake_fiat_inf = self.dp.get_pair_dataframe(
                pair=stake_fiat, timeframe=self.timeframe)

            dataframe[f"{self.custom_fiat}_rmi-slow"] = RMI(coin_fiat_inf,
                                                            length=20,
                                                            mom=5)
            dataframe[f"{self.config['stake_currency']}_rmi-slow"] = RMI(
                stake_fiat_inf, length=20, mom=5)

        # Informative for current pair on inf_timeframe
        informative = self.dp.get_pair_dataframe(pair=metadata['pair'],
                                                 timeframe=self.inf_timeframe)

        inf_macd = ta.MACD(informative,
                           fastperiod=12,
                           slowperiod=26,
                           signalperiod=9)
        informative['macd'] = inf_macd['macd']
        informative['macdsignal'] = inf_macd['macdsignal']
        informative['macdhist'] = inf_macd['macdhist']

        dataframe = merge_informative_pair(dataframe,
                                           informative,
                                           self.timeframe,
                                           self.inf_timeframe,
                                           ffill=True)

        return dataframe