CancelOrder()

View as Markdown

Definition

Cancels a specified order. This method is reserved for experienced programmers that fully understand the concepts of advanced order handling.

Notes:

  1. This method sends a cancel request to the broker and does not guarantee that an order is completely cancelled. Most of the time you can expect your order to come back 100% cancelled.
  2. An order can be completely filled or part filled in the time that you send the cancel request and the time the exchange receives the request. Check the OnOrderUpdate() method for the state of an order you attempted to cancel.

Syntax

CancelOrder(Order order)

Warning: If you have existing historical order references which have transitioned to real-time, you MUST update the order object reference to the newly submitted real-time order; otherwise errors may occur as you attempt to cancel the order. You may use the GetRealtimeOrder() helper method to assist in this transition.

Parameters

ParameterDescription
orderAn Order object representing the order you wish to cancel.

Examples

1private Order myEntryOrder = null;
2private int barNumberOfOrder = 0;
3
4protected override void OnBarUpdate()
5{
6 // Submit an entry order at the low of a bar
7 if (myEntryOrder == null)
8 {
9 // use 'live until canceled' limit order to prevent default managed order handling which would expire at end of bar
10 EnterLongLimit(0, true, 1, Low[0], "Long Entry");
11 barNumberOfOrder = CurrentBar;
12 }
13
14 // If more than 5 bars has elapsed, cancel the entry order
15 if (CurrentBar > barNumberOfOrder + 5)
16 CancelOrder(myEntryOrder);
17}
18
19protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
20 double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
21{
22 // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
23 // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
24 if (order.Name == "Long Entry")
25 myEntryOrder = order;
26
27 // Evaluates for all updates to myEntryOrder.
28 if (myEntryOrder != null && myEntryOrder == order)
29 {
30 // Check if myEntryOrder is cancelled.
31 if (myEntryOrder.OrderState == OrderState.Cancelled)
32 {
33 // Reset myEntryOrder back to null
34 myEntryOrder = null;
35 }
36 }
37}