Mariner Backtesting - Market On Open

Examples using GUID for MOO orders using order.algo_buy() and order.algo_sell()

See also: MOO GUIDs

Market on Open (MOO) order type

A Market-on-Open order is a special order type that instructs the market to execute the order at the day's opening price. MOO orders cannot be executed at any other time during the trading day.

NASDAQ

MOO orders can be entered, canceled or cancel replaced from 7 a.m. to 9:28 a.m. EST on days that the markets are open. Execution is guaranteed if and only if there is sufficient size (liquidity) to fill the order.

NYSE

MOO orders can be entered starting at 7:30 EST on days that the markets are open.

Working Example:
# Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy

class MarketOnOpen(Strategy):
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol in ["SPY", "AAPL", "GDX", "NUGT", "XLF", "JNUG"]

    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")
        # algorithm: Buy Market ARCA MOO
        # Buys at Arca 
        # send order; use a variable to accept the order_id that order.algo_buy returns

        order_id = order.algo_buy(self.symbol, "48c76891-2d0b-424e-91de-480d15718761", intent="init",
                                  order_quantity=100)

        # print the symbol and the order_id
        print(self.symbol + "\torder number: " + order_id + "\n")

        # add a timer to sell the shares 1 minute later
        service.add_time_trigger(service.time(9,31))

    def on_fill(self, event, md, order, service, account):
        if event.shares > 0:
            print('BUY\t' + service.time_to_string(event.timestamp) + '\t' + self.symbol + "\tin on_fill()")
        elif event.shares < 0:
            print('SELL\t' + service.time_to_string(event.timestamp) + '\t' + self.symbol +  "\tin on_fill()")

        # print statement when an order fills
        print(event, '\n')

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

        # algorithm: Sell Market ARCA
        order.algo_sell(self.symbol, "8fdee8fe-b772-46bd-b411-5544f7a0d917", intent='exit')

Console

AAPL
2017-02-01 09:25:00.000000  in on_start()

you placed an order for 100 shares of AAPL  order number: 0000000000000001

GDX
2017-02-01 09:25:00.000000  in on_start()

you placed an order for 100 shares of GDX   order number: 0000000000000002

JNUG
2017-02-01 09:25:00.000000  in on_start()

you placed an order for 100 shares of JNUG  order number: 0000000000000003

NUGT
2017-02-01 09:25:00.000000  in on_start()

you placed an order for 100 shares of NUGT  order number: 0000000000000004

SPY
2017-02-01 09:25:00.000000  in on_start()

you placed an order for 100 shares of SPY   order number: 0000000000000005

XLF
2017-02-01 09:25:00.000000  in on_start()

you placed an order for 100 shares of XLF   order number: 0000000000000006

