Mariner Backtesting - service.add_event_trigger()

service.add_event_trigger()

Add a event trigger to the list of triggers. Should clear other event triggers first. It only works in on_start().

IMPORTANT NOTE, YOUR TOP LINE CLOUDQUANT IMPORT MUST ALSO INCLUDE EVENT...

from cloudquant.interfaces import Strategy, Event

Interface:
 add_event_trigger(symbols,
                  types)

Sample - Calling Method

 service.add_event_trigger( [self.symbol], [Event.TRADE, Event.FILL])
Parameters:
Name Type Default Information
symbols symbols from symbol universe required A list of strings representing symbol names. You can subscribe to more than one symbols events (e.g. for trading pairs)
types list required A list of integers (from Event enum) representing the kind of event.
  • Event.ACK
  • Event.ARCA_IMBALANCE
  • Event.CANCEL
  • Event.CANCEL_REJECT
  • Event.FILL
  • Event.NASDAQ_IMBALANCE
  • Event.MINUTE_BAR
  • Event.NBBO_PRICE
  • Event.NEWS
  • Event.NYSE_IMBALANCE
  • Event.AMER_IMBALANCE
  • Event.REJECT
  • Event.TRADE
  • Event.TRADING_HALT
  • Event.TRADING_RESUME
Returns:

None

Remarks:
  • Only works in on_start()
  • In backtesting, code loads automatically
  • In forward/production trading, the system will call all system methods by default
  • In the case of pairs and baskets, all system events can be instructed to go to the same instance (very advanced topic).
Working Example:

Add Event Trigger. Note: clearing other event triggers first.

from cloudquant.interfaces import Strategy, Event


class AddEventTriggerExample(Strategy):

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

    def on_start(self, md, order, service, account):
        # print start time and symbol
        print(self.symbol + "\n" + service.time_to_string(service.system_time) + "\tin on_start()\n\n")

        self.only_once = True

        #set timer for 3:55 PM EST (5 minutes before close)
        service.add_time_trigger(service.time(15, 55))

        #this will clear the event triggers, but will not interfere with the above time_trigger
        #however this clear_event_trigger does stop you from calling on_trade
        service.clear_event_triggers()

        #adds on_trade to the list of triggers allowing it to be called
        service.add_event_trigger([self.symbol], [Event.TRADE])

    def on_trade(self, event, md, order, service, account):
        #on_trade is only able to be called because of the add_event_trigger

        if self.only_once:
            print(service.time_to_string(md.L1.timestamp) + "\tin on_trade()\n\n")

            #  change instance variable to False
            self.only_once = False

    def on_timer(self, event, md, order, service, account):

        #even though event triggers were cleared, on_timer is still called
        print(service.time_to_string(event.timestamp) + "\tin on_timer()\n\n")

    def on_finish(self, md, order, service, account):
        # even though event triggers were cleared, on_finish is still called
        print(service.time_to_string(md.L1.timestamp) + "\tin on_finish()\n\n")

Console

AAL
2016-08-08 09:29:55.102000    in on_start()

2016-08-08 09:30:00.312000    in on_trade()


2016-08-08 15:55:00.000000    in on_timer()


2016-08-08 16:00:00.000000    in on_finish()