Mariner Backtesting - order.cancel()

Function for canceling orders. This function can be used to cancel orders in multiple ways. Take some time to explore the options.


Key points:
  • order.cancel() replaces ktgfunc.cancel_helper()
  • Cancel pending orders for some criterion for a given symbol and/or user_key.
  • Note that if both symbol and user_key are provided, they will both need to match for the order(s) to be canceled.
  • Note that if both user_key and criteria are provided, they will both need to match for the order(s) to be canceled.
  • symbol=None applies the action to all symbols in the account.
  • The call of order.cancel() is queued until the current Strategy method returns from being called. In other words, the system doesn't process the cancel request until after returning from the system method.
  • The account object is held constant until the current Strategy method returns from being called. In other words, the account won't reflect being canceled if queried right after calling order.cancel()
Interface:
 cancel(symbol=None,
       order_id=None,
       criteria=None,
       user_key=None)

Samples - Calling Method

 order.cancel( self.symbol )

The following example cancels all pending buy orders for AAPL:

 order.cancel(symbol='AAPL', criteria='bids')

This example cancels all pending orders for all symbols that contain the key 'some_user_key':

 order.cancel(user_key='some-user-key')

This example cancels all pending sell orders for the 'AAPL' symbol that have the key 'some_user_key':

 order.cancel(symbol='AAPL', criteria='offers', user_key='some-user-key')
Parameters:
Name Type Default Information
symbol string None Symbol to be cancelled. None applies the action to all symbols in the account.
order_id string None The order_id returned from the order submission
criteria string None Criteria can be one of the following strings:
  • "all": All pending buy and sell orders that match the symbol and/or user_key
  • "bids": All pending buy orders that match the symbol and/or user_key
  • "offers": All pending sell orders that match the symbol and/or user_key
  • "oldest": The least-recently-placed pending buy or sell order that matches the symbol and/or user_key
  • "newest": The most-recently-placed pending buy or sell order that matches the symbol and/or user_key. If orders from different symbols match the user_key, this cancels one order for each symbol.
An exception is raised if the criteria doesn't match one of the above options. If orders from different symbols match the user_key, this cancels one order for each symbol.
user_key string None User-selected key (previously attached to one or more orders) to filter on before canceling
Working Examples:
 from cloudquant.interfaces import Strategy


class OnCancelExample(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")

        # send order
        order.algo_buy(self.symbol, "limit", intent="init", order_quantity=100, price=0.01)
        print('Buy order sent.\n\n')

    def on_minute_bar(self, event, md, order, service, account, bar):
        print(service.time_to_string(event.timestamp) + "\tin on_minute_bar()\n")

        # cancel order
        order.cancel(self.symbol)
        print('Cancel order sent.\n\n')

        service.terminate()

        # The order will be canceled, positions will be = to 0
        # profit and loss will be equal to 0

Console

AAL
2016-09-07 09:30:00.000000  in on_start()

Buy order sent.


2016-09-07 09:30:00.323000  in on_minute_bar()

Cancel order sent.