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/

    Sizer based on ATR

    General Code/Help
    4
    5
    397
    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.
    • KonstantinKlychkov
      KonstantinKlychkov last edited by

      Hi!
      I try to implement Sizer based on ATR using bt.indicators.ATR. As a result, I get an error which I don't know how to solve, please help to fix that.

      class AtrBasedPositionSize(bt.Sizer):
          # list of parameters which are configurable for the sizer
          params = (('period', 20),
                    ('risk', 0.05),
                    ('ATR_multiple', 1.5))
      
          def _getsizing(self, comminfo, cash, data, isbuy):
              if(len(data)<=self.p.period):
                  return 0
      
              self.ticker_ATR = bt.indicators.ATR(data, period=self.p.period)
              position_size = int((self.p.risk * self.broker.getvalue()) // (self.p.ATR_multiple * self.ticker_ATR))
              return position_size
      
      ...
      
      cerebro.addsizer(AtrBasedPositionSize)
      

      ERROR:
      position_size = int((self.p.risk * self.broker.getvalue()) // (self.p.ATR_multiple * self.ticker_ATR))
      TypeError: int() argument must be a string, a bytes-like object or a number, not 'LinesOperation'

      1 Reply Last reply Reply Quote 0
      • alexbrillant
        alexbrillant last edited by

        either self.p.ATR_multiple, or self.ticker_ATR is a LinesOperation object. LinesOperation must have a property for the value that you are looking for.

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

          Define self.ticker_ATR in the sizer's __init__(), then use self.ticker_ATR[0] in _getsizing() method. Should work.

          If you use ATR in your strategy for other needs, than you can define it on the strategy level once and call in the _getsizing() method.

          • 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
          KonstantinKlychkov Linuxpower Ludo 2 Replies Last reply Reply Quote 2
          • KonstantinKlychkov
            KonstantinKlychkov @ab_trader last edited by

            @ab_trader thanks, that works!

            1 Reply Last reply Reply Quote 1
            • Linuxpower Ludo
              Linuxpower Ludo @ab_trader last edited by

              @ab_trader said in Sizer based on ATR:

              Define self.ticker_ATR in the sizer's __init__(), then use self.ticker_ATR[0] in _getsizing() method. Should work.

              If you use ATR in your strategy for other needs, than you can define it on the strategy level once and call in the _getsizing() method.

              Hi @ab_trader ,

              I try to apply your advices but i have an error :

                  class LongOnly(bt.Sizer):
                      def __init__(self):
                          self.dataatr=bt.indicators.ATR(data, period=20)
              
                      def _getsizing(self, comminfo, cash, data, isbuy):
                          if isbuy:
                              divide = math.floor(5000/self.dataatr[0])
                              self.p.stake = divide
                              return self.p.stake
                          # Sell situation
                          position = self.broker.getposition(data)
                          if not position.size:
                              return 0  # do not sell if nothing is open
                          return self.p.stake
              
              self.lines.truehigh = Max(self.data.high, self.data.close(-1))
              

              File "F:\Software\Python\Python3.6\lib\site-packages\backtrader\lineseries.py", line 461, in getattr
              return getattr(self.lines, name)
              AttributeError: 'Lines_LineSeries_LineSeriesStub' object has no attribute 'high'

              Can you help me please ?

              Regards
              Ludo

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