<< Click to Display Table of Contents >>

Navigation:  NinjaScript > Language Reference > Strategy >


Previous page Return to chapter overview Next page


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().
If you are relying on the OnOrderUpdate() method to trigger actions such as the submission of a stop loss order when your entry order is filled ALWAYS reference the properties on the Order object passed into the OnOrderUpdate() method.
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.


Multi-threaded consideration

NinjaTrader is a multi-threaded application and therefore it is extremely important to understand the following concepts:


This method guarantees that you will see each order state change in sequence
The "order" method parameter represents the core order object updated by NinjaTrader
The supplementary method parameters provide an updating value representing each order change in sequence.  Think of this as the relevant information on the order at the time the state changed.
Since the "order"  method parameter represents the current order object state, it is possible for the updating values of that object to be out of sync with the correspond method parameters during a particular order update event.


As an example, the NinjaTrader core may have received "Working" and then "PartFilled" order state change events back from the broker API on thread "B". At some point in time (milliseconds later) the NinjaTrader core will take these events and trigger the OnOrderUpdate() method in the strategy on thread "A". Thus, when the strategy receives the first "Working" state for an order, the orderState method parameter will reflect the "Working" state although the actual order.OrderState is really in a state of "Part Filled".  You would see that current value truly reflected in the core Order object method parameter or any order objects returned in any of the order methods such as EnterLong(). Of course, the OnOrderUpdate() method parameters will eventually receive the event for "PartFilled" state in the sequence the events were received.


Considering the concept above, if you are unsure if you should be using the core order object value vs the updating method parameter value value, ask your self if you are truly looking for the most current order state, or the sequence of order states:


For the most current order state, use the core "order" object property (e.g., order.OrderState, order.LimitPrice, order.StopPrice, etc)
For the sequence of order states, use the updating method parameter value (e.g., orderState, limitPrice, stopPrice, etc)


Please see the example on the bottom of this page on "Understanding the core order object parameter vs updating value parameter"



Critical: 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.


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


An Order object representing the core order object state


A double value representing the limit price of the order update


A double value representing the stop price of the order update


An int value representing the quantity of the order update


An int value representing the filled amount of the order update


A double value representing the average fill price of the order update


An OrderState value representing the state of the order (e.g., filled, cancelled, rejected, etc)


Note:  See order state values table below


A DateTime structure representing the last time the order changed state


An ErrorCode value which categorizes an error received  from the broker


Possible values are:
















A string representing the error message provided directly from the broker


OrderState Values


Order is initialized in NinjaTrader


Order is submitted to the broker


Order is accepted by the broker or exchange


Order is pending submission


Order is working in the exchange queue


Order change is pending in NinjaTrader


Order change is submitted to the broker


Order cancellation is pending in NinjaTrader


Order cancellation is submitted to the broker


Order is rejected


Order is partially filled


Order is completely filled


An unknown order state. Default if broker does not report current order state.





Understanding the core order object parameter vs updating value parameter


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




Properly assigning order object values


private Order entryOrder = null;
protected override void OnBarUpdate()
  if (entryOrder == null && Close[0] > Open[0])
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
  // check if the current order matches the orderName passed in "EnterLong"()
  // 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 guaranteed to be complete if it is referenced immediately after submitting
  if (order.Name == "entryOrder")
      entryOrder = order;
  // if entry order exists
  if (entryOrder != null && entryOrder == order)
      if (order.OrderState == OrderState.Cancelled)
          // Do something here
          entryOrder = null;

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