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

Array index out of range

  • 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
            # Aliases to avoid long lines
            c =
            h =
            l =
            v =
   = 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)
   =[-1] +

    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: = bt.If([-1] in locals(),[-1],0) +

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

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

    Refer to Docs - Platform Concepts - Delayed Indexing.

Log in to reply