Mariner Backtesting - order.cancel()

Cancel Order (Simple)

Cancel Order (Simple Order Algo) Overview:

Function to cancel an order.

Example

To be able to cancel an order you must have the order handle, the easiest way to achieve this is to capture it at the time of placing the order.

Normally you simply send the order like this...

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

Instead, assign it to a variable like this...

self.buy_handle = order.send(self.symbol, 'buy', 10, type='LMT', price=10)

Then to cancel this order simple pass the handle to the cancel function...

order.cancel(self.symbol, order_id=self.buy_handle)

Not for use with live trading

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

Working Example:(Link)
# Copyright Cloudquant, LLC. All right reserved.
# Cancel Order - CQ LITE & ELITE - Cancel Order
# Run on 1/18/2018 with GOOG as the symbol for testing purposes. We start checking at 12:50 with the GOOG price around 127.5
# We deliberately place an impossible Limit Buy order for GOOG @ $10 which will obviously never reach a fill.
# We capture the order handle as we place the order.
# Ten minutes later, if it has not been filled (!), we cancel the order using the symbol name and the order handle.

from cloudquant.interfaces import Strategy
class New_Order_Method_Cancel(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
        self.order_handle = 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='LMT', price=10)  # Buy  Limit - This price will never be reached
                print("{} -  Impossible Buy  Limit order sent for GOOG @ $10 {}".format(time_string ,self.symbol))
                self.status = 1
        if event.timestamp >= service.time(13,0) and self.status == 1: # 10 mins have passed with no fill...
                self.order_handle_bc = order.cancel(self.symbol, order_id=self.order_handle_b)
                print("{} -  10 mins have passed with no fill - Cancelled Buy  {}".format(time_string ,self.symbol))
                self.status = 2
                service.terminate()


# 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(event)
            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 -  Impossible Buy  Limit order sent for GOOG @ $10 GOOG
12:51:00 -  prevbarlow 1127.51  bid 1127.58  last 1127.89  ask 1127.93  prevbarhigh 1127.97
12:52:00 -  prevbarlow 1127.28  bid 1127.28  last 1127.30  ask 1127.82  prevbarhigh 1127.75
12:53:05 -  prevbarlow 1127.00  bid 1126.93  last 1127.63  ask 1127.62  prevbarhigh 1127.70
12:54:01 -  prevbarlow 1126.93  bid 1126.93  last 1127.10  ask 1127.53  prevbarhigh 1127.61
12:55:05 -  prevbarlow 1126.93  bid 1127.30  last 1127.75  ask 1128.31  prevbarhigh 1127.75
12:56:03 -  prevbarlow 1127.55  bid 1126.73  last 1127.70  ask 1128.27  prevbarhigh 1128.28
12:57:03 -  prevbarlow 1127.00  bid 1127.00  last 1127.62  ask 1128.23  prevbarhigh 1128.27
12:58:00 -  prevbarlow 1126.40  bid 1126.40  last 1126.40  ask 1127.63  prevbarhigh 1127.68
12:59:03 -  prevbarlow 1126.40  bid 1126.40  last 1126.77  ask 1127.54  prevbarhigh 1127.56
13:00:00 -  prevbarlow 1126.40  bid 1126.00  last 1126.69  ask 1126.69  prevbarhigh 1127.32
13:00:00 -  10 mins have passed with no fill - Cancelled Buy  GOOG