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

params values



  • Hi,

    I am trying to access broker params values from within the strategy. Depending on they way I try to get the coc value, sometimes I get the actual value and other the default value. Can someone please explain the behaviour?

    Many thanks

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import backtrader as bt
    import pandas as pd
    import datetime
    
    # Create a Strategy
    class TestStrategy(bt.Strategy):
    
        def __init__(self):
            params_dict = self.broker.params._getpairs()
            print(type(self.broker.params))
            print("COC: {}, {}, {}".format(self.broker.params.coc, params_dict['coc'], self.broker.params._get('coc')))
    
    
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
    
        # Add a strategy with parameters
        cerebro.addstrategy(TestStrategy)
    
        df1 = pd.DataFrame({'Close': list(range(1,11))}, index=pd.date_range(start='1/1/2018', periods=10))
        cerebro.adddata(bt.feeds.PandasData(dataname=df1, nocase=True))
    
        # Set our desired cash start
        cerebro.broker.setcash(100000.0)
        cerebro.broker.set_coc(True)
    
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Run over everything
        cerebro.run(runonce=False, preload=False)
    
        # Print out the final result
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
    
    Starting Portfolio Value: 100000.00
    <class 'backtrader.metabase.AutoInfoClass_BrokerBase_BackBroker'>
    COC: True, False, True
    Final Portfolio Value: 100000.00
    

  • administrators

    @momentum said in params values:

    Depending on they way I try to get the coc value, sometimes I get the actual value and other the default value.

    That's not accurate. You always get the same value using the same method. There is not "sometimes".

    @momentum said in params values:

    self.broker.params.coc, params_dict['coc'], self.broker.params._get('coc'))
    

    Only the 1st one is the right one. The other are undocumented methods, to get the actual declared, active values.

    There are additional methods with proper names (which were not expected to be used and are also only in the source code) which will specifically give you the defaults or not.