NinjaScript > Language Reference > Strategy >

OnOrderUpdate()

Print this Topic Previous pageReturn to chapter overviewNext page

Definition
The OnOrderUpdate() method is called everytime 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.

 

Programming to this method is considered advanced programming and exposed for experienced programmers
You can program your own order rejection handling

 

NinjaTrader is a multi-threaded application and therefore it is extremely important to understand

 

This method guarantees that you will see each order state change in sequence
This method does not provide an update for the most current state of an order but instead provides you an event notifying you of each state change in sequence and the relevant information on the order at the time the state changed
CRITICAL: If you want to drive your strategy logic based on order fills you must use OnExecution() instead of OnOrderUpdate(). OnExecution() is always triggered after OnOrderUpdate(). There is internal strategy logic that is triggered after OnOrderUpdate() is called but before OnExecution() that can adversely affect your strategy if you are relying on tracking fills within OnOrderUpdate().
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" and 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 IOrder object passed in will reflect the "Working" state although the actual order is really in a state of "Part Filled" which is truly reflected in the original IOrder object returned in any of the order methods such as EnterLong(). Of course, the OnOrderUpdate() method will subsequently receive the event for "PartFilled" state.
When connected to the Market Replay 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 IOrder object. This is done to ensure that all events are in sync at high speed market replays.

 

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 IOrder object passed into the OnOrderUpdate() method.

 

 

Method Return Value

This method does not return a value.

 

Method Parameters

IOrder order

 

Syntax
You must override the method in your strategy with the following syntax.

 

protected override void OnOrderUpdate(IOrder order)
{
 
}

 

 

Examples

private IOrder entryOrder = null;

 

protected override void OnBarUpdate()
{
    if (entryOrder == null && Close[0] > Open[0])
         entryOrder = EnterLong();
}

 

protected override void OnOrderUpdate(IOrder order)
{
    if (entryOrder != null && entryOrder == order)
    {
         Print(order.ToString());
        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.