For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

I seem to have some problems when I trade with CCXT

  • This code is correct when doing a backtest.But when I pass in real-time data from CCXT, I get an error.I used the CCXT branch of backtrader.By the way, how to set the leverage ratio for Bitmex in this branch.Thx.

    class HFT(bt.Strategy):
        params = (
            ('highest_high', 20), 
            ('StdDev', 100),  
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0]
            print('%s, %s' % (dt.isoformat(), txt))
        def __init__(self):
            self.dataclose = self.datas[0].close
            self.datavolume = self.datas[0].volume
            self.datalow = self.datas[0].low
            self.sma_veryfast = btind.MovingAverageSimple(self.dataclose, period=10)
            self.sma_fast = btind.MovingAverageSimple(self.dataclose, period=20)
            self.sma_mid = btind.MovingAverageSimple(self.dataclose, period=50)
            self.sma_slow = btind.MovingAverageSimple(self.dataclose, period=100)
            self.sma_veryslow = btind.MovingAverageSimple(self.dataclose, period=200)
            # BollingerBandsWidth = upperband - lowerband/middleband.
            self.bbw = BollingerBandsW()
            self.boll = btind.BollingerBands()
            self.std = btind.StdDev(self.bbw.l.bbw, period=self.params.StdDev)
            self.lines_bbw = ( - / self.boll.l.mid
            self.volatility_level = VolatilityLevel()
            self.low_volatility_level = self.volatility_level.l.VLI_fast < self.volatility_level.l.VLI_slow
            self.high_volatility_level = self.volatility_level.l.VLI_fast > self.volatility_level.l.VLI_slow
            self.extreme_volatility_level = self.bbw.l.bbw > self.volatility_level.l.VLI_top
            self.vol_condition = (btind.MovingAverageSimple(self.datavolume, period=10) >
                                  btind.MovingAverageSimple(self.datavolume, period=50))
            self.crossdown_boll_top = bt.ind.CrossDown(self.dataclose,
            self.crossup_boll_bot = bt.ind.CrossUp(self.dataclose,
            self.highest_high = btind.Highest(self.dataclose, period=self.params.highest_high)
            self.low_of_last_candle = self.datalow[0]
            self.close_of_price = self.dataclose[0]# IndexError: array index out of range
            self.stop_win = None
            self.stop_loss = None
            self.order = None
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        hist_start_date = datetime.utcnow() - timedelta(minutes=10)
        cerebro = bt.Cerebro()
        broker_config = {
            # 'verbose': True,
            'apiKey': 'xxxx',
            'secret': "xxx"
        broker = bt.brokers.CCXTBroker(exchange='bitmex', currency='BTC/USD', config=broker_config)
        hist_start_date = datetime.utcnow() - timedelta(minutes=15)
        data = bt.feeds.CCXT(
        # Add the Data Feed to Cerebro
    Traceback (most recent call last):
      File "C:/Users/Administrator/PycharmProjects/this/", line 201, in <module>
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\", line 1217, in runstrategies
        strat = stratcls(*sargs, **skwargs)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\", line 88, in __call__
        _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\", line 78, in doinit
        _obj.__init__(*args, **kwargs)
      File "C:/Users/Administrator/PycharmProjects/this/", line 89, in __init__
        self.low_of_last_candle = self.datalow[0]
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\", line 163, in __getitem__
        return self.array[self.idx + ago]
    IndexError: array index out of range

  • @kemoduodejuxi said in I seem to have some problems when I trade with CCXT:

    self.low_of_last_candle = self.datalow[0]

    You are trying to access the datafeed's data in your __init__ method. The datafeed will have no data loaded at this point.

    Please see the following docs:

Log in to reply