Mariner Backtesting - order.send() MOO, MOC (Simple)

Market On Open Close (Simple)

Market On Open (MOO) and Market On Close (MOC) Orders (Simple Order Algo)

ELITE ONLY

Overview:

Function to submit MOO and MOC orders.

Market On Open - matches the exchange for the symbol - if no MOO is available a 2-minute VWAP, aggression=2, slices=4 starting on market open is entered.

Market On Close - matches the exchange for the symbol - if no MOC is available a 2-minute VWAP, aggression=2, slices=4 starting 2 minutes before the market close is entered.

There are a number of Exchange rules surrounding Market On Open (MOO) and Market on Close (MOC) orders. These generally relate to the timing of the order and the direction of the order.

For simplest usage place your MOO orders before 9:20am (md.market_open_time - service.time_interval(minutes=11)) and your MOC orders more than 15 minutes before the close (md.market_close_time - service.time_interval(minutes=16).

If you try to enter beyond those times, you must abide by each individual exchange's rules regarding imbalances.

Remember to extend your backtests beyond the normal core hours to allow your orders to be sent/filled. Use custom hours. Starting at 09:15 and ending at 16:15 should get the majority of your fills but keep an eye on outstanding positions, some symbols can fill very late after the close.

Examples
 order.send(self.symbol, 'buy', 100, type='MOO') 
order.send(self.symbol, 'sell', 100, type='MOC') 

Not for use with live trading

This order method is for use with backtesting only. It isn't supported in live trading.

Parameters
Argument Required Description Example(s)
symbol Yes The symbol for the order. QQQ
side Yes The "buy" or "sell" indicator. buy, sell
qty Yes The order quantity. 100
type Yes The order type. MOO,MOC
start_time No The time that the order is to be sent. Defaults to now. Only available for Elite. md.market_open_time - service.time_interval(minute=15)
Working Example:(Link)
# Copyright Cloudquant, LLC. All right reserved.
# MOO CQ ELITE ONLY   = Market On Open  - matches the exchange for the symbol - if no MOO is available a 2-minute VWAP, aggression=2, slices=4 starting on market open is entered.
# MOC CQ ELITE ONLY   = Market On Close - matches the exchange for the symbol - if no MOC is available a 2-minute VWAP, aggression=2, slices=4 starting 2 minutes before the market close is entered.
# Run 09:15 to 16:15 to allow time for the MOO to be submitted and the MOC to close.
# you can use the + button on custom hours and set two time periods if your model is only expected to trade in exclusive time periods.
# for this model you could run 09:15 to 09:45 and 15:45 to 16:15
# Run on 1/18/2018 with a small set of random symbols from different exchanges. 
# Exit MOC orders are submitted at 15:45

from cloudquant.interfaces import Strategy
class New_Order_Method_MOO_MOC(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol in ['FB','AAPL','AMZN','NFLX','GOOG','MSFT','IBM','EEM','HMNY','SRCI']

    def on_start(self, md, order, service, account):
        # MOO CQ ELITE ONLY   = Market On Open  - matches the exchange for the symbol - if no MOO is available a 2-minute VWAP, aggression=2, slices=4 starting on market open is entered.
        order.send(self.symbol, 'buy', 100, type='MOO') 
        print("Pre-Open - Buy    MOO {} Exchange {}".format(self.symbol.ljust(6),md.stat.exchange))
        self.status = 1

    def on_minute_bar(self, event, md, order, service, account, bar):
        if event.timestamp >= service.time(15,45) and (self.status==1 or self.status==2): # we check for both status' in case it is running in CQ LITE and so does not call on_fill
            time_string =service.time_to_string(event.timestamp, format='%H:%M:%S') 
            # MOC CQ ELITE ONLY   = Market On Close - matches the exchange for the symbol - if no MOC is available a 2-minute VWAP, aggression=2, slices=4 starting 2 minutes before the market close is entered.
            order.send(self.symbol, 'sell', 100, type='MOC') 
            print("{} - Sell   MOC {} Exchange {}".format(time_string ,self.symbol.ljust(6),md.stat.exchange))
            self.status = 3


# Note : on_fill will only be called for CQ Elite users 
    def on_fill(self, event, md, order, service, account): 
        time_string =service.time_to_string(event.timestamp, format='%H:%M:%S') 
        if self.status == 1:
            print("{} - Filled MOO {} Price{:8.2f}".format(time_string,self.symbol.ljust(6),event.price))
            self.status = 2
        if self.status == 3:
            print("{} - Filled MOC {} Price{:8.2f}".format(time_string,self.symbol.ljust(6),event.price))
            self.status = 4

Console

Pre-Open - Buy    MOO AAPL   Exchange Q
Pre-Open - Buy    MOO AMZN   Exchange Q
Pre-Open - Buy    MOO EEM    Exchange P
Pre-Open - Buy    MOO FB     Exchange Q
Pre-Open - Buy    MOO GOOG   Exchange Q
Pre-Open - Buy    MOO HMNY   Exchange Q
Pre-Open - Buy    MOO IBM    Exchange N
Pre-Open - Buy    MOO MSFT   Exchange Q
Pre-Open - Buy    MOO NFLX   Exchange Q
Pre-Open - Buy    MOO SRCI   Exchange A
09:30:00 - Filled MOO SRCI   Price    9.06
09:30:00 - Filled MOO EEM    Price   49.95
09:30:00 - Filled MOO NFLX   Price  220.35
09:30:00 - Filled MOO MSFT   Price   89.85
09:30:00 - Filled MOO AMZN   Price 1293.95
09:30:00 - Filled MOO FB     Price  177.72
09:30:00 - Filled MOO GOOG   Price 1131.41
09:30:00 - Filled MOO AAPL   Price  179.47
09:30:00 - Filled MOO HMNY   Price    7.85
09:30:00 - Filled MOO IBM    Price  170.07
15:45:00 - Sell   MOC NFLX   Exchange Q
15:45:00 - Sell   MOC AAPL   Exchange Q
15:45:00 - Sell   MOC GOOG   Exchange Q
15:45:00 - Sell   MOC FB     Exchange Q
15:45:00 - Sell   MOC IBM    Exchange N
15:45:00 - Sell   MOC EEM    Exchange P
15:45:00 - Sell   MOC MSFT   Exchange Q
15:45:02 - Sell   MOC AMZN   Exchange Q
15:45:05 - Sell   MOC HMNY   Exchange Q
15:45:05 - Sell   MOC SRCI   Exchange A
16:00:00 - Filled MOC SRCI   Price    9.11
16:00:00 - Filled MOC EEM    Price   50.03
16:00:00 - Filled MOC HMNY   Price    8.14
16:00:00 - Filled MOC FB     Price  179.80
16:00:00 - Filled MOC AAPL   Price  179.26
16:00:00 - Filled MOC MSFT   Price   90.10
16:00:00 - Filled MOC GOOG   Price 1129.79
16:00:00 - Filled MOC NFLX   Price  220.33
16:00:00 - Filled MOC AMZN   Price 1293.32
16:01:41 - Filled MOC IBM    Price  169.12