Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position.GetUnrealizedProfitLoss Lag

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

    Position.GetUnrealizedProfitLoss Lag

    I am testing some function of a strategy and I noticed that the result from the Position.GetUnrealizedProfitLoss method is inaccurate and has an extreme lag when compared to the results viable on the chart. I have set up a timer to print the current PnL every 3 seconds. You can see see it is displaying the wrong PnL I originally thought it was because of the stick spread but the tick spread is only 1, I am using the simulated data

    Code:
    currentPnL = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]); // Get the unrealzied PnL
    Click image for larger version

Name:	currentPnL.PNG
Views:	561
Size:	19.0 KB
ID:	1096209

    Attached Files

    #2
    Hello cutzpr,

    Thanks for your post.

    Close[0] references the close price of the bar the script is processing. If you are calculating OnBarClose, this will be up to date with the close value of the bar that had just closed. Close[0] will update with the close price of the developing bar value if you are using Calculate.OnEachTick, or OnPriceChange.

    If you are using a timer which then references Close[0], I suggest using TriggerCustomEvent to ensure the BarsAgo reference will be accurate.

    TriggerCustomEvent - https://ninjatrader.com/support/help...ustomevent.htm

    You could also consider having UnrealizedPnL calculated from the last tick seen in OnMarketData. I've included a snippet below.

    Code:
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
        if (marketDataUpdate.MarketDataType == MarketDataType.Last)
            Print(Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, marketDataUpdate.Price));
    }
    
    protected override void OnBarUpdate()
    {
        if (State == State.Historical)
            return;
        EnterLong();
    }
    We look forward to assisting.

    Comment


      #3
      Jim,

      Thank you very much. I always thought Close[0] was the most recent price but that is not true for Calculate.OnBarClose. This makes so much sense now. The snippet is very helpful and I will go that route.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      83 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      47 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      29 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      32 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      66 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X