For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

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 and self.params interchangeably - they are actually the same object.
    (see the 'Parameters' section in the docs:


    One need to distinguish between the params class variable and the self.params (or self.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 the self.params (or self.p) instance variable

    This self.params (or self.p) instance variable will have members for each tuple originally present in params 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 )


    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 and self.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 the with_metaclass method in utils\ for technical details of how this is implemented.

    For example one of such meta-classes is MetaParams class which is particularly responsible for creating the self.p and self.params instance variables ( see its donew method )

    During your strategy (derived from Strategy class which uses MetaParams 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 in X class. The instance of this X class will be created, where each member will get its value either from the parameters passed during the Cerebro.addstrategy call or from the default value in the params 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

Log in to reply