Dynamic parameters passed to cerebro.optstrategy()
-
Dear All,
I'm familiar with the following syntax:cerebro.optstrategy(MyHolyGrailStrategy, period=range(1, 20), )
But how can I call optstrategy when the parameters to be optimized and their respective ranges are only known at runtime?
For addstrategy(), I'm using something likecerebro.addstrategy(MyHolyGrailStrategy, parameters)
where parameters is a dictionnary that is received by the _init method of my strategy
def __init__(self, params):
But the same approach does not seem to be working here.
Many thanks in advance
Kind regards,
Lamp' -
ok, found what I was looking for. For the benefits of others...
d = {'period': [14, 16, 20 , 22, 25, 30], 'period2': [33, 39]} cerebro.addstrategy(MyHolyGrailStrategy, *[], **d)
cheers
-
Just to be clear, what have you exactly found?
The signature for
addstrategy
andoptstrategy
is clear an documented. It taks*args
and*kwargs
and where possible some of the keyword arguments are incorporated to the declarative built-inparams
(see below). The rest is given verbatim to the strategy.Note: you probably didn't want to use
addstrategy
in your last post, given that you obviously pass a combination of parameters meant for optimization.@lampalork said in Dynamic parameters passed to cerebro.optstrategy():
For addstrategy(), I'm using something like
cerebro.addstrategy(MyHolyGrailStrategy, parameters)
where parameters is a dictionary that is received by the
__init__
method of my strategydef __init__(self, params):
That pattern for example goes against the declarative
params
provided as a built-in by backtrader and brings absolutely no benefit. -
yes, my bad. I meant this:
d = {'period': [14, 16, 20 , 22, 25, 30], 'period2': [33, 39]} cerebro.optstrategy(MyHolyGrailStrategy, *[], **d)
@lampalork said in Dynamic parameters passed to cerebro.optstrategy():
d = {'period': [14, 16, 20 , 22, 25, 30], 'period2': [33, 39]}
cerebro.addstrategy(MyHolyGrailStrategy, *[], **d)