Order Cancel Event
Called whenever a previously placed order is canceled.
Interface: def on_cancel(self,
event,
md,
order,
service,
account):
pass
Name | Type | Information |
---|---|---|
account_id | integer | The account id of the order event. Will be the same as account id of the script. |
clordid | string | Client Order Id in FIX. Is generated when the order is sent. Will be the same on executions. |
expected_direction | enum | Either increase, reduce, or reverse. This will be set when the order is first placed, and is a prediction of whether your order will increase, reduce, or reverse your current posistion. Note a short on a symbol that you do not currently hold would be considered an increase of that position. This can obviously change as it's possible you have orders in flight, that haven't filled yet. Can be usefull in debugging order behavior |
instruction_id | string | Unique GUID for the instance of the script that generated the initial order. Will be None if the initial order wasn't started from same machine (e.g. from trading client) |
intent | string | The intent attribute you set in the order that generated this event. |
kind | integer | Event enum (6) representing the kind of event. *LIVE AND FORWARD ONLY*, represents the type of event (trade, quote, news, imbalance etc) |
line_number | string | line_number is the line in your strategy script where the order was placed that generated this event. Can be used to help debug order behavior |
order_algorithm | string | The order_algorithm GUID you set in the order that generated this event. This can be useful in debugging order behavior. |
order_id | string | Unique GUID of order being cancel (very similar clordid). Will be None if the initial order wasn't started from same machine (e.g. from trading client) |
price | float | Price of the order/cancel. |
reason | string | Text passed back on the cancel. |
script_class_name | string | script_class_name represents the name of your Strategy class that placed the order that generated this event. Can be used to help debug order behavior |
script_id | string | GUID of the script. Will be None if the initial order wasn't started from same machine (e.g. from trading client) |
shares | integer | Shares being canceled. May differ from order shares if partially filled |
state | string | Order Message's States |
symbol | string | Event Symbol. In most cases symbol is obvious (self.symbol, md.symbol). If the script has subscribed to multiple symbols events, then event.symbol should be used to identify the symbol. |
timestamp | muts | Timestamp of the event. |
user_key | string | user_key set on the order. Can be used to identify trades that go together (pairs/batch/strategy). account object can be filtered on user_key |
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 symbol and timestamp
print(self.symbol + "\n" + service.time_to_string(service.system_time) + "\tin on_start()\n\n")
#instance variable
self.do_once =True
def on_trade(self, event, md, order, service, account):
print( service.time_to_string(service.system_time) + "\tin on_trade()\n\n")
if self.do_once :
#send order
order.algo_buy(self.symbol, "limit", intent="init", order_quantity=100, price=0.01)
#cancel order
order.cancel(self.symbol)
# change instance variable to False
self.do_once = False
def on_cancel(self, event, md, order, service, account):
# print a timestamp
print( service.time_to_string(service.system_time) + "\tin on_ack()\n\n")
print("\nAn order has been cancelled \n")
service.terminate()
Console
AAL 2016-08-15 09:25:00.000000 2016-08-15 09:30:00.147000 An order has been cancelled