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/

    Start with existing position, encounter IndexError: array index out of range

    General Discussion
    3
    5
    748
    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.
    • RealZ
      RealZ last edited by

      I am using oanda v20 broker, when there is no existing position, it works well. but if I place an order before the strategy run, it throw the below error, why?

      File "xxxxmain.py", line 46, in <module>
      cerebro.run()
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\cerebro.py", line 1127, in run
      runstrat = self.runstrategies(iterstrat)
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies
      self._runnext(runstrats)
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\cerebro.py", line 1630, in _runnext
      strat._next()
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\strategy.py", line 351, in _next
      self._next_observers(minperstatus)
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\strategy.py", line 379, in _next_observers
      observer._next()
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\lineiterator.py", line 277, in _next
      self.nextstart() # only called for the 1st value
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\lineiterator.py", line 342, in nextstart
      self.next()
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\observers\trades.py", line 148, in next
      if trade.data not in self.datas:
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\lineroot.py", line 281, in eq
      return self._operation(other, operator.eq)
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\lineroot.py", line 88, in _operation
      return self._operation_stage2(other, operation, r=r)
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\lineroot.py", line 209, in _operation_stage2
      other = other[0]
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\lineseries.py", line 473, in getitem
      return self.lines[0][key]
      File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\linebuffer.py", line 165, in getitem
      return self.array[self.idx + ago]
      IndexError: array index out of range

      ==========================
      I have to edit it like this:
      C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\linebuffer.py

      def getitem(self, ago):
      if self.idx == -1:
      return None

      return self.array[self.idx + ago]

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @RealZ last edited by

        @realz said in Start with existing position, encounter IndexError: array index out of range:

        why?

        The question cannot be answered. You only show the error the Python interpreter has produced.

        Even if the Oandav20 broker is an independent project, the error seems 100% related to a mistake in your code, given the trace. But it's impossible to say.

        RealZ 1 Reply Last reply Reply Quote 0
        • RealZ
          RealZ @backtrader last edited by

          @backtrader my code is very common, I think.

          cerebro = bt.Cerebro()
          # oandastore = bt.stores.OandaStore(token='apikey', account=acc, practice=True)
          oandastore = btoandav20.stores.OandaV20Store(token=apikey, account=acc, practice=not isLive)
          cerebro.broker = oandastore.getbroker()

          # data0 = oandastore.getdata(dataname="EUR_USD", timeframe=bt.TimeFrame.Seconds, compression=5, backfill_start=False, backfill=False)
          data0 = btoandav20.feeds.OandaV20Data(
              dataname=dname,
              timeframe=bt.TimeFrame.Seconds, compression=10, backfill_start=False, backfill=False,
              name="mshort",candles=True
          )
          
          data1 = btoandav20.feeds.OandaV20Data(
              dataname=dname,
              timeframe=bt.TimeFrame.Minutes, compression=15, backfill_start=True, backfill=True,
              name="mlong",
              candles=True
          )
          #This is setting what timeframe we want to use.
          cerebro.addsizer(bt.sizers.FixedSize, stake=5000)
          cerebro.adddata(data0)
          # cerebro.resampledata(data0, timeframe=bt.TimeFrame.Seconds, compression=15,name="mlong",backfill=True)
          cerebro.adddata(data1)
          cerebro.addstrategy(WinStrategy1, printlog=True)
          cerebro.run()
          
          D 1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by backtrader

            First thing when posting is reading how to post (at the top of each page in the forum). That helps.

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

            If you don't do this, your code become basically unreadable.

            In any case your code is not complete. From the log it seems an error related to observers. Not much more can be said.

            1 Reply Last reply Reply Quote 0
            • D
              dasch @RealZ last edited by

              @realz you have one data feed with backfilled data (it will fetch 500 entries from the past at startup), the other does not. Maybe this causes some problems too?

              For the data, as soon, as your data will come live, you will not get S10 and M15 data but instead tick data.

              to get resampled data live you would use:

              # cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=15,name="mlong")
              

              You can also count the amount of lines before trying to access them?

              But without code there It is hard to say, what's not working.

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