Mariner Backtesting - service.terminate()

service.terminate()

service. (method) Overview:

Unload the instance of strategy from memory after the current method finishes.

Interface:
 terminate()
Parameters:

None

Sample

 service.terminate()
Returns
  • None
Remarks:

Can be called from anywhere service is available.

Instance doesn't terminate until the current Strategy method returns from being called. In other words, the system doesn't process the terminate request until after returning from the system method.

on_finish() is called before the instance unloads.

If you use service.terminate() on a symbol in a "single multiday backtest job", that symbol WILL come available again in is_symbol_qualified for the next trading day, it is not terminated for the duration of the backtest.

Working Example:

Note: print ("on_start has completed") is still called. terminate() doesn't exit immediately on_trade() is never called on_finish() is called before unloading

from cloudquant.interfaces import Strategy


class TerminateExample(Strategy):
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == "AAL"

    def on_start(self, md, order, service, account):
        print('The simulation has begun at ' + service.time_to_string(service.system_time)[12:21])

        # service.terminate() will end the simulation early
        service.terminate()

        # even though service.terminate() method has been called, the rest of on_start will be completed
        print ("on_start has completed")

        # return leaves the method
        return
        # this will never print
        print ("this will never print")

    def on_finish(self, md, order, service, account):
        print('\n\non_finish has been called')
        print('The simulation will end at ' + service.time_to_string(service.system_time)[12:21])

Console

The simulation has begun at 9:30:00.0
on_start has completed

on_finish has been called
The simulation will end at 9:30:00.1