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
    3
    4
    127
    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.

          KonstantinKlychkov 1 Reply Last reply Reply Quote 2
          • KonstantinKlychkov
            KonstantinKlychkov @ab_trader last edited by

            @ab_trader thanks, that works!

            1 Reply Last reply Reply Quote 1
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }