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

How do I get the realized PnL of a strategy programatically?

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

    How do I get the realized PnL of a strategy programatically?

    I have tried the sample
    found here: https://ninjatrader.com/support/help...collection.htm


    PHP Code:
    protected override void OnBarUpdate()
    {
      
    // Accesses the first/last trade in the strategy (oldest trade is at index 0)
      // and prints out the profit as a percentage to the output window
      
    if (SystemPerformance.AllTrades.Count 1)
      {
          
    Trade lastTrade SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count 1];
          
    Trade firstTrade SystemPerformance.AllTrades[0];

          Print(
    "The last trade profit is " lastTrade.ProfitPercent);
          Print(
    "The first trade profit is " firstTrade.ProfitPercent);
      }

    But it does not work.


    I do not get any print statements after the strategy makes a trade.

    I am trying to get the realized PnL that is displayed when you view the strategy under the Strategies tab.
    Attached Files
    Last edited by lakersfan; 06-21-2020, 10:10 PM.

    #2
    Hello lakersfan,

    Thanks for your post.

    You would use: SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit

    Reference: https://ninjatrader.com/support/help...?cumprofit.htm

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_PaulH View Post
      Hello lakersfan,

      Thanks for your post.

      You would use: SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit

      Reference: https://ninjatrader.com/support/help...?cumprofit.htm
      Great this worked! however I forgot to mention that I want to also get the realized PnL for the last trade, just like in the sample. Any idea why that is not working?

      Comment


        #4
        Hello lakersfan,

        Thanks for your reply.

        I've attached an example. Note that the trade profit here is in percent as the example specified.

        Click image for larger version

Name:	lakersfan-2.PNG
Views:	650
Size:	175.3 KB
ID:	1106138
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_PaulH View Post
          Hello lakersfan,

          Thanks for your reply.

          I've attached an example. Note that the trade profit here is in percent as the example specified.

          Click image for larger version

Name:	lakersfan-2.PNG
Views:	650
Size:	175.3 KB
ID:	1106138
          It does not work on my end. I have attached a try/catch block. (minus checking if count is greater than one to access directly)
          PHP Code:
          System.ArgumentOutOfRangeExceptionIndex was out of rangeMust be non-negative and less than the size of the collection.
          Parameter nameindex
             at System
          .ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argumentExceptionResource resource)
             
          at System.Collections.Generic.List`1.get_Item(Int32 index)
             at NinjaTrader.NinjaScript.Strategies.SampleStrategy2.OnBarUpdate() 
          Could it be that i'm on market replay? And if so, how can I make it work on market replay so that I can debug?

          I find it odd that it is not registering a count, when this code functions properly
          PHP Code:
          Print("TradesCount " +SystemPerformance.AllTrades.TradesCount); 

          Comment


            #6
            Hello lakersfan,

            Thanks for your reply.

            The code example I posted (which was a copy of what you posted, which was copied from the Help guide) works on Market replay without errors.

            I would suggest replicating the simple crossover code I've shown and add the code from your first post.

            Just to be clear, the strategy has to make the trades. I was assuming you were not showing your trade entry code in the first post.


            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_PaulH View Post
              Hello lakersfan,

              Thanks for your reply.

              The code example I posted (which was a copy of what you posted, which was copied from the Help guide) works on Market replay without errors.

              I would suggest replicating the simple crossover code I've shown and add the code from your first post.

              Just to be clear, the strategy has to make the trades. I was assuming you were not showing your trade entry code in the first post.

              Can you please provide the sample strategy that you used so I can see if my machine does the same?

              I have tried a few variations of my stategy, yet it does not work.

              To be clear, the strategy definitely knows there are trades taking place, but so far, with AllTrades.TradesCount, not Trades.Count

              Print("TradesCount " +SystemPerformance.AllTrades.TradesCount);

              This actually shows me the trades I am taking, as well as the cumprofit from your 1st response.

              Comment


                #8
                Hello lakersfan,

                Thanks for your reply.

                I have attached the strategy for your convenience.

                ExampleRealizedPNLpertrade.zip
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_PaulH View Post
                  Hello lakersfan,

                  Thanks for your reply.

                  I have attached the strategy for your convenience.

                  [ATTACH]n1106164[/ATTACH]
                  Thank you. The code is not working on my end. Please review the screenshot.

                  I did not touch the code in the strategy, I enabled once I imported.

                  Comment


                    #10
                    Hello lakersfan,

                    Thanks for your post.

                    Please write into PlatformSupport[at]Ninjatrader[dot]Com. Mark the e-mail Atten:Paul and Ticket# 2615788.

                    In the e-mail please add a link to this thread for reference.

                    In the e-mail please attach your log and trace files:

                    Open your NinjaTrader folder under, "Documents" (sometimes called, "My Documents")
                    Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
                    Send the 2 compressed folders as attachments to the email.
                    Once complete, you can delete those compressed folders.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_PaulH View Post
                      Hello lakersfan,

                      Thanks for your post.

                      Please write into PlatformSupport[at]Ninjatrader[dot]Com. Mark the e-mail Atten:Paul and Ticket# 2615788.

                      In the e-mail please add a link to this thread for reference.

                      In the e-mail please attach your log and trace files:

                      Open your NinjaTrader folder under, "Documents" (sometimes called, "My Documents")
                      Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
                      Send the 2 compressed folders as attachments to the email.
                      Once complete, you can delete those compressed folders.
                      One thing I noticed was that I do not have the green/red entry and exit lines. Could that shed light before I open a ticket?

                      Comment


                        #12
                        Hello lakersfan,

                        Thanks for your reply.

                        That would not relate to the print statements in any way.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          I did some research, and it seems like this might be a NinjaTrader issue: https://ninjatrader.com/support/foru...lltrades-count I followed, the steps and my NT still does not print the values. Has this problem been fixed, or the bug getting taken care of?

                          Comment


                            #14
                            Hello lakersfan,

                            I have attached a video demonstration showing Paul's strategy in action. I'm also noting the behavior seen when we change the IncludeTradeHistoryInBacktest property and test the strategy against the Control Center and from a chart (which is discussed in your linked thread.)

                            You may take the same steps as I have in the video to observe.

                            Demo - https://drive.google.com/file/d/1vs7...w?usp=drivesdk

                            We look forward to assisting.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Jim View Post
                              Hello lakersfan,

                              I have attached a video demonstration showing Paul's strategy in action. I'm also noting the behavior seen when we change the IncludeTradeHistoryInBacktest property and test the strategy against the Control Center and from a chart (which is discussed in your linked thread.)

                              You may take the same steps as I have in the video to observe.

                              Demo - https://drive.google.com/file/d/1vs7...w?usp=drivesdk

                              We look forward to assisting.
                              Yes, I followed the steps in that thread for the solution, but I was not able to produce the same results as you or Paul. I have opened a new ticket on going around this issue, but now I'm stuck https://ninjatrader.com/support/foru...ntry-order-was

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GLFX005, Today, 03:23 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post GLFX005
                              by GLFX005
                               
                              Started by XXtrader, Yesterday, 11:30 PM
                              2 responses
                              11 views
                              0 likes
                              Last Post XXtrader  
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              12 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Working...
                              X