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



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


  • administrators

    @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):
    

  • administrators

    @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.



  • That works thanks!



  • @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!



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


  • administrators

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


Log in to reply
 

});