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/

    trouble with feeds for _getsizing

    Indicators/Strategies/Analyzers
    3
    5
    69
    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.
    • V
      vaclavku last edited by

      Hello guys,
      I try to build a simple algo with percent based sizers.
      In method "def runstrat():", I simply call the sizer: "cerebro.addsizer(RiskSizer, rs=0.5)"

      The instance of the sizers is quite simple:

      class RiskSizer(bt.Sizer):
          params = (('rs', 0.03),)
      
          def _getsizing(self, comminfo, cash, data, isbuy):
              if isbuy == True:
                  size = (cash*self.params.rs)/data[0]
              else:
                  size = (cash*self.params.rs)/data[0] * -1
      
              return size
      

      It raises this error: KeyError

      For being sure, this - fixed version - works well (as expected):

      class RiskSizer(bt.Sizer):
          params = (('rs', 0.03),)
      
          def _getsizing(self, comminfo, cash, data, isbuy):
              if isbuy == True:
                  size = self.params.rs*10
              else:
                  size = self.params.rs*10 * -1
      
              return size
      

      In another version, the risk based sizers works good as well.
      As I try to find the cause of this misbehaviour, I've found the issues is related to the (prob.) missing parameters of the method: _getsizing (self, comminfo, cash, data, isbuy). It seems to me that the parameters are not fed.

      Any hints where to search for?
      v

      Note:
      The strategy is quite simple and straightforward:

              if self.position and self.buysell == -1:
                  """self.log('SELL CREATE, %.4f' % self.data.close[0])"""
                  self.sell()
      
              elif not self.position and self.buysell == 1:
                  self.buy()
      
      B 1 Reply Last reply Reply Quote 0
      • V
        vaclavku last edited by

        Hello everyone,
        I don't full grasp why, however when I try the sizer from library:

        class RiskSizer(bt.Sizer):
            params = (
                ('rs', 0.2),
                ('retint', False),  # 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)
                if not position:
                    size = math.floor((cash  * self.params.rs) / data.close[0])
                else:
                    size = position.size
        
                if self.p.retint:
                    size = int(size)
        
                return size
        

        It works fine. :-)

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

          @vaclavku try to call data.close[0] instead of data[0] in your implementation, may also work. Just a guess.

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

            @vaclavku said in trouble with feeds for _getsizing:

            It raises this error: KeyError

            And don't you believe that showing the actual error and not what you think is important ... could actually help?

            @vaclavku said in trouble with feeds for _getsizing:

            As I try to find the cause of this misbehaviour, I've found the issues is related to the (prob.) missing parameters of the method: _getsizing (self, comminfo, cash, data, isbuy). It seems to me that the parameters are not fed.

            And why does it seem that to you? Or is it just a bogus claim?

            @vaclavku said in trouble with feeds for _getsizing:

            however when I try the sizer from library:
            class RiskSizer(bt.Sizer):
            params = (
            ('rs', 0.2),
            ('retint', False), # 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)
                if not position:
                    size = math.floor((cash  * self.params.rs) / data.close[0])
                else:
                    size = position.size
            
                if self.p.retint:
                    size = int(size)
            
                return size
            

            It works fine. :-)

            That's NOT and Sizer from the library. It's something you have modified.

            1 Reply Last reply Reply Quote 0
            • V
              vaclavku last edited by

              Many thanks. I'm enjoying this nice conversation.
              Always very appreciated.
              @backtrader : You're always right. I'm not accurate, however I do my best to improve the ability. Also thanks to you.
              Finally, you're also right: It's a modified piece of the library.

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