Mariner Backtesting - order.send() MKT Market Order (Simple)

Market Order

Market Order (Simple Order Algo) Overview:

Function to send a market order, buy or sell. Type is not necessary as orders will default to Market.

Our backtesting engine will attempt to give you an accurate simulation of your fill using the market conditions at the time the order was entered.

Note: if you try to fill outside market hours or on extremely thin symbols the fill engine may fill you at the bid or ask which can be spread wide and sometimes non-existant. If they do not exist then the simulator will fill you at 0 or $199,999.99 to draw your attention to the riskiness of the trade.

Example

order.send(self.symbol, 'buy', 10, type='MKT')

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 No The order type. Defaults to market. MKT
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=5)
Working Example: (link)
# Copyright Cloudquant, LLC. All right reserved.
# MKT Order - CQ LITE & ELITE - Market Order - Place a market order right away
# Run on 1/18/2018. We test with GOOG, we start checking at 12:50 with the GOOG price around 127.5

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

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

    def on_start(self, md, order, service, account):
        self.status = 0

    def on_minute_bar(self, event, md, order, service, account, bar):
        if event.timestamp >= service.time(12,50):
            time_string =service.time_to_string(event.timestamp, format='%H:%M:%S') # going to print this a few times so no point repeatedly calling it and code is easier to read.
            mbar = bar.minute(start=-1)
            mlow = mbar.low[0]
            mhigh = mbar.high[0]
            print("{} -  prevbarlow{:8.2f}  bid{:8.2f}  last{:8.2f}  ask{:8.2f}  prevbarhigh{:8.2f}".format(time_string, mlow,md.L1.bid, md.L1.last, md.L1.ask,mhigh))

            if self.status==0: 
                self.order_handle_b = order.send(self.symbol, 'buy', 10, type='MKT')  # Market order, no price required
                print("{} - Buy  Market order sent {}, most recent print{:8.2f}".format(time_string ,self.symbol,md.L1.last))
                self.order_handle_s = order.send(self.symbol, 'sell', 10, type='MKT') # Market order, no price required
                print("{} - Sell Market order sent {}, most recent print{:8.2f}".format(time_string ,self.symbol,md.L1.last))
                self.status = 1

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

Console

12:50:01 -  prevbarlow 1127.51  bid 1127.51  last 1127.55  ask 1127.99  prevbarhigh 1127.98
12:50:01 -  Buy  Market order sent GOOG, most recent print 1127.55
12:50:01 -  Sell Market order sent GOOG, most recent print 1127.55
12:50:01 -  Filled,  price 1127.99
12:50:01 -  Filled,  price 1127.51
12:51:00 -  prevbarlow 1127.51  bid 1127.58  last 1127.89  ask 1127.93  prevbarhigh 1127.97
...