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/

    Accessing Strategy Parameters

    General Code/Help
    4
    7
    2135
    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.
    • L
      Laurent Michelizza last edited by

      Hi,

      I would like to access the strategy parameters for a project I am working on.

      However when trying to do so, I get the following:

      ort.Test.params
      Out[25]: backtrader.metabase.AutoInfoClass_LineRoot_LineMultiple_LineSeries_LineIterator_DataAccessor_StrategyBase_Strategy_Test1
      

      So as a workaround, I put my parameters in a dictionary "parameters" and then passed these parameters as a tuple to params, see below:

         parameters = {'param1': 1}
         params = tuple(parameters.items())
      

      This seems to be working but I am curious to know if there is a better/more elegant way of doing this?

      Thanks!

      B 2 Replies Last reply Reply Quote 1
      • B
        backtrader administrators @Laurent Michelizza last edited by backtrader

        @laurent-michelizza said in Accessing Strategy Parameters:

         parameters = {'param1': 1}
         params = tuple(parameters.items())
        

        This seems to go in the opposite direction of accessing the params

        The params class/instance has the following methods (see metabase.py)

            def isdefault(self, pname):
        
            def notdefault(self, pname):
        
            def _get(self, name, default=None):
        
            @classmethod
            def _getkwargsdefault(cls):
        
            @classmethod
            def _getkeys(cls):
        
            @classmethod
            def _getdefaults(cls):
        
            @classmethod
            def _getitems(cls):
        
            @classmethod
            def _gettuple(cls):
        
            def _getkwargs(self, skip_=False):
        
            def _getvalues(self):
        
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators @Laurent Michelizza last edited by

          @laurent-michelizza said in Accessing Strategy Parameters:

             parameters = {'param1': 1}
             params = tuple(parameters.items())
          

          Furthermore, you may find several samples that show that parameters can also be defined as a dictionary. There is no need for that conversion. A tuple ensures ordering (which can also be achieved with a collections.OrderedDict for example) should it it be needed.

          1 Reply Last reply Reply Quote 1
          • L
            Laurent Michelizza last edited by

            That works thanks!

            1 Reply Last reply Reply Quote 0
            • K
              kojinakata last edited by kojinakata

              @Laurent-Michelizza Did you provide the params like below?

              class TestStrategy(bt.Strategy):
                  params = (
                      ('maperiod', 15),
                      ('printlog', False),
                  )
              

              I have been trying to access the best performing strategy's parameters but wasn't able to:

              cerebro.optstrategy(TestStrategy, maperiod=range(10, 32))
              
              results = cerebro.run(maxcpus=4)
              print("Number of strategies tested: {}".format(len(results)))
              
              strat_return_df = pd.DataFrame()
              for i, strat_result in enumerate(results):
                  print("strat_parameters - {}: {}".format(i, strat_result.params()))
              

              But I get the error:

              AttributeError: 'list' object has no attribute 'params'
              

              Any help is appreciated!

              1 Reply Last reply Reply Quote 0
              • G
                GozatSwing last edited by

                Try asking results, you'll see it's a list, what you want is results[0] I think.

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

                  What is returned and in which format is documented: https://www.backtrader.com/docu/cerebro/

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