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 can I create a sizer based on value?

    General Code/Help
    1
    2
    104
    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.
    • C
      chrix last edited by

      I'm having trouble using the PercentSizer for live trading, because if I have a big short position what the sizer sees as cash is far from the real cash in my account. So I'd like to build a custom sizer that does exactly the same, but sizing on a percentage of Value instead of cash. This is what I tried, without success.

      class ValuePercentSizer(bt.Sizer):
          params = (
              ('percents', 20),
              ('retint', True),
          )
          def __init__(self):
              pass
      
          def _getsizing(self, comminfo, value, data, isbuy):
              position = self.broker.getposition(data)
              if not position:
                  size = value / data.close[0] * (self.params.percents / 100)
              else:
                  size = position.size
      
              if self.p.retint:
                  size = int(size)
      
              return size
      
      C 1 Reply Last reply Reply Quote 0
      • C
        chrix @chrix last edited by

        @chrix I solved it like this

        class ValuePercentSizer(bt.Sizer):
            params = (
                ('percents', 20),
                ('retint', True),# return an int size or rather the float value
            )
        
            def __init__(self):
                pass
        
            def _getsizing(self, comminfo, cash, data, isbuy):
                position = self.broker.getposition(data)
                accvalue = self.broker.getvalue()
                if not position:
                    size = accvalue / data.close[0] * (self.params.percents / 100)
                else:
                    size = position.size
        
                if self.p.retint:
                    size = int(size)
        
                return size
        
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors