OnExecutionUpdate()

<< Click to Display Table of Contents >>

Navigation:  NinjaScript > Language Reference > Strategy >

OnExecutionUpdate()

Previous page Return to chapter overview Next page

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 always called after OnOrderUpdate() is called
Only orders which have been submitted and managed by the strategy will call OnExecutionUpdate()

 

Notes:

If you are relying on the OnExecutionUpdate() 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 property attached to the Execution object passed into the OnExecutionUpdate() method.
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.

 

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

execution

An Execution object representing the execution

executionId

A string value representing the execution id

price

A double value representing the execution price

quantity

An int value representing the execution quantity

marketPosition

A MarketPosition object representing the position of the execution.  Possible values are:

MarketPosition.Long
MarketPosition.Short

orderId

A string representing the order id

time

A DateTime value representing the time of the execution

 

 

Examples

ns

Finding the executions of a particular Order object

 

// Example #1
private Order entryOrder = null;
 
protected override void OnBarUpdate()
{
  if (entryOrder == null && Close[0] > Open[0])
      EnterLong("myEntryOrder");
}
 
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
  // Assign entryOrder in OnExecutionUpdate() 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 (execution.Order.Name == "myEntryOrder")
      entryOrder = execution.Order;
  if (entryOrder != null && entryOrder == execution.Order)
      Print(execution.ToString());
}

 

 

ns

Generic execution logic not specific to a particular Order object

 

// Example #2
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
  // Remember to check the underlying Order object for null before trying to access its properties
  if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
      Print(execution.ToString());
}

 

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