Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Get Unrealized PNL

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

    Get Unrealized PNL

    For some reason this does not return the correct PNL for both instruments (multi-instrument strategy). I have tried this code in OnMarketData(MarketDataEventArgs marketDataUpdate) and it spits out incorrect numbers. I'm basically looking to get my unrealizedPNL for all open positions (not static pnl) and make quick decisions based on the net amount.

    For example:
    ES Active Trade= 100
    NQ Active Trade= -50
    Net amount= 50.
    I would like to get this net amount and quickly act on it.

    See code below:

    if (Positions[0].MarketPosition != MarketPosition.Flat)
    {
    Print("ES is up " + Positions[0].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]));
    Print("NQ is up " + Positions[2].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[2][0]));

    }

    Any help would be much appreciated.
    Last edited by bc24fl; 08-14-2019, 02:34 PM.

    #2
    Hello bc34fl,

    Thanks for your post.

    I have an example I have tested which is performing the expected behavior. Please test this example on your end. You may also test using Calculate.OnPriceChange so Closes[][] will update with each price change.

    We look forward to assisting.
    Attached Files

    Comment


      #3
      I believe the problem with Closes[][] is that it updates when the bar finishes. I'd like to get it once the price updates. My strategy uses Calculate.OnBarUpdate so i cannot use OnPriceChange. What about using marketDataUpdate.Price instead of closes[][] to get the price instantly?

      Comment


        #4
        Here is a screenshot of what the actual PNL is on top and what the output window says. I'm only showing ES but you can see it is clearly off.

        Comment


          #5
          The code below works but feels hackish.... If you can think of a better way i'm all ears. Thanks for you help.

          if ( (BarsInProgress == 0) && (marketDataUpdate.MarketDataType == MarketDataType.Last) )
          {
          esLastPrice = marketDataUpdate.Price;
          }

          if ( (BarsInProgress == 2) && (marketDataUpdate.MarketDataType == MarketDataType.Last) )
          {
          nqLastPrice = marketDataUpdate.Price;
          }


          if (Positions[0].MarketPosition != MarketPosition.Flat)
          {
          Print("Real ES pnl " + Positions[0].GetUnrealizedProfitLoss(PerformanceUnit.Currency, esLastPrice).ToString());
          Print("Real NQ pnl " + Positions[2].GetUnrealizedProfitLoss(PerformanceUnit.Currency, nqLastPrice).ToString());
          }

          Comment


            #6
            Hello bc24fl,

            I do not think this is a hacky approach, you are getting the last traded price for each instrument correctly in OnMarketData and supplying that correctly to GetUnRealizedPnL.

            You could consider doing something like the following, but is going to be up to preference and what you specifically need for your logic.

            Code:
            if (marketDataUpdate.MarketDataType == MarketDataType.Last) 
            {
                if (Positions[BarsInProgress].MarketPosition != MarketPosition.Flat)
                    Print(String.Format("{0} PnL: {1}", Instruments[BarsInProgress].FullName, Positions[BarsInProgress].GetUnrealizedProfitLoss(PerformanceUnit.Currency, marketDataUpdate.Price).ToString()));
            }
            Let me know if there is anything else I can do to help.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            43 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            20 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            30 views
            1 like
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            47 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            38 views
            0 likes
            Last Post CarlTrading  
            Working...
            X