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

IndexError: array index out of range



  • Why is this error message coming up with this line?

    # error message:
        return self.array[self.idx + ago]
    IndexError: array index out of range   
    
        def __init__(self):
    
            x = bt.If(d[-3] > e[-2] > f[-1], d[-1], float('Inf'))
            y = bt.If(d[-3] > e[-2] > f[-1], e[-1], float('Inf'))
            z = bt.If(y < float('Inf'), y[-1], float('Inf'))  # error message line
    

    Thanks.



  • In the __init()__ indexing should be done with (), not with [] brackets, ref Docs - Lines: DELAYED indexing:

        def __init__(self):
    
            x = bt.If(d(-3) > e(-2) > f(-1), d(-1), float('Inf'))
            y = bt.If(d(-3) > e(-2) > f(-1), e(-1), float('Inf'))
            z = bt.If(y < float('Inf'), y(-1), float('Inf'))  # error message line
    

    Also bt.If operates with arrays, but float('Inf') is not an array. Might be an error due to this reason also.



  • @ab_trader

    Thank you, I have replaced the [] with () and the out of range error has gone, but now I have another error:

        def __init__(self):
    
            x = bt.If(d(-3) > e(-2) > f(-1), d(-1), float('Inf'))  # new error here
    
     # error message: TypeError: __bool__ should return bool, returned LineOwnOperation     
    

    Thanks again.



  • @kam-dorra bt.If works with lines, and you are using float as one of the arguments. I would try the following to create a dummy line which then to be used in bt.If:

        def __init__(self):
    
            infline = bt.LineNum(float('Inf'))
            x = bt.If(d(-3) > e(-2) > f(-1), d(-1), infline)
            y = bt.If(d(-3) > e(-2) > f(-1), e(-1), infline)
            z = bt.If(y < infline, y(-1), infline)
    

    You also may want to remove x since it is the same as y and you don;t use it further.



  • @ab_trader said in IndexError: array index out of range:

    infline = bt.LineNum(float('Inf'))

    thanks, but the change to infline still produces the same error:
    TypeError: __bool__ should return bool, returned LineOwnOperation

    x is different from y, because it returns d(-1) instead of e(-1),
    unless I am stating the bt.If incorrectly.


  • administrators

    @kam-dorra said in IndexError: array index out of range:

    x = bt.If(d(-3) > e(-2) > f(-1), d(-1), float('Inf'))  # new error here
    

    The magic of "overloading" cannot handle the chained comparison: d(-3) > e(-2) > f(-1). Hence

    condition = bt.And(d(-3) > e(-2), e(-2) > f(-1))
    bt.If(condition, result_if_True, result_if_False)
    


  • Is bt.And only allowed to have 2 elements for the condition description as well?
    Would I be able to build or chain together a higher number of elements with and?
    for example:

    condition = bt.And(d(-3) > e(-2), e(-2) > f(-1)) \
                and bt.And(i(-5) > g(-4), g(-4) > d(-3))
    

    Thanks.


  • administrators

    @kam-dorra said in IndexError: array index out of range:

    and

    bt.And exists because and cannot be overloaded. But you can pass any number of arguments to bt.And or nest them.



  • @ab_trader said in IndexError: array index out of range:

    def __init__(self):
    

    Can bt.And be used like this?:

    condition = bt.And(A < float('Inf'), A > b(-1), A < c(-2), A > d(-3))
    

    would all the A's after the A < float('Int') be smaller than infinity?

    Thanks.



  • @kam-dorra A will not change. The result is the condition array which have True or False elements depending on the results of the logical And operation applied to the results of logical comparison of arrays A, b(-1), c(-2) and d(-3). Logical operations will be applied to each element of the arrays, original arrays A, b(-1), c(-2) and d(-3) will be kept the same.


  • administrators

    @ab_trader said in IndexError: array index out of range:

    A will not change.

    Indeed ... in Python

    if A < float('Inf') and A > b(-1):
       do_something_here()
    

    does also not change the value of A.



  • Test_A
    
        def __init__(self):
    
            self.data.high
            self.data.low
            self.data.close
    
    
        def next(self):
    
            h = self.data.high
            l = self.data.low
            c = self.data.close
            buy_signal = self.data.buy_signal = c[0] > h[-1] and (h[-3] > h[-2] > h[-1])
    
            if buy_signal:
                 self.buy()
    
            elif self.position:
                if c[0] < min(l[-3], l[-2], l[-1]):
                    self.close()
    
    Test_B
    
        def __init__(self):
    
            self.data.high
            self.data.low
            self.data.close
    
    
        def next(self):
    
            h = self.data.high
            l = self.data.low
            c = self.data.close
    
            # is the result of this logic below same as Test_A? if not, 
            # how to implement Test_A logic w/ bt.If/bt.And?
            pattern = self.data.pattern =  bt.And(h(-3) > h(-2), h(-2) > h(-1))
            A = self.data.A = bt.If(pattern, h(-1), float('Inf'))
            B = self.data.B = bt.If(A < float('Inf'), A(-1), float('Inf'))
            buy_signal = self.data.buy_signal = bt.If(B < float('Inf'), c(0) > B(-1), False)
    
            if buy_signal[0]:  # error here: "IndexError: array index out of range"
                 self.buy()
    
            elif self.position:
                if c[0] < min(l[-3], l[-2], l[-1]):
                    self.close()
    

    Test_B is my attempt to implement the same strategy as Test_A with the use of bt.If and bt.And.

    I get this error: "IndexError: array index out of range "

    Where are the flaws in the logic?

    How would I compose Test_A using bt.If and bt.And?

    Thanks.


  • administrators

    Read

    You may wish to concentrate on: Stage1, Stage 2, Indexing and Delayed Indexing


  • administrators

    @ab_trader said in IndexError: array index out of range:

    In the __init()__ indexing should be done with (), not with [] brackets, ref Docs - Lines: DELAYED indexing:

    Pointed out some days ago by @ab_trader


Log in to reply
 

});