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/

    Attribute Error: 'int' object has no attribute 'set'

    General Code/Help
    2
    2
    544
    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
      Carson Lansdowne last edited by

      Hello,

      I am trying to modify the sizer within my strategy and I am using the code exactly as stated in the docs from backtrader, but I get the error:

      File "anaconda3\lib\site-packages\backtrader\strategy.py", line 1449, in setsizer
          sizer.set(self, self.broker)
      
      AttributeError: 'int' object has no attribute 'set'
      

      I've tried to trouble shoot, but the documentation is not really much help as to what this error could be. How do I change my sizer based on a variable passed to the strategy class?

      Here is my code

      import backtrader as bt
      import datetime
      import math
      
      class buynhold_1(bt.Strategy):
          params = (
              ('start_date', None),
              ('end_date', None),
              ('sizer', None),
              )
          
          def __init__(self):
              self.dataclose = self.datas[0].close()
              if self.p.sizer is not None:
                  self.sizer = self.p.sizer
      

      and here is how I am calling my code

      cerebro.adddata(data)
      
      # Set our desired cash start
      cerebro.broker.setcash(1000.0)
      
      # Add strategy to Cerebro
      cerebro.addstrategy(buynhold_1,
                              start_date = datetime.datetime(2016,1,17),
                              end_date = datetime.datetime(2017,1,20),
                              sizer = 2
                              )
      
      #add the sizer
      #cerebro.addsizer(bt.sizers.SizerFix, stake=2)
      
      if __name__ == '__main__':
          # Run Cerebro Engine
          start_portfolio_value = cerebro.broker.getvalue()
      
          cerebro.run()
      
      1 Reply Last reply Reply Quote 0
      • Soham Jain
        Soham Jain last edited by

        Hey Carson,
        I guess you are confused between sizer and size.
        sizer is a python class that comes with the backtrader module.
        size is the quantity that you want to buy during a specific self.buy() call.

        In order to derive the size to be bought logically, you must create a sub class that inherits from bt.Sizer base class.

        import backtrader as bt
        
        class FixedSize(bt.Sizer):
            params = (('stake', 1),)
        
            def _getsizing(self, comminfo, cash, data, isbuy):
                return self.params.stake
        

        something like this...

        More on bt.Sizers available here.

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