Array index out of range for lower period params
Hello! So I coded a bit with backtrader, my backtests works just as intended to a point. When I change the period of Envelope to 16 and below (I swear some tweaks ago it was 22 and below...) I get an error:
Traceback (most recent call last): File "F:/xxx/Showoff.py", line 102, in <module> Cerebro.run() File "F:\xxx\venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "F:\xxx\venv\lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies self._runnext(runstrats) File "F:\xxx\venv\lib\site-packages\backtrader\cerebro.py", line 1630, in _runnext strat._next() File "F:\xxx\venv\lib\site-packages\backtrader\strategy.py", line 347, in _next super(Strategy, self)._next() File "F:\xxx\venv\lib\site-packages\backtrader\lineiterator.py", line 263, in _next indicator._next() File "F:\xxx\venv\lib\site-packages\backtrader\lineiterator.py", line 263, in _next indicator._next() File "F:\xxx\venv\lib\site-packages\backtrader\linebuffer.py", line 621, in _next self.nextstart() File "F:\xxx\venv\lib\site-packages\backtrader\lineroot.py", line 144, in nextstart self.next() File "F:\xxx\venv\lib\site-packages\backtrader\linebuffer.py", line 744, in next self = self.operation(self.a, self.b) File "F:\xxx\venv\lib\site-packages\backtrader\linebuffer.py", line 163, in __getitem__ return self.array[self.idx + ago] IndexError: array index out of range Process finished with exit code 1
The problem is it only works on some datasets, for some it works with any period.
The probe of the code that works standalone and causes the error is:
class ATRP(bt.indicators.AverageTrueRange): def next(self): self.lines.atr = (self.lines.atr / self.data) * 100 class MovingAverageSimpleEnvelope(bt.indicators.MovingAverageSimpleEnvelope): params = (("period", 10),) class Envelope_strategy(bt.Strategy): def __init__(self): self.ATRP = ATRP(data_1d) self.perc = self.ATRP / 2 self.Envelope = MovingAverageSimpleEnvelope(data_1h, perc = self.perc)
I think I understand what is the problem, it's because I set Envelope perc as the ATRP I created. When it's set as a fixed int it works perfectly. So my small brain thinks that with a lower period the Envelope tries to plots sooner, before the ATRP gives data (and it requires the data from it) and it causes the error. I'm not sure how could I fix if that's the problem, make the Envelope wait a bit? (But I think it's implemented in Cerbero to some extent). Or remove some rows from Envelope data so it's equal? Thanks for help!
I noticed that if I drop 5 first rows of the dataframe the datas use (data_1d is resampled data_1h so they are born from the same dataframe) it happens on different datasets. I guess it's some issue with coupling? Or it's maybe when the backtest start's in the middle of the week or something? So it makes me think even more I have to trim the data somehow to make it work.
When I drop a few rows it works with a particular period. To be exact, when I drop 12 rows (12 hours of data), I'm able to use 10-period envelope (for 9 period it crashes). When I drop 13 rows, I can use 9 (and it goes to 21 rows for period 1). When I delete more than 21 it crashes again for numbers it didn't crash before removing that much. I can see that it's connected to those periods and the amount of it or something but I'm going insane a bit as I can't wrap my head around what's the exact problem.
I made it work. I had to delete enough rows so the 1h dataframe that the data is taken from starts on any day but at 23:00:00. I still don't know why it matters and how. I guess it's coding in a nutshell (fixing the error I don't know why exists by guessing). I can only guess, maybe data resampling from 1 hour to 1 day only occurs when there is a full day available (from 00:00 to 24:00)? And when the data stated on for ex. 2:00, there were 22 1h candles that envelope wanted to plot on (with a lower period than 22 it could), where ATR couldn't even start as there was no 1D candle. And when it starts at 23:00 both can start plotting on next candle. If someone knows what is/was the issue I'm happy to listen, hope it won't show again at some point.
ab_trader last edited by
In order to understand what is going on in your script, log the values of the prices and indicators from the
next()calls on an hourly basis. In this case you will move from