Backtrader Community

    • 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/

    Feature Request: Allow passing of data object to Sizer

    General Discussion
    2
    6
    2174
    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.
    • RandyT
      RandyT last edited by

      I find that I am searching for a way to specify the data used in the sizer depending on order of data feeds created or whether I am in live or static mode.

      Would it be possible to add a dataname parameter to the sizer class that would allow me to pass the proper data source?

      1 Reply Last reply Reply Quote 0
      • RandyT
        RandyT last edited by

        Actually just found the magic for passing this as a parameter:

        cerebro.addsizer(lsizers.VaRContracts, percent=1, leverage=args.leverage,
                                       scaled=args.scaled, dataname=cerebro.datas[0])
        
        class VaRContracts(bt.Sizer):
            params = (('percent', 1),
                      ('leverage', 40000),
                      ('scaled', None),
                      ('reverse', False),
                      ('dataname', None))
        
            def __init__(self):
                if self.p.dataname:
                    self.dataobj = self.p.dataname
        
            def _getsizing(self, comminfo, cash, data, isbuy):
                self.pchg = self.calc_pchg(self.dataobj.close.get(size=51))
        
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators last edited by backtrader

          The solution above is for sure one that works. There are alternatives.

          For example, Sizer` instances have a specific attribute which was thought for such thing.

          • strategy (See the docs: Sizers - Smart Stakng )

          Strategy and Cerebro instances share the same array of data feeds. With that in mind the code above could look different.

          class VaRContracts(bt.Sizer):
              params = (('percent', 1),
                        ('leverage', 40000),
                        ('scaled', None),
                        ('reverse', False),
                        ('didx', 0),
              )
          
              def __init__(self):
                  self._data = self.strategy.datas[self.p.didx]
          
              def _getsizing(self, comminfo, cash, data, isbuy):
                  self.pchg = self.calc_pchg(self._data.close.get(size=51))
          

          This uses an numeric index to address the array.

          Using names could make even more sense. This has the advantage that you don't need to have created the data feed before adding the sizer.

          cerebro.addsizer(lsizers.VaRContracts, percent=1, leverage=args.leverage,
                                         scaled=args.scaled, dataname='SPY')
          
          cerebro.adddata(data0, name='SPY')
          
          class VaRContracts(bt.Sizer):
              params = (('percent', 1),
                        ('leverage', 40000),
                        ('scaled', None),
                        ('reverse', False),
                        ('dataname', None),
              )
          
              def __init__(self):
                  self._data = self.strategy.getdatabyname(self.p.dataname)
          
              def _getsizing(self, comminfo, cash, data, isbuy):
                  self.pchg = self.calc_pchg(self._data.close.get(size=51))
          
          1 Reply Last reply Reply Quote 2
          • RandyT
            RandyT last edited by

            Very nice. Thank you for that guidance.

            1 Reply Last reply Reply Quote 0
            • RandyT
              RandyT last edited by

              @backtrader appears that self.strategy.getdatabyname() is not available in __init__()

              I don't see it until we hit _getsizing()

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

                Should be (meaning it will be there as soon as possible, because it should be there in __init__ and not after it)

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