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

Problem with creating a custom indicator

  • Hi guys,

    I am trying to create a custom indicator, I am following

    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):
        def next(self):
            datasum = math.fsum(
            self.lines.sma[0] = 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 "", line 213, in <module>
        thestrats =
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", line 1295, in runstrategies
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", line 1626, in _runnext
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", line 325, in _next
        super(Strategy, self)._next()
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", line 268, in _next
        self.nextstart()  # only called for the 1st value
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", line 342, in nextstart
      File "", line 153, in next
        self.log('SMA %.8f' % self.sma[0])
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", line 467, in __getitem__
        return self.lines[0][key]
      File "/usr/local/lib/python3.6/dist-packages/backtrader/", 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.


  • administrators

    @kazi said in Problem with creating a custom indicator:

     self.sma = SimpleMovingAverage1(self.datas, period=20)

    Passing an array is likely to break things

  • @kazi I think you should use SimpleMovingAverage1(, period=20) or SimpleMovingAverage1(self.datas[0], period=20)

  • Hi guys,

    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.

  • administrators

    It could be adding NaN values itself ... we can't know. Easy way:

    When you get to next in your indicator

    • print the len(self), so you'll know when it was first invoked
    • print the actual values being gathered by get(size=self.p.period)

    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.

Log in to reply