OnOrderUpdate()

View as Markdown

Definition

An event driven method which is called each time an order managed by a strategy changes state. An order will change state when a change in order quantity, price or state (working to filled) occurs. You can use this method to program your own order rejection handling.

  • Only orders which have been submitted and managed by the strategy will call OnOrderUpdate().
  • Programming in this environment is reserved for the more advanced user. If you are for example looking to protect a strategy managed position with a basic stop and target, then the Set() methods would be more convenient.
  • For triggering actions such as the submission of a stop loss order and target order using custom OCO logic when your entry order is filled, we recommend working directly in OnExecutionUpdate() instead.
  • OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated.
  • Rithmic and Interactive Brokers Users: When using a NinjaScript strategy it is best practice to only work with passed by value data from OnExecutionUpdate(). Instances of multiple fills at the same time for the same instrument might result in an incorrect OnPositionUpdate, as sequence of events are not guaranteed due to provider API design. For an example on protecting positions with this approach, see OnExecutionUpdate().
  • If you want to drive your strategy logic based on order fills you must use OnExecutionUpdate() instead of OnOrderUpdate(). OnExecutionUpdate() is always triggered after OnOrderUpdate(). There is internal strategy logic that is triggered after OnOrderUpdate() is called but before OnExecutionUpdate() that can adversely affect your strategy if you are relying on tracking fills within OnOrderUpdate().

Playback Connection

When connected to the Playback Connection, calling market order based methods such as EnterLong() and EnterShort() will result in order state events being fired prior to the order method return an Order object. This is done to ensure that all events are in sync at high speed playback.

Method Return Value

This method does not return a value.

Syntax

You must override the method in your strategy with the following syntax:

protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)

Method Parameters

ParameterDescription
orderAn Order object passed by reference representing the order object
limitPriceA double value representing the limit price of the order update
stopPriceA double value representing the stop price of the order update
quantityAn int value representing the quantity of the order update
filledAn int value representing the filled amount of the order update
averageFillPriceA double value representing the average fill price of the order update
orderStateAn OrderState value representing the state of the order (e.g., filled, canceled, rejected, etc). Note: See order state values table below
timeA DateTime structure representing the last time the order changed state
errorAn ErrorCode value which categorizes an error received from the broker. Possible values are: ErrorCode.LoginExpired, ErrorCode.LogOnFailed, ErrorCode.NoError, ErrorCode.OrderRejected, ErrorCode.OrderRejectedByRisk, ErrorCode.Panic, ErrorCode.UnableToCancelOrder, ErrorCode.UnableToChangeOrder, ErrorCode.UnableToSubmitOrder, ErrorCode.UserAbort
commentA string representing the error message provided directly from the broker

OrderState Values

OrderStateDescription
OrderState.InitializedOrder is initialized in NinjaTrader
OrderState.SubmittedOrder is submitted to the broker
OrderState.AcceptedOrder is accepted by the broker or exchange
OrderState.TriggerPendingOrder is pending submission
OrderState.WorkingOrder is working in the exchange queue
OrderState.ChangePendingOrder change is pending in NinjaTrader
OrderState.ChangeSubmittedOrder change is submitted to the broker
OrderState.CancelPendingOrder cancellation is pending in NinjaTrader
OrderState.CancelSubmittedOrder cancellation is submitted to the broker
OrderState.CancelledOrder cancellation confirm received from broker
OrderState.RejectedOrder is rejected
OrderState.PartFilledOrder is partially filled
OrderState.FilledOrder is completely filled
OrderState.UnknownAn unknown order state. Default if broker does not report current order state.

Examples

Understanding the order object parameter vs updating value parameter** (Multi-Thread Considerations for NinjaScript)

1protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
2 int quantity, int filled, double averageFillPrice,
3 Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
4{
5 Print("The most current order state is: " + order.OrderState); // OrderState.PartFilled
6 Print("This particular order update state is: " + orderState); // OrderState.Working
7}

Properly assigning order object values

1private Order entryOrder = null;
2
3protected override void OnBarUpdate()
4{
5 if (entryOrder == null && Close[0] > Open[0])
6 EnterLong("entryOrder");
7}
8
9protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
10{
11 // check if the current order matches the orderName passed in "EnterLong"()
12 // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
13 // 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
14 if (order.Name == "entryOrder")
15 entryOrder = order;
16
17 // if entry order exists
18 if (entryOrder != null && entryOrder == order)
19 {
20 Print(order.ToString());
21 if (order.OrderState == OrderState.Cancelled)
22 {
23 // Do something here
24 entryOrder = null;
25 }
26 }
27}

Additional reference code samples are available in the NinjaScript Educational Resources section of our support forum.