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/

    How to build a sizer that can use some measure of volatility, like ATR, and the multiplier for the future to determine position sizing?

    General Code/Help
    3
    3
    343
    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.
    • T
      t3chap last edited by

      The following is my attempt to make a custom position sizer based on ATR and the multiplier for the given future. I'm sure this is relatively easy but I seem to have hit a wall.

      I seem to have a position size of 0 for every trade.
      What am I getting wrong?

      Thanks.

      class comm_sizer(bt.Sizer):
          def __init__(self, atr):
              #self.atr_parameter= atr_parameter
              atr= self.atr
              risk_per_trade=5000
              atr_risk_per_trade=5
              
              sizing = math.floor((risk_per_trade/(mult*atr_risk_per_trade*atr)))
              
         # position = self.broker.getposition(data)
          
          def _getsizing(self,comminfo,cash,data,isbuy):
              
              return sizing
      
      B 1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        You may have already checked this, but in case you haven't.

        Getting zero size for every trade could imply there's not enough cash to execute the transaction. Check to see if you are getting margined out. Read "Orders -Order Status Values"

        RunBacktest.com

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

          @t3chap said in How to build a sizer that can use some measure of volatility, like ATR, and the multiplier for the future to determine position sizing?:

          I seem to have a position size of 0 for every trade.

          With the following code ...

          @t3chap said in How to build a sizer that can use some measure of volatility, like ATR, and the multiplier for the future to determine position sizing?:

          class comm_sizer(bt.Sizer):
              def __init__(self, atr):
                  #self.atr_parameter= atr_parameter
                  atr= self.atr
                  risk_per_trade=5000
                  atr_risk_per_trade=5
                  
                  sizing = math.floor((risk_per_trade/(mult*atr_risk_per_trade*atr)))
                  
             # position = self.broker.getposition(data)
              
              def _getsizing(self,comminfo,cash,data,isbuy):
                  
                  return sizing
          

          It would be a miracle if you get something other than an Exception, both in __init__ and in _getsizing.

          In any case to use the ATR you probably want to access the strategy attributes (where the ATR will have been instantiated), taking advantage of the fact that the sizers have an attribute member called strategy

          • https://www.backtrader.com/docu/sizers/sizers/
          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors