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 "Order Methods" section has a method overload designed for advanced handling.
Live 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.
The IOrder Class
All order methods return an IOrder object. Several things you need to be aware of:
The following example code demonstrates the submission of an order and assigning the IOrder return object to the variable "entryOrder" and then checking the "Token" property in the OnOrderUpdate() method for equality and then checking if the order was filled.
|
private IOrder entryOrder = null; protected override void OnBarUpdate()
protected override void OnOrderUpdate(IOrder order)
|
Working 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()
|