NinjaScript > Language Reference > Strategy > Order Methods > Managed Approach >

Advanced Order Handling

Print this Topic Previous pageReturn to chapter overviewNext page

Advanced order handling is reserved for EXPERIENCED programmers. Through advanced order handling you can submit, change and cancel orders at your discretion through any event-driven method within a strategy. Each order method within the "Managed Approach" section has a method overload designed for advanced handling.


tog_minusLive Until Cancelled Orders

Orders can be submitted that are live until you call the CancelOrder() method or the order's time in force has expired, which ever comes first. This flexibility allows you to control exactly when an order should be cancelled instead of relying on the close of a bar. Each order method such as EnterLongLimit() has a method variation (overload) designed to submit a live until cancelled order. When using this overload please be sure to hold onto the IOrder object for the order to be able to call CancelOrder() on it at a later point in time.

tog_minusThe IOrder Class

All order methods return an IOrder object. Several things you need to be aware of:


An IOrder object returned from calling an order method is dynamic in that its properties will always reflect the current state of an order
The property <IOrder>.OrderId is NOT a unique value since it can change throughout an order's lifetime
The property <IOrder>.Token is NOT a unique value since it will change as the strategy transitions from historical to real-time
To check for equality you can compare IOrder objects directly


The following example code demonstrates the submission of an order and assigning the IOrder return object to the variable "entryOrder" and then checking the object in OnOrderUpdate() method for equality and then checking if the order was filled.


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)
        if (order.OrderState == OrderState.Filled)
              entryOrder = null;

tog_minusWorking with a Multi-Instrument Strategy

With advanced order handling you can submit an orders in the context of any bars object by designating the "BarsInProgress" index. What this means is that if your primary bar series is "MSFT" and your secondary series added to the strategy through the Add() method is "AAPL", you can submit an order for either "MSFT" or "AAPL" anywhere from within the strategy. There is detailed information on working with multi-time frame and instrument strategies however, this sub-section strictly deals with the concept of order submission.


Let's take as an example the EnterLongLimit() method and one of its method variations (below) designed for advanced order handling:        


 EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)


Let's also continue with the example above where a "MSFT" 1 minute chart is the primary bar series the strategy is running on. We add a secondary bar series, an "AAPL" 1 min series to our strategy by calling the Add() method in the Initialize() method as follows:


Add("AAPL", PeriodType.Minute, 1);


In NinjaScript, "MSFT" has a bars index value of "0" and "AAPL" would have bars index value of "1". More information on this concept can be found in the multi-time frame and instrument strategies section as well as the property BarsInProgress. In a multi-instrument strategy, using the EnterLongLimit() method above will generate an order for the instrument that is associated to the value of the "barsInProgressIndex" parameter you pass in.


The following example code demonstrates how you can monitor for bar update events on "MSFT" but submit an order to "AAPL". The key is in passing in a value of "1" (bold red below) into for the "BarsInProgressIndex". A value of 1 represents the "AAPL" bar series and thus the order is submitted for "AAPL".


private IOrder entryOrder = null;

protected override void OnBarUpdate()
    // Check if the MSFT series triggered an bar update event
    if (BarsInProgress == 0)

        // Submit an order for AAPL in the context of MSFT bar update event
        if (entryOrder == null)
              entryOrder = EnterLongLimit(1, true, 1, Lows[1][0], "AAPL Order");