Early stopping of backtesting
I'd like to use the multiprocessing feature of cerebro with optstrategy(). However, I'd like to stop backtesting, if a strategy performs poorly. So for example, if the initial cash is 100.000, then I want to stop if the total portfolio value drops below 50.000. Or maybe check if the performance after every N bars is above 90% etc.
Is there a feature like this already? I did some research but have not found anything. (Actually this is independent of using addstrategy or optstrategy)
Thanks and best regards,
I just want to add some (maybe trivial) clarifications:
MyStrategy(bt.Strategy)is not instantiated until actual run, neither
Meanwhile calling for
self.envof actual strategy instance (while running backtesting) is ok for defining early stop method:
class MyStrategy(bt.Strategy): ... def early_stop(self): self.env.runstop() print('Early stop envoked!') def next(self): ... if <early stop condition>: self.early_stop() ... <some other next() computations>
Note: even if
early_stop()is called somewhere inside
next()body, it will not terminate imidiately, i.e.
<some other next() computations>above still be executed once.
You could always do:
if <early stop condition>: self.early_stop() return
No further calculations will run after
The original poster asked how to stop during the calculations and not during instantiation (
__init__). To do so see: