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/

    Got "IndexError: array index out of range" with current data

    Indicators/Strategies/Analyzers
    2
    3
    305
    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.
    • Yacc Don
      Yacc Don last edited by

      To access datetime in indicator next function, I pass self.datas[0].datetime as extra data:

      class UserStockStrategy(bt.Strategy):
          def __init__(self):
                self.indicator = my_indicator(self.datas[0], self.datas[0].datetime)
      
      class my_indicator(bt.Indicator):
          def next(self):
                print(self.datas[0][0])  #This is ok
                print( bt.num2date(self.datas[1][0] )  # This line give error at the final, while self.datas[0][0] is avaliable.
      

      I didn't access the futher data, why did I get IndexError.

      Could anybody help me, Thanks in advance

      @backtrader @ab_trader

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

        @yacc-don said in Got "IndexError: array index out of range" with current data:

        To access datetime in indicator next function, I pass self.datas[0].datetime as extra data

        no, you don't do this. you initiated indicator class with additional argument, but within indicator class this argument is not specified. calling

        self.datas[1]
        

        you just refer to 2nd data feed in the bt environment, which (my guess) doesn't exist.

        iirc data feeds are passed to the indicators as default, so this

        class UserStockStrategy(bt.Strategy):
            def __init__(self):
                  self.indicator = my_indicator()
        
        class my_indicator(bt.Indicator):
            def next(self):
                  print(self.datas[0][0])
                  print(self.datas[0].datetime)
        

        should give you what you want. maybe you need to massage second print statement a little bit, it was long time ago when i used it.

        • 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
        Yacc Don 1 Reply Last reply Reply Quote 2
        • Yacc Don
          Yacc Don @ab_trader last edited by

          @ab_trader Your solution is very good, thank you.

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