<< Click to Display Table of Contents >>

Navigation:  NinjaScript > Language Reference > Strategy >


Previous page Return to chapter overview Next page


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.



Multi-threaded consideration

NinjaTrader is a multi-threaded application and therefore it is extremely important to understand the following concepts:


This method guarantees that you will see each position change in sequence
The "position" method parameter represents the core position object updated by NinjaTrader
The supplementary method parameters provide an updating value representing each position change in sequence.  Think of this as the relevant information on the position at the time the position changed.
Since the "position" method parameter represents the current position object, it is possible for the current values of that object to be out of sync with the correspond updating method parameters values during a particular position update event.


As an example, an account already in a "Long" position state which is updating to "Flat", the NinjaTrader core may have multiple partial position changes events back from the broker API on thread "B". At some point in time (milliseconds later) the NinjaTrader core will take these events and trigger the OnPositionUpdate() method in the strategy on thread "A". Thus, when the strategy receives the first position update, the "marketPosition" method parameter will reflect "Long" although the actual "position.MarketPosition" state is really "Flat".  You would see that current value truly reflected in the core position object method parameter or any position objects exposed to the strategy. Of course, the OnPositionUpdate() method parameters will eventually receive the event for "Flat" in the sequence the events were received.


Considering the concept above, if you are unsure if you should be using the core position object value vs the updating method parameter value, ask your self if you are truly looking for the most current position state, or the sequence of position states:


For the most current position, use the core position object property (e.g., position.MarketPosition, position.Quantity, etc)
For the sequence of position updates, use the updating method parameter value (e.g., marketPosition, averagePrice, etc)


Please see the example on the bottom of this page on "Understanding the core position object parameter vs updating value parameter"


Method Return Value

This method does not return a value.


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


A Position object representing the current core position object


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


An int value representing the updating quantity of a position


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


Possible values are:








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



Understanding the core order object parameter vs updating value parameter


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.