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

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:

    One workaround idea would be to use regular python logging module in the strategy and to disable the logger from outside before starting the optimization run.

  • administrators

    Put a parameter is_opt in the strategy and set it to True when invoking optstrategy

  • 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:

    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!

  • administrators

    Subclass Cerebro, add an attribute which you set to True when optstrategy is called (obviously you override that method) and you won't have to depend on an internal value.

  • Ok, thanks alot!

Log in to reply