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



  • 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.



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

    Refer to Docs - Platform Concepts - Delayed Indexing.


Log in to reply
 

});