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/

    Passing params to my strategy

    General Code/Help
    2
    2
    81
    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.
    • R
      Rapha last edited by

      Hi there,

      I am in the process of segmenting and packaging different part of my code, and the backtrader part containing my strategy (class ML_strategy(bt.Strategy)) is now isolated from the rest of in a package (backtest).

      For this reason, I need to pass the params to my strategy without defining them in the class as I used to do:

      class ML_strategy(bt.Strategy):
          params = (('target_n_positions', 20),
                    ('min_n_positions', 5),
                    ('min_position_value', 2000),
                    ('rebalancing_frequency', 'daily'),
                    ('rebalancing_direction', 'long'),
                    ('verbose', False))
      

      I have tried two methods, which both give me some errors:

      1. Setting the class variables this way from my main.py:
                  backtest.ML_strategy.params = (('target_n_positions', 20),
                                                 ('min_n_positions', 5),
                                                 ('min_position_value', 2000),
                                                 ('rebalancing_frequency','daily'),
                                                 ('rebalancing_direction','long'),
                                                 ('verbose', False))
      
                  cerebro.addstrategy(backtest.ML_strategy)
      

      which gives me the following error:

      File "C:\Users\rapha\Documents\GitHub\algorithms\venv\lib\site-packages\backtrader\metabase.py", line 283, in donew
        params = cls.params()
      TypeError: 'tuple' object is not callable
      
      1. Providing the instance variables in addstrategy:
      cerebro.addstrategy(backtest.ML_strategy,target_n_positions=20,min_n_positions=5,min_position_value=2000,rebalancing_frequency='daily',rebalancing_direction='long',verbose=False)
      

      which gives me the following error:

      File "C:\Users\rapha\Documents\GitHub\algorithms\venv\lib\site-packages\backtrader\metabase.py", line 283, in donew
        params = cls.params()
      TypeError: 'tuple' object is not callable
      

      Additional information: I have switch to Python 3.10. I used to work on python 3.6 for backtrader, not sure if that might be the causing the issue as well.

      Any idea on what I might be doing wrong here?

      Thanks in advance
      Rapha

      B 1 Reply Last reply Reply Quote 0
      • B
        BorutF @Rapha last edited by

        @rapha It is possible that it is about the Python version change.

        params = dict(
              target_n_positions=20
        )
        

        You could try by defining the params inside in a dict, as shown above.

        When I run both the tuple and dict approaches work, but who knows. I run backtrader in a environment in 3.8, as I have found some troubles with the newer versions.

        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors