Backtrader Community

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

    My code may be in a endless loop, what happened?

    General Code/Help
    3
    9
    1205
    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.
    • M
      Mike Van last edited by

      This is my code on strategy

      class Turtle(bt.Strategy):
      
          def __init__(self):
              self.ATR = list(range(len(self.datas))) 
              for i in range(len(self.datas)):
                  self.ATR[i] = bt.talib.ATR([self.datas[i].high, self.datas[i].low, self.datas[i].close], timeperiod=20) # P227
      
              self.sys = None 
              self.lasttrade = None
      

      I used many datafeeds. When I use this code, the program never runs out.
      When I commented out this code as follow

       self.ATR = list(range(len(self.datas))) 
              for i in range(len(self.datas)):
                  self.ATR[i] = bt.talib.ATR([self.datas[i].high, self.datas[i].low, self.datas[i].close], timeperiod=20) # P227
      

      Everything will be OK. What happened?
      I hope someone can help me.

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

        Any reason as to why you pass a list to ATR?

        1 Reply Last reply Reply Quote 0
        • M
          Mike Van last edited by Mike Van

          @backtrader
          I have many stocks, so I just want to store ATRs by a list.
          My aim is:

          self.ATR[0] = datas[0]'s ATR
          self.ATR[1] = datas[1]'s ATR
          ......
          

          Now I use bt.indicators.ATR can achieve this goal:

          self.ATR = list(range(len(self.datas))) 
          for index, stock_data in enumerate(self.datas): 
                  self.ATR[index] = bt.indicators.ATR(self.datas[index], period=20)
          

          But I still want to know why Ta-lib doesn't work. What is wrong with me?

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

            If you change you talib.ATR call as follows it might also work:

            self.ATR[i] = bt.talib.ATR(self.datas[i].high, self.datas[i].low, self.datas[i].close, timeperiod=20)
            
            • 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
            M 1 Reply Last reply Reply Quote 1
            • B
              backtrader administrators @Mike Van last edited by

              @mike-van said in My code may be in a endless loop, what happened?:

              I have many stocks, so I just want to store ATRs by a list.

              The question was not why you store the ATRs in a list.

              @backtrader said in My code may be in a endless loop, what happened?:

              Any reason as to why you pass a list to ATR?

              The question is why you pass a list to ATR.

              See the answer by @ab_trader

              M 1 Reply Last reply Reply Quote 0
              • M
                Mike Van @ab_trader last edited by

                @ab_trader Thanks a lot. Let me try again.

                1 Reply Last reply Reply Quote 0
                • M
                  Mike Van @backtrader last edited by

                  @backtrader
                  I just define self.ATR as a list to store ATRs of different stocks.
                  What the better method should I use to store different stocks' indicators?

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

                    Please, re-read the messages above. Nobody was talking about how you stored the ATRs.

                    M 1 Reply Last reply Reply Quote 0
                    • M
                      Mike Van @backtrader last edited by

                      @backtrader ohoh I get it. My English understanding is really poor. Thanks for your patience.

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