OnPositionUpdate()

<< Click to Display Table of Contents >>

Navigation:  NinjaScript > Language Reference > Strategy >

OnPositionUpdate()

Previous page Return to chapter overview Next page

Definition

An event driven method which is called each time the position of a strategy changes state.

 

This method is called after OnExecutionUpdate()
OnPositionUpdate() is guaranteed to be called for every change in strategy position

 

NOTE: You will NOT receive position updates for manually placed orders, or orders managed by other strategies (including any ATM strategies) in OnPositionUpdate(). The Account class contains a pre-built event handler (PositionUpdate) which can be used to filter position updates on a specified account.

 

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 OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
{
 
}  

 

 

Method Parameters

position

A Position object passed by reference representing the current position object

averageFillPrice

A double value representing the updating average fill price of a position

quantity

An int value representing the updating quantity of a position

marketPosition

A MarketPosition object representing the updating position update provided directly from the broker.

 

Possible values are:

 

MarketPosition.Flat
MarketPosition.Long
MarketPosition.Short

 

 

Examples

ns

 

protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
      int quantity, Cbi.MarketPosition marketPosition)
{
  if (position.MarketPosition == MarketPosition.Flat)
  {
    // Do something like reset some variables here
  }
}

 

ns

Understanding the order object parameter vs updating value parameter (Multi-Thread Considerations for NinjaScript)

 

protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
      int quantity, Cbi.MarketPosition marketPosition)
{
  Print("the most current position MarketPosition is: " + position.MarketPosition);   // Flat
  Print("this particular position update marketPosition is: " + quantity); // Long
}

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