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()
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')
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:
|
user_key | string | None | User-selected key (previously attached to one or more orders) to filter on before canceling |
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.