Difference between params and p
-
is there a difference between params and p? can anyone give example how it is working?
-
Short:
In your class methods you may use
self.p
andself.params
interchangeably - they are actually the same object.
(see the 'Parameters' section in the docs: https://www.backtrader.com/docu/concepts/)Longer:
One need to distinguish between the
params
class variable and theself.params
(orself.p
for that matter) instance variable.params
class variable (list of tuples or dict) defines the custom parameters and their default values that will be later used to "magically" create and populate theself.params
(orself.p
) instance variableThis
self.params
(orself.p
) instance variable will have members for each tuple originally present inparams
class 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 )TLDR:
The
params
class 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.p
andself.params
in 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 thewith_metaclass
method in utils\py3.py for technical details of how this is implemented.For example one of such meta-classes is
MetaParams
class which is particularly responsible for creating theself.p
andself.params
instance variables ( see itsdonew
method )During your strategy (derived from
Strategy
class which usesMetaParams
meta-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 inX
class. The instance of thisX
class will be created, where each member will get its value either from the parameters passed during theCerebro.addstrategy
call or from the default value in theparams
class variable tuples.For example let's assume your strategy
params
variable defined as:class SMACrossOver(bt.Strategy): params = ( ('stake', 1), ('period', 30), )
and you add this strategy to Cerebro using:
cerebro.addstrategy(SMACrossOver, period=15)
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