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

Realised PnL

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

    Realised PnL

    To get unrealised PnL for a position I use...

    Position.GetProfitLoss(Close[0], PerformanceUnit.Points)

    But how do find realised the PnL of a closed position?


    Thanks in advance!

    #2
    Hello,
    Can you clarify if you are wanting to get the last trade's PNL?

    You could track the PNL by using GetProfitLoss() and storing the value to a variable such as currentTrade and once the close occurs to store the value into another variable such as previous trade. I have provided an example of this below.

    if(Position.MarketPosition != MarketPosition.Flat)
    currentTrade = Position.GetProfitLoss;
    if(Position.MarketPosition == MarketPosition.Flat)
    {
    previousTrade = currentTrade;
    currentTrader = 0;
    }
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      That's great! I think this is what I need, I'll try this out!

      Thanks for your help!

      Comment


        #4
        Originally posted by NinjaTrader_CodyB View Post
        Hello,
        Can you clarify if you are wanting to get the last trade's PNL?

        You could track the PNL by using GetProfitLoss() and storing the value to a variable such as currentTrade and once the close occurs to store the value into another variable such as previous trade. I have provided an example of this below.

        if(Position.MarketPosition != MarketPosition.Flat)
        currentTrade = Position.GetProfitLoss;
        if(Position.MarketPosition == MarketPosition.Flat)
        {
        previousTrade = currentTrade;
        currentTrader = 0;
        }


        Hi Cody
        Unfortunately, I cannot get your code snippet above to work.
        I have 'currentTrade' and 'previousTrade' printing in the output window.
        'currentTrade' works but 'previousTrade' is always a 0 value, even after exit.

        Any thoughts?

        Comment


          #5
          Hello,

          Can you provide the script that you are implementing this in so I can test this on my end?
          You can send me this by sending it to platformsupport[AT]ninjatrader[DOT]com with ATTN Cody: http://www.ninjatrader.com/support/f...ad.php?t=73670

          I look forward to your reply.
          Cody B.NinjaTrader Customer Service

          Comment


            #6
            Thanks Cody, I've sent you an email

            Regards
            John

            Comment


              #7
              Cody, did you have chance to look at the email I sent a few days ago?

              Comment


                #8
                Hello,
                We have not received an email with this forum thread referenced.
                Please resend the email to platformsupport[AT]ninjatrader[DOT]com with ATTN Cody: http://www.ninjatrader.com/support/f...ad.php?t=73670
                Cody B.NinjaTrader Customer Service

                Comment


                  #9
                  Yes I already sent the email to the address given, and I have copied it below...

                  I have pasted the code I am using below.
                  It is basically a copy of the code snippet you provided.
                  I am running several instruments in my script (I have the first 2 below).
                  What I need to calculate is the 'realised' and 'unrealised' PnL is at any time.

                  I have the value of 'unrealised' shows in the output window and is correct, but the 'realised' is always at 0 even after the position has closed out.

                  Any thoughts?

                  Print("Un-Realised = " + (UnRe0 + UnRe1 + UnRe2 + UnRe3));
                  Print("Realised = " + (Re0 + Re1 + Re2 + Re3 ));

                  #region Re/UnRe 0
                  if(Positions[0].MarketPosition != MarketPosition.Flat)
                  UnRe0 = Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Currency);
                  if(Positions[0].MarketPosition == MarketPosition.Flat)
                  {
                  Re0 = UnRe0;
                  UnRe0 = 0;
                  }
                  #endregion

                  #region Re/UnRe 1
                  if(Positions[1].MarketPosition != MarketPosition.Flat)
                  UnRe1 = Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Currency);
                  if(Positions[1].MarketPosition == MarketPosition.Flat)
                  {
                  Re1 = UnRe1;
                  UnRe1 = 0;
                  }
                  #endregion

                  Comment


                    #10
                    Hello,
                    I have tested your Snippet on my end and I am receiving values with it.
                    I do get 0's before a trade is made. Once a trade is made you will receive values.
                    Are trades being entered?
                    If you use the following print statement do you receive the values?
                    Print(Time[0] + " PreviousTradee" + Re0 + " " + "CurrentTrade" + UnRe0);
                    Cody B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi, unfortunately I cannot get this to work.
                      The print output of UnRe0 shows the unrealised PnL, but when the trade is closed out both Re0 and UnRe0 shows 0 values.

                      I've worked round this problem, and I'm now using:
                      Performance.AllTrades.TradesPerformance.Currency.C umProfit

                      Thanks

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by bmartz, 03-12-2024, 06:12 AM
                      4 responses
                      32 views
                      0 likes
                      Last Post bmartz
                      by bmartz
                       
                      Started by Aviram Y, Today, 05:29 AM
                      4 responses
                      12 views
                      0 likes
                      Last Post Aviram Y  
                      Started by algospoke, 04-17-2024, 06:40 PM
                      3 responses
                      28 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by gentlebenthebear, Today, 01:30 AM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by cls71, Today, 04:45 AM
                      1 response
                      7 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Working...
                      X