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 Mindset, 04-21-2026, 06:46 AM
            0 responses
            87 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            132 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            68 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            118 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            67 views
            0 likes
            Last Post PaulMohn  
            Working...
            X