OnExecutionUpdate()

View as Markdown

Definition

An event driven method which is called on an incoming execution of an order managed by a strategy. An execution is another name for a fill of an order.

  • An order can generate multiple executions (partial fills)
  • OnExecutionUpdate is typically called after OnOrderUpdate()
  • Only orders which have been submitted and managed by the strategy will call OnExecutionUpdate()
  • Executions drive the strategy Position object, which is updated when this method is called
  • 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.
  • When connected to the Playback connection, it is possible for OnExecutionUpdate() to trigger in the middle of a call to OnBarUpdate(). The Sim101 account adds a simulated random delay for processing execution events, but the Playback connection triggers executions immediately, for the sake of consistency in backtesting. Because of this, OnExecutionUpdate() can appear to be triggered earlier than it would in live trading, or when simulation trading on a live connection.
  • Please also review Multi-Thread Considerations for NinjaScript.
  • Its best practice to only work with the passed by value parameters and not reference parameters. This insures that you process each change of the underlying state.
  • 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.

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 OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)

Parameters

ParameterDescription
executionAn Execution object passed by reference representing the execution
executionIdA string value representing the execution id
priceA double value representing the execution price
quantityAn int value representing the execution quantity
marketPositionA MarketPosition object representing the position of the execution. Possible values are:
MarketPosition.Long
MarketPosition.Short
orderIdA string representing the order id
timeA DateTime value representing the time of the execution

Examples

OnExecutionUpdate Example (See SampleOnOrderUpdate for complete example)

1private Order entryOrder = null; // This variable holds an object representing our entry order
2private Order stopOrder = null; // This variable holds an object representing our stop loss order
3private Order targetOrder = null; // This variable holds an object representing our profit target order
4private int sumFilled = 0; // This variable tracks the quantities of each execution making up the entry order
5
6protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
7{
8 /* We advise monitoring OnExecutionUpdate() to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
9 which ensures your strategy has received the execution which is used for internal signal tracking. */
10 if (entryOrder != null && entryOrder == execution.Order)
11 {
12 if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
13 {
14 // We sum the quantities of each execution making up the entry order
15 sumFilled += execution.Quantity;
16
17 // Submit exit orders for partial fills
18 if (execution.Order.OrderState == OrderState.PartFilled)
19 {
20 stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - 4 * TickSize, "MyStop", "MyEntry");
21 targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
22 }
23 // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
24 else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
25 {
26 // Stop-Loss order for OrderState.Filled
27 stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - 4 * TickSize, "MyStop", "MyEntry");
28 targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
29 }
30
31 // Resets the entryOrder object and the sumFilled counter to null / 0 after the order has been filled
32 if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
33 {
34 entryOrder = null;
35 sumFilled = 0;
36 }
37 }
38 }
39
40 // Reset our stop order and target orders' Order objects after our position is closed. (1st Entry)
41 if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
42 {
43 if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
44 {
45 stopOrder = null;
46 targetOrder = null;
47 }
48 }
49}

Additional Reference Samples

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