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

self.env.runstop() when optstrategy results in strange behaviour



  • Hi all,

    I run cerebro using cerebro.optstrategy(strategy, rsiparam=[20, 40, 60]). If I use a standard strategy without using self.env.runstop(), Cerebro runs 3 times as expected, one for each param. However, if I use self.env.runstop() in the strategy next() method once a certain condition is triggered, the following happens:

    • The current run with param (rsiparam=20) is stopped (expected behaviour)
    • The rsiparam=40 run is started, however is immediately stopped, start() and stop() methods are called, next() is not. (unexpected behaviour).
    • Rsiparam=60 same as rsiparam=40 run.
      Therefore, it seems that if I run self.env.runstop(), all the other optstrategy runs after are also stopped. How to fix this?

    Thanks!

    Kind regards,



  • runstop() method just sets a special flag (_event_stop) in Cerebro engine (not a strategy - that's important) - nothing more. This flag is checked at multiple check points inside the Cerebro machinery during the live cycle of the strategy and if set - the engine stops the execution of its current strategy and if there is nothing else to do, just returns from its run method.

    Now for the scenario in question. When optstrategy is used - Cerebro runs multiple instances of the same strategy for each parameters permutation. However it may run all of those strategy instances using the same Cerebro engine instance or multiple Cerebro engine instances (in separate processes).

    The first case occurs only if you have a single CPU (or set the maxcpus parameter to 1). So , as mentioned above, the same Cerebro instance is used to run all those strategy instances. If some strategy calls the runstop() method, the Cerebro engine will eventually stop the execution of the strategy at the closest check point and will try to execute the next strategy. However the flag is still set - so the next strategy will be stopped at the first suitable check point as well - the same with the following strategies.

    The second case is much more complicated ( from the Cerebro engine perspective - not from user perspective) since now multiple Cerebro engines are instantiated (actually cloned or pickle-ized) in separate processes in order to execute the strategies in parallel. Here if runstop method is called within a single strategy - it will impact only the Cerebro engine in the same process - others should continue to execute.

    It seems that the behavior you have described matches the first case.



  • @monstrar said in self.env.runstop() when optstrategy results in strange behaviour:

    How to fix this?

    I'm not sure I have a good advice here - probably others will help.

    However if your were able to detect the 'certain condition' during the strategy's __init__ method - the better option is to raise the StrategySkipError instead of calling the runstop method.



  • @vladisld

    Thank you for the detailed comment. You are correct, I am running cerebro.run(maxcpu=1) so the first case you described happened for me. I will try rasing an exception instead as you recommended, thanks!


Log in to reply
 

});