Difference between params and p
김진원 last edited by
is there a difference between params and p? can anyone give example how it is working?
vladisld last edited by
In your class methods you may use
self.paramsinterchangeably - they are actually the same object.
(see the 'Parameters' section in the docs: https://www.backtrader.com/docu/concepts/)
One need to distinguish between the
paramsclass variable and the
self.pfor that matter) instance variable.
paramsclass variable (list of tuples or dict) defines the custom parameters and their default values that will be later used to "magically" create and populate the
self.p) instance variable
self.p) instance variable will have members for each tuple originally present in
paramsclass variable but their value could be different depending of which parameters where passed during class instantiation ( or have their default values if no matching parameters were passed )
paramsclass variable may be defined in classes that use MetaParams metaclass (or its derivatives) during their instantiation sequences (like Cerebro, Strategy, Analyzer, BrokerBase, Filter, Indicator, an so on and all their inherited classes ).
During the instantiation the meta-class "hijack" the instantiation sequence and creates additional instance variables (
self.paramsin particular, but also many others like 'datas', 'data0' and other aliazes for LineSeries inherited classes for example), intializing them in various ways. You may take a look at the
with_metaclassmethod in utils\py3.py for technical details of how this is implemented.
For example one of such meta-classes is
MetaParamsclass which is particularly responsible for creating the
self.paramsinstance variables ( see its
During your strategy (derived from
Strategyclass which uses
MetaParamsmeta-class) instantiation process a new auto-generated class (let's call it X) will be used to hold the parameter values. For each tuple in the 'params' class variable there will be a member in
Xclass. The instance of this
Xclass will be created, where each member will get its value either from the parameters passed during the
Cerebro.addstrategycall or from the default value in the
paramsclass variable tuples.
For example let's assume your strategy
paramsvariable defined as:
class SMACrossOver(bt.Strategy): params = ( ('stake', 1), ('period', 30), )
and you add this strategy to Cerebro using:
The values for 'self.p' members inside your strategy will be:
self.p.stake == 1 # from default value in `params` class variable self.p.period == 15 # from parameters to cerebro.addstrategy