Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

When and how is Position object updated?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    When and how is Position object updated?

    Hi,

    Could you please let me know when Position object gets updated?

    It appears that in OnOrderUpdate handler it has its Position.MarketPosition property set correctly (is that always the case?) but the following is zero for the first trade:

    // unrealizedPL is zero in OnOrderUpdate when order state is Filled
    double unrealizedPL = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);

    I realize there is an update handler for Position object, however, it looks like Position object gets partially updated when control comes in OnOrderUpdate.

    Is my understanding correct? If so, what would be the workaround?

    Thanks.

    #2
    Hello alex.nt,
    We have not made any changes in how the position gets updated and it has always been such.

    It may be the case that the order is partially filled or you may be witnessing a race condition.

    OnOrderUpdate is updated first and then OnExecution gets updated. If you move your code to the OnExectuion event then can you get the correct data.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hi Joydeep,

      I have no doubts that you have not done any changes to how Position object is updated, my question is rather about how it occurs.

      As I mentioned in my original post the order state is Filled, i.e. order is fully filled, yet the expression Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) returns 0, yet the Position.MarketPosition is NOT flat.

      Therefore, Position object may get updated partially.

      Therefore my question is: when and how Position object is updated? I realize it is updated in OnPositionUpdate but it appears that Position.MarketPosition is sort of exception from the rule. Are there any other exceptions?

      Can I rely upon (or how much can I rely upon) a Position object in handlers other than OnPositionUpdate? Like in OnOrderUpdate, OnExecution, etc?

      Thanks

      Comment


        #4
        Hello alex.nt,
        If the current market price is equal to the fill price then the unrealized PnL value will be 0 (zero).

        If you try printing out the value of the PnL along with the position average price and the current market price, then what values are you getting.

        Code:
        Print("Unrealized PnL= " + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency).ToString() + " Avg Price= " + Position.AvgPrice.ToString() + " Current Market Price= " + Close[0].ToString() );
        I look forward to assisting you further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Yes, that was exactly the case: Close == Fill Price, thanks, I feel a bit stupid.

          However, anyways, does Position object get always updated before OnOrderUpdate call? In other words, may I rely upon it in OnOrderUpdate?

          Comment


            #6
            Hello alex.nt,
            Should be fine as I am not aware of any known limitations regarding it.
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Rapine Heihei, 04-23-2024, 07:51 PM
            2 responses
            30 views
            0 likes
            Last Post Max238
            by Max238
             
            Started by Shansen, 08-30-2019, 10:18 PM
            24 responses
            943 views
            0 likes
            Last Post spwizard  
            Started by Max238, Today, 01:28 AM
            0 responses
            9 views
            0 likes
            Last Post Max238
            by Max238
             
            Started by rocketman7, Today, 01:00 AM
            0 responses
            7 views
            0 likes
            Last Post rocketman7  
            Started by wzgy0920, 04-20-2024, 06:09 PM
            2 responses
            28 views
            0 likes
            Last Post wzgy0920  
            Working...
            X