Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Problem with creating a custom indicator

    General Code/Help
    3
    5
    1734
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kazi last edited by

      Hi guys,

      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[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 "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[0])
        File "/usr/local/lib/python3.6/dist-packages/backtrader/lineseries.py", line 467, in __getitem__
          return self.lines[0][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.

      Thanks

      B Y 2 Replies Last reply Reply Quote 0
      • B
        backtrader administrators @kazi last edited by

        @kazi said in Problem with creating a custom indicator:

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

        Passing an array is likely to break things

        1 Reply Last reply Reply Quote 0
        • Y
          Yelloww @kazi last edited by

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

          1 Reply Last reply Reply Quote 0
          • K
            kazi last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors