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/

    IndexError: array index out of range

    Indicators/Strategies/Analyzers
    3
    14
    2747
    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.
    • kam dorra
      kam dorra last edited by

      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.

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

        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.

        • 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
        kam dorra B 2 Replies Last reply Reply Quote 1
        • kam dorra
          kam dorra @ab_trader last edited by

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

          A B 2 Replies Last reply Reply Quote 0
          • A
            ab_trader @kam dorra last edited by

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

            • 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
            • kam dorra
              kam dorra last edited by

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

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

                @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)
                
                1 Reply Last reply Reply Quote 2
                • kam dorra
                  kam dorra last edited by

                  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.

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

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

                    1 Reply Last reply Reply Quote 1
                    • kam dorra
                      kam dorra last edited by

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

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

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

                        • 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 2
                        • B
                          backtrader administrators last edited by

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

                          1 Reply Last reply Reply Quote 1
                          • kam dorra
                            kam dorra last edited by

                            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.

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

                              Read

                              • Docs - Platform Concepts - https://www.backtrader.com/docu/concepts/

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

                              1 Reply Last reply Reply Quote 2
                              • B
                                backtrader administrators @ab_trader last edited by

                                @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

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