Mariner Backtesting - service.stop_strategy()

service.stop_strategy()

service. (method)

stop_strategy() is used stop a strategy from within a script. The stopped strategy must have been started via service.start_strategy().

Interface:
 stop_strategy(instruction_id)

Sample - Calling Method

 instruction_id = service.start_strategy('839c5f3b-a3c8-4ac8-88fa-952515e9502c')
service.stop_strategy(instruction_id)
Parameters:
Name Type Default Information
instruction_id string required The instruction_id of the strategy that should be stopped. This was returned in the call to service.start_strategy()
Returns:
Type Notes
   
Remarks:
  • Multiple scripts can be started from one 'parent' script
  • start_strategy() can be used to start an exit script.
  • The parent script(the script containing start_strategy()) must have the child scripts listed as dependencies.
  • How to set dependencies
  • How to find the script guid
Working Example:

This parent script determines whether to start a long or short child script.

from cloudquant.interfaces import Strategy


class StrategyHub(Strategy):
    # note that this doesn't start with "self" because it's a @staticmethod
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'AAPL'

    def on_start(self, md, order, service, account):
        self.instruction_id1 = None
        self.instruction_id2 = None

    # called every minute before the first on_trade of every new minute, or 5 seconds after a new minute starts
    def on_minute_bar(self, event, md, order, service, account, bar):
        print 'In on_minute_bar\t' + service.time_to_string(service.system_time), event, '\n'

        if self.instruction_id1 is not None:
            # Stop the strategy using the instruction_id from start_strategy
            print 'Stopping the short strategy.'
            service.stop_strategy(self.instruction_id1)

            # once the child strategy has been stopped, terminate
            service.terminate()
            print 'Terminating...\n'
            return

        if self.instruction_id2 is not None:
            # Stop the strategy using the instruction_id from start_strategy
            print 'Stopping the long strategy.'
            service.stop_strategy(self.instruction_id2)

            # once the child strategy has been stopped, terminate
            service.terminate()
            print 'Terminating...\n'
            return

        # compare the minute low to the previous close
        if event.low < md.stat.prev_close:

            # if the minute low is below the previous close, start the short script
            print 'The minute low for ' + self.symbol + ' is below the previous close.'
            print 'Starting the short script.\n'

            # Start the short script using that scripts'GUID
            self.instruction_id1 = service.start_strategy('3d9d1846-0e2b-45cf-9eef-9f6581b53ba9', symbol=self.symbol)
            print 'The instruction id to stop the strategy is ' + self.instruction_id1 + '\n'
        else:
            # if the minute low is above the previous close, start the long script
            print 'The minute low for ' + self.symbol + ' is above the previous close.'
            print 'Starting the long script.\n'

            # Start the long script using that scripts'GUID
            self.instruction_id2 = service.start_strategy('289e7377-38b9-45d6-b275-c17f0f08dc2f', symbol=self.symbol)
            print 'The instruction id to stop the strategy is ' + self.instruction_id2 + '\n'

Long Script

 from cloudquant.interfaces import Strategy

class LongAAPL(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'AAPL'

    def on_start(self, md, order, service, account):
        print 'In on_start for LongAAPL\n'
        # place a buy order
        order.algo_buy(self.symbol, 'limit', 'init', order_quantity=100, price=100.01)
        print 'Order for 100 shares of ' + self.symbol + ' placed.'

    def on_finish(self, md, order, service, account):
            print "LongAAPL finished"

Short Script

 from cloudquant.interfaces import Strategy

class ShortAAPL(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'AAPL'

    def on_start(self, md, order, service, account):
        print 'In on_start for ShortAAPL\n'
        # place a sell order
        order.algo_sell(self.symbol, 'limit', 'init', order_quantity=100, price=600.13)
        print 'Sell Order for 100 shares of ' + self.symbol + ' placed.'

    def on_finish(self, md, order, service, account):
            print "ShortAAPL finished"

Console

In on_minute_bar    2016-02-11 09:30:00.007000 MinuteBarEvent(timestamp=1455201000007000, symbol='AAPL', length=60, open=93.5999984741211, high=93.80999755859375, low=93.58999633789062, close=93.69999694824219, volume=21442, vwap=93.69684600830078, spread=0.10080300271511078, bidvol=6494, askvol=12853, count=124)

The minute low for AAPL is below the previous close.
Starting the short script.

The instruction id to stop the strategy is db6da89b-00c6-4706-b2a9-5de67509300f

In on_start for ShortAAPL

Sell Order for 100 shares of AAPL placed.
In on_minute_bar    2016-02-11 09:31:00.177000 MinuteBarEvent(timestamp=1455201060177000, symbol='AAPL', length=60, open=93.75, high=93.87999725341797, low=93.55999755859375, close=93.87999725341797, volume=775419, vwap=93.76644897460938, spread=0.049607545137405396, bidvol=52048, askvol=643756, count=1138)

Stopping the short strategy.
Terminating...

ShortAAPL finished