Detecting if we are running an Optimization from within a Strategy
-
I am looking for a way inside a strategy to detect if we a running a regular backtest or if we are running an optimization. Is this possible?
The background is that I would like to do logging only in regular backtest runs. During (multiprocessor) optimization runs there should be no logging (it would not be properly readable anyway, right)?
So my function
log
would start with something like this:if is_opt: return
One workaround idea would be to use regular python
logging
module in the strategy and to disable thelogger
from outside before starting the optimization run. -
Put a parameter
is_opt
in the strategy and set it toTrue
when invokingoptstrategy
-
Ok, thanks, thats another solution. But I thought it would be nice to make that parameter not part of the regular strategy parameter set. Mainly because I would prefer if that parameter would not appear as normal parameter in my result evaluation scriptings.
I went for using this:
if self.cerebro._dooptimize: return
I know
_dooptimize
is not part of the official API and might break anytime but I think it is ok for now ofr my use case. Thanks! -
Subclass
Cerebro
, add an attribute which you set toTrue
whenoptstrategy
is called (obviously you override that method) and you won't have to depend on an internal value. -
Ok, thanks alot!