Problem with creating a custom indicator
I am trying to create a custom indicator, I am following https://www.backtrader.com/docu/inddev.html:
I am using the indicator code from the above tutorial which is as follows:
class SimpleMovingAverage1(bt.Indicator): lines = ('sma',) params = (('period', 20),) def __init__(self): self.addminperiod(self.params.period) def next(self): datasum = math.fsum(self.data.get(size=self.p.period)) self.lines.sma = datasum / self.p.period
I have defined it under def init(self): as follows:
self.sma = SimpleMovingAverage1(self.datas, period=20)
The issue is when I try to print out the out put of the indicator I am getting the following error:
Starting Portfolio Value: 0.02100000 2018-02-06, Close, 0.00098700 2018-02-06, Current balance: 0.02100000, Profit: 0.00000000 Traceback (most recent call last): File "machine_engine-test.orig.py", line 213, in <module> thestrats = cerebro.run(runonce=False) File "/usr/local/lib/python3.6/dist-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/usr/local/lib/python3.6/dist-packages/backtrader/cerebro.py", line 1295, in runstrategies self._runnext(runstrats) File "/usr/local/lib/python3.6/dist-packages/backtrader/cerebro.py", line 1626, in _runnext strat._next() File "/usr/local/lib/python3.6/dist-packages/backtrader/strategy.py", line 325, in _next super(Strategy, self)._next() File "/usr/local/lib/python3.6/dist-packages/backtrader/lineiterator.py", line 268, in _next self.nextstart() # only called for the 1st value File "/usr/local/lib/python3.6/dist-packages/backtrader/lineiterator.py", line 342, in nextstart self.next() File "machine_engine-test.orig.py", line 153, in next self.log('SMA %.8f' % self.sma) File "/usr/local/lib/python3.6/dist-packages/backtrader/lineseries.py", line 467, in __getitem__ return self.lines[key] File "/usr/local/lib/python3.6/dist-packages/backtrader/linebuffer.py", line 163, in __getitem__ return self.array[self.idx + ago] IndexError: array index out of range
I am not sure why I am getting this error, from my understanding the self.addminperiod(self.params.period) option is supposed to inform the system to backfill x amount of data.
Your help is greatly appreciated.
@kazi I think you should use SimpleMovingAverage1(self.data, period=20) or SimpleMovingAverage1(self.datas, period=20)
Thanks a lot for your assistance, that solved the issue of:
IndexError: array index out of range
But I noticed that the indicator does not seem to get the addminperiod data before beginning its operations, the initial output of this indicator is NAN.
Thank you in advance.
It could be adding
NaNvalues itself ... we can't know. Easy way:
When you get to
nextin your indicator
- print the
len(self), so you'll know when it was first invoked
- print the actual values being gathered by
That will for sure help you and in case that's not enough it will help the others see if they can help. The more the things you let others know the easier it will be.
- print the