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

`IndexError: array index out of range` with resampled data.



  • At Strategy __init__(), I make a indicator:

    def __init__(self):
            self.cmi = abs(self.data.close - self.data.close[-29]) / (btind.Highest(self.data.high, period=30, plot=False) - btind.Lowest(self.data.low, period=30, plot=False)) * 100
    

    when:

    data = bt.feeds.PandasData(
            dataname=df,
            timeframe=bt.TimeFrame.Minutes,
            compression=1)
    cerebro.adddata(data)
    

    It works.

    but, when:

    data = bt.feeds.PandasData(
            dataname=df,
            timeframe=bt.TimeFrame.Minutes,
            compression=1)
    cerebro.resampledata(
            data,
            timeframe=bt.TimeFrame.Minutes,
            compression=5)
    

    There is exception:

    Traceback (most recent call last):
      File "backtest.py", line 220, in <module>
        results = cerebro.run()
      File "d:\quant\backtrader\backtrader\cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "d:\quant\backtrader\backtrader\cerebro.py", line 1217, in runstrategies
        strat = stratcls(*sargs, **skwargs)
      File "d:\quant\backtrader\backtrader\metabase.py", line 88, in __call__
        _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
      File "d:\quant\backtrader\backtrader\metabase.py", line 78, in doinit
        _obj.__init__(*args, **kwargs)
      File "backtest.py", line 44, in __init__
        self.cmi = abs(self.data.close - self.data.close[-29]) / (btind.Highest(self.data.high, period=30, plot=False) - bti
    nd.Lowest(self.data.low, period=30, plot=False)) * 100
      File "d:\quant\backtrader\backtrader\linebuffer.py", line 163, in __getitem__
        return self.array[self.idx + ago]
    IndexError: array index out of range
    

    So, what is wrong?



  • I think the issue is that you use [...], but it should be (...). Not sure, why it worked on the non-resampled data feed.



  • @ab_trader

    Yes, (...) works. Thanks!

    But I dont know why...


  • administrators

    @g_will said in &#x60;IndexError: array index out of range&#x60; with resampled data.:

    But I dont know why...

    Because you haven't read the manual:


Log in to reply
 

});