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

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
    JimNinjaTrader Customer Service

    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.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by geddyisodin, Today, 05:20 AM
            2 responses
            16 views
            0 likes
            Last Post geddyisodin  
            Started by hazylizard, Today, 08:38 AM
            0 responses
            6 views
            0 likes
            Last Post hazylizard  
            Started by Max238, Today, 01:28 AM
            5 responses
            42 views
            0 likes
            Last Post Max238
            by Max238
             
            Started by giulyko00, Yesterday, 12:03 PM
            3 responses
            13 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by habeebft, Today, 07:27 AM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_ChristopherS  
            Working...
            X