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

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

  • 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 "", line 46, in <module>
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 1298, in runstrategies
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 1630, in _runnext
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 351, in _next
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 379, in _next_observers
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", 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\", line 342, in nextstart
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\observers\", line 148, in next
    if not in self.datas:
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 281, in eq
    return self._operation(other, operator.eq)
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", 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\", line 209, in _operation_stage2
    other = other[0]
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 473, in getitem
    return self.lines[0][key]
    File "C:\Users\xxxx\AppData\Local\conda\conda\envs\py36\Lib\site-packages\backtrader\", line 165, in getitem
    return self.array[self.idx + ago]
    IndexError: array index out of range

    I have to edit it like this:

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

    return self.array[self.idx + ago]

  • administrators

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


    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.

  • @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) = oandastore.getbroker()

    # data0 = oandastore.getdata(dataname="EUR_USD", timeframe=bt.TimeFrame.Seconds, compression=5, backfill_start=False, backfill=False)
    data0 = btoandav20.feeds.OandaV20Data(
        timeframe=bt.TimeFrame.Seconds, compression=10, backfill_start=False, backfill=False,
    data1 = btoandav20.feeds.OandaV20Data(
        timeframe=bt.TimeFrame.Minutes, compression=15, backfill_start=True, backfill=True,
    #This is setting what timeframe we want to use.
    cerebro.addsizer(bt.sizers.FixedSize, stake=5000)
    # cerebro.resampledata(data0, timeframe=bt.TimeFrame.Seconds, compression=15,name="mlong",backfill=True)
    cerebro.addstrategy(WinStrategy1, printlog=True)

  • administrators

    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:

    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.

  • @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.

Log in to reply