BUY 2017-02-01 09:30:00.055000  AAPL    in on_fill()
(FillEvent(instruction_id='c1898a3f-7daf-5768-b348-05f179fe6abf', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='c1898a3f-7daf-5768-b348-05f179fe6abf', order_id='0000000000000001', state='F', price=127.08999633789062, shares=100, timestamp=1485959400055000, symbol='AAPL', account_id=0, clordid=None, collect=None, intent='init', order_algorithm='48c76891-2d0b-424e-91de-480d15718761', expected_direction='increase', actual_direction='increase', script_class_name='Cow', line_number=17, kind=8), '\n')
BUY 2017-02-01 09:30:00.123000  GDX in on_fill()
(FillEvent(instruction_id='c2092d2b-b0a7-5513-bbda-0ef5eb70b5a1', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='c2092d2b-b0a7-5513-bbda-0ef5eb70b5a1', order_id='0000000000000002', state='F', price=23.559999465942383, shares=100, timestamp=1485959400123000, symbol='GDX', account_id=0, clordid=None, collect=None, intent='init', order_algorithm='48c76891-2d0b-424e-91de-480d15718761', expected_direction='increase', actual_direction='increase', script_class_name='Cow', line_number=17, kind=8), '\n')
BUY 2017-02-01 09:30:00.127000  SPY in on_fill()
(FillEvent(instruction_id='b0b41a3a-44fe-5c56-b417-243017a9edfc', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='b0b41a3a-44fe-5c56-b417-243017a9edfc', order_id='0000000000000005', state='F', price=228.27999877929688, shares=100, timestamp=1485959400127000, symbol='SPY', account_id=0, clordid=None, collect=None, intent='init', order_algorithm='48c76891-2d0b-424e-91de-480d15718761', expected_direction='increase', actual_direction='increase', script_class_name='Cow', line_number=17, kind=8), '\n')
BUY 2017-02-01 09:30:00.161000  JNUG    in on_fill()
(FillEvent(instruction_id='abb63c91-cd11-53a3-a50a-ab0131ea101d', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='abb63c91-cd11-53a3-a50a-ab0131ea101d', order_id='0000000000000003', state='F', price=8.569999694824219, shares=100, timestamp=1485959400161000, symbol='JNUG', account_id=0, clordid=None, collect=None, intent='init', order_algorithm='48c76891-2d0b-424e-91de-480d15718761', expected_direction='increase', actual_direction='increase', script_class_name='Cow', line_number=17, kind=8), '\n')
BUY 2017-02-01 09:30:00.168000  NUGT    in on_fill()
(FillEvent(instruction_id='5019aa63-e06c-5b13-94c8-dbbe4451d640', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='5019aa63-e06c-5b13-94c8-dbbe4451d640', order_id='0000000000000004', state='F', price=10.579999923706055, shares=100, timestamp=1485959400168000, symbol='NUGT', account_id=0, clordid=None, collect=None, intent='init', order_algorithm='48c76891-2d0b-424e-91de-480d15718761', expected_direction='increase', actual_direction='increase', script_class_name='Cow', line_number=17, kind=8), '\n')
BUY 2017-02-01 09:30:00.169000  XLF in on_fill()
(FillEvent(instruction_id='2f642a65-50b4-5718-8c6c-383be70f5546', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='2f642a65-50b4-5718-8c6c-383be70f5546', order_id='0000000000000006', state='F', price=23.489999771118164, shares=100, timestamp=1485959400169000, symbol='XLF', account_id=0, clordid=None, collect=None, intent='init', order_algorithm='48c76891-2d0b-424e-91de-480d15718761', expected_direction='increase', actual_direction='increase', script_class_name='Cow', line_number=17, kind=8), '\n')
SELL    2017-02-01 09:31:00.001700  AAPL    in on_fill()
(FillEvent(instruction_id='c1898a3f-7daf-5768-b348-05f179fe6abf', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='c1898a3f-7daf-5768-b348-05f179fe6abf', order_id='0000000000000007', state='F', price=127.30999755859375, shares=-100, timestamp=1485959460001700, symbol='AAPL', account_id=0, clordid=None, collect=None, intent='exit', order_algorithm='market', expected_direction='reduce', actual_direction='reduce', script_class_name='Cow', line_number=37, kind=8), '\n')
SELL    2017-02-01 09:31:00.001700  GDX in on_fill()
(FillEvent(instruction_id='c2092d2b-b0a7-5513-bbda-0ef5eb70b5a1', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='c2092d2b-b0a7-5513-bbda-0ef5eb70b5a1', order_id='0000000000000008', state='F', price=23.549999237060547, shares=-100, timestamp=1485959460001700, symbol='GDX', account_id=0, clordid=None, collect=None, intent='exit', order_algorithm='market', expected_direction='reduce', actual_direction='reduce', script_class_name='Cow', line_number=37, kind=8), '\n')
SELL    2017-02-01 09:31:00.001700  JNUG    in on_fill()
(FillEvent(instruction_id='abb63c91-cd11-53a3-a50a-ab0131ea101d', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='abb63c91-cd11-53a3-a50a-ab0131ea101d', order_id='0000000000000009', state='F', price=8.539999961853027, shares=-100, timestamp=1485959460001700, symbol='JNUG', account_id=0, clordid=None, collect=None, intent='exit', order_algorithm='market', expected_direction='reduce', actual_direction='reduce', script_class_name='Cow', line_number=37, kind=8), '\n')
SELL    2017-02-01 09:31:00.001700  NUGT    in on_fill()
(FillEvent(instruction_id='5019aa63-e06c-5b13-94c8-dbbe4451d640', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='5019aa63-e06c-5b13-94c8-dbbe4451d640', order_id='0000000000000010', state='F', price=10.59000015258789, shares=-100, timestamp=1485959460001700, symbol='NUGT', account_id=0, clordid=None, collect=None, intent='exit', order_algorithm='market', expected_direction='reduce', actual_direction='reduce', script_class_name='Cow', line_number=37, kind=8), '\n')
SELL    2017-02-01 09:31:00.001700  SPY in on_fill()
(FillEvent(instruction_id='b0b41a3a-44fe-5c56-b417-243017a9edfc', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='b0b41a3a-44fe-5c56-b417-243017a9edfc', order_id='0000000000000011', state='F', price=228.3800048828125, shares=-100, timestamp=1485959460001700, symbol='SPY', account_id=0, clordid=None, collect=None, intent='exit', order_algorithm='market', expected_direction='reduce', actual_direction='reduce', script_class_name='Cow', line_number=37, kind=8), '\n')
SELL    2017-02-01 09:31:00.001700  XLF in on_fill()
(FillEvent(instruction_id='2f642a65-50b4-5718-8c6c-383be70f5546', script_id=u'762703c2-9ec5-4b42-98a1-7827ffb32018', user_key='2f642a65-50b4-5718-8c6c-383be70f5546', order_id='0000000000000012', state='F', price=23.559999465942383, shares=-100, timestamp=1485959460001700, symbol='XLF', account_id=0, clordid=None, collect=None, intent='exit', order_algorithm='market', expected_direction='reduce', actual_direction='reduce', script_class_name='Cow', line_number=37, kind=8), '\n')