Passing params to my strategy
-
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:
- 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
- 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 -
@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.