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/

    Difference between params and p

    General Discussion
    2
    2
    246
    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.
    • 김진원
      김진원 last edited by

      is there a difference between params and p? can anyone give example how it is working?

      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        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: https://www.backtrader.com/docu/concepts/)

        Longer:

        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 )

        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 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\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 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
        
        1 Reply Last reply Reply Quote 4
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }