Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    I seem to have some problems when I trade with CCXT

    General Code/Help
    2
    2
    100
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • kemoduodejuxi
      kemoduodejuxi last edited by

      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].datetime.date(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.top - self.boll.l.bot) / 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.boll.top)
              self.crossup_boll_bot = bt.ind.CrossUp(self.dataclose, self.boll.bot)
      
              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)
          cerebro.setbroker(broker)
      
          hist_start_date = datetime.utcnow() - timedelta(minutes=15)
          data = bt.feeds.CCXT(
              exchange='bitmex',
              symbol='BTC/USD',
              timeframe=bt.TimeFrame.Minutes,
              fromdate=hist_start_date,
              historical='False',
              )
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
          cerebro.addstrategy(HFT)
          cerebro.run()
      
      
      Traceback (most recent call last):
        File "C:/Users/Administrator/PycharmProjects/this/bitmex_HFT.py", line 201, in <module>
          cerebro.run()
        File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1217, in runstrategies
          strat = stratcls(*sargs, **skwargs)
        File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\metabase.py", line 88, in __call__
          _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
        File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\metabase.py", line 78, in doinit
          _obj.__init__(*args, **kwargs)
        File "C:/Users/Administrator/PycharmProjects/this/bitmex_HFT.py", line 89, in __init__
          self.low_of_last_candle = self.datalow[0]
        File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\linebuffer.py", line 163, in __getitem__
          return self.array[self.idx + ago]
      IndexError: array index out of range
      
      
      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        @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: https://www.backtrader.com/docu/concepts/#lines-delayed-indexing

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors