1. This method sends a cancel request to the broker and does not guarantee that an order is completely canceled. Most of the time you can expect your order to come back 100% canceled.
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.
I'm using:
private Order myEntryOrder = null;
private int barNumberOfOrder = 0;
protected override void OnBarUpdate()
{
// Submit an entry order at the low of a bar
if (myEntryOrder == null)
{
// use 'live until canceled' limit order to prevent default managed order handling which would expire at end of bar
EnterLongLimit(0, true, 1, Low[0], "Long Entry");
barNumberOfOrder = CurrentBar;
}
// If more than 5 bars has elapsed, cancel the entry order
if (CurrentBar > barNumberOfOrder + 5)
CancelOrder(myEntryOrder);
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity , int filled,
double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError )
{
// Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
// This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
if (order.Name == "Long Entry")
myEntryOrder = order;
// Evaluates for all updates to myEntryOrder.
if (myEntryOrder != null && myEntryOrder == orde r)
{
// Check if myEntryOrder is cancelled.
if (myEntryOrder.OrderState == OrderState.Cancel led)
{
// Reset myEntryOrder back to null
myEntryOrder = null;
}
}
}
Is there anything I'm missing in the script that causes an order not to cancel 100% of the time?
Comment