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/

    Array index out of range

    General Code/Help
    2
    2
    168
    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.
    • P
      Pave last edited by

      I am trying to create my own indicator. I want to calculate an "ADL" line. Here is my code:

          def __init__(self):
              # Add min Period
              self.addminperiod(self.params.period)
      
              # Aliases to avoid long lines
              c = self.data.close
              h = self.data.high
              l = self.data.low
              v = self.data.volume
      
              self.data.m = bt.If(bt.Or(bt.And(c == h, c == l), h == l), 0, bt.DivByZero((2 * c - l - h), (h - l), zero=self.p.safezero) * v)
              self.data.adl = self.data.m[-1] + self.data.m
      

      m is calculated correctly. However as ADL requires the current m value and the previous m I ran into a problem:

      array index out of range
      

      As m isn't available in the first calculation (I think). So I tried to eliminate the error:

      self.data.adl = bt.If(self.data.m[-1] in locals(),self.data.m[-1],0) + self.data.m
      

      Unfortunately this doesn't work. I am very new to backtrader. Maybe some of you can help me.

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        You can not use [] notation in the init(), only () notation.

        Refer to Docs - Platform Concepts - Delayed Indexing.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors