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

Stopping or pausing a backtest



  • Is it possible to stop or pause a backtest run, whether opt_runs or otherwise early and retain the data?

    My indicators can be a little slow (machine learning) and so backtesting even just 5,000 lines (with no optimization) takes about 5 hours. Sometimes it would be nice to stop this run early (or better yet - pause it so it can be restarted!) and then save the results and take a look at it.

    Anybody know if this is possible?


  • administrators

    Nothing prevents you from doing whatever you want inside the next method, like giving control to something else.



  • @backtrader said in Stopping or pausing a backtest:

    Nothing prevents you from doing whatever you want inside the next method, like giving control to something else.

    OK, so I'm in the middle of a very long opt_runs which has been going for a few days now. I would like to stop it so I can do some tweaks and move it to a cloud server.

    How can I pause it or stop it but still have access to what the best parameters (so far) are?


  • administrators

    If you have no provisions in place already in your code, Python is not prepared out of the box to stop execution. (There may be some magic hack in the net, but I don't know if it does exist and if it does, how well it works)



  • One useful thing could be to dump some pickles at some interval.

    Something like:

    
    def notify_trade(self, trade):
            date = self.data.datetime.datetime()
            if trade.isclosed:
    
                sr_file = open('pickled_opt_runs_{}'.format(sharperatio_analyzer_object), 'wb')
                sqn_file = open('pickled_opt_runs_{}'.format(sqn_analyzer_object), 'wb')
    
                pickle.dump(opt_runs, file)
                pickle.dump(opt_runs, file)
    
                sr_file()
                sqn_file()