Mariner Backtesting - service.start_strategy()

service.start_strategy()

start_strategy() is used start a strategy from within a script.

Interface:
 start_strategy(script_id,
               symbol,
               options,
               parameters)

Sample - Calling Method

 service.start_strategy('839c5f3b-a3c8-4ac8-88fa-952515e9502c')
Parameters:
Name Type Default Information
script_id string required The GUID or filename of the strategy.
symbol string required The symbol that the strategy will monitor. Accessible via self.strategy when the strategy executes.
options dictionary required Control options for starting and stopping strategies. dict[str]->object
parameters dictionary required A dictionary of parameters given to the __init__ method when the strategy is created.
Returns:
Type Notes
string Returns an instruction_id that can be used to stop the strategy
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'

    # 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'

        # 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 self.symbol + ' is dipped below the previous close during the first minute.'
            print 'Starting the short script.\n'

            # Start the short script using that scripts'GUID
            service.start_strategy('d8edc6ba-803c-4689-a876-204f7b1b373b', symbol=self.symbol)
        else:
            # if the minute low is above the previous close, start the long script
            print self.symbol + ' did not dip below the previous close during the first minute.'
            print 'Starting the long script.\n'

            # Start the long script using that scripts'GUID
            service.start_strategy('059e8348-bef8-4b32-b195-147945439410', symbol=self.symbol)

        service.terminate()

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 ShortAAPL\n'
        # place a buy order
        order.algo_buy(self.symbol, 'market', 'init', order_quantity=100)
        print 'Order for 100 shares of ' + self.symbol + ' placed.'
        # terminate the script
        service.terminate()

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, 'market', 'init', order_quantity=100)
        print 'Sell Order for 100 shares of ' + self.symbol + ' placed.'
        # terminate the script
        service.terminate()

Console

In on_minute_bar    2016-03-01 09:30:00.124000 MinuteBarEvent(timestamp=1456842600124000, symbol='AAPL', length=60, open=97.6500015258789, high=97.75, low=97.6500015258789, close=97.73999786376953, volume=6104, vwap=97.7056884765625, spread=0.06190381571650505, bidvol=1014, askvol=4939, count=21)

AAPL did not dip below the previous close during the first minute.
Starting the long script.

In on_start for ShortAAPL

Order for 100 shares of AAPL placed.