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

AvgProfit == Average Net Profit?

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

    AvgProfit == Average Net Profit?

    Howdy--

    Simple question: Does .AvgProfit return net profit, including deducts for commission and slippage if "Apply commission to PnL calculations" is checked in the Tools > Options ? General tab, a single side commission value is included in the Tools > Options > Commissions "Futures - Simulator" section (i.e., $2.50, which would be $5.00 roundtrip) and a Slippage value is included in Initialize() (i.e., "Slippage = 2;")?

    Here is the NT .AvgProfit documentation:

    AvgProfit
    Definition
    Returns the average profit of the collection.

    Property Value
    A double value that represents the average profit of the collection.

    Syntax
    <TradeCollection>.TradesPerformance.<TradesPerform anceValues>.AvgProfit


    Examples
    protected override void OnBarUpdate()
    {
    // Print out the average profit of all trades in currency
    Print("Average profit of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.A vgProfit);
    }

    If in fact .AvgProfit is average net profit, I might suggest that the NT documentation is amended to reflect that this is a net number if commission and/or slippage is included (per above?).

    Thanks for your help,

    Aventeren

    #2
    Hello aventeren,

    Thanks for your post.

    The .AvgProfit does not include commission. You can access the commissions with:
    Performance.AllTrades.TradesPerformance.Commission .
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello aventeren,

      Thanks for your post.

      The .AvgProfit does not include commission. You can access the commissions with:
      Performance.AllTrades.TradesPerformance.Commission .
      Thanks for the clarification.

      So to return net profit for all trades, I would use something like the following?

      Code:
      netProfit = Performance.AllTrades.TradesPerformance.GrossProfit - Performance.AllTrades.TradesPerformance.Commission;

      Comment


        #4
        Hi aventeren,
        Originally posted by aventeren View Post
        Thanks for the clarification.

        So to return net profit for all trades, I would use something like the following?

        Code:
        netProfit = Performance.AllTrades.TradesPerformance.GrossProfit - Performance.AllTrades.TradesPerformance.Commission;
        For the net profit I think you would also need to subtract any loss.
        Code:
        netProfit = Performance.AllTrades.TradesPerformance.GrossProfit - Performance.AllTrades.TradesPerformance.GrossLoss - Performance.AllTrades.TradesPerformance.Commission;
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hi aventeren,


          For the net profit I think you would also need to subtract any loss.
          Code:
          netProfit = Performance.AllTrades.TradesPerformance.GrossProfit - Performance.AllTrades.TradesPerformance.GrossLoss - Performance.AllTrades.TradesPerformance.Commission;
          Thanks, Chelsea.

          Could you please confirm that .GrossProfit only includes "winning" trades and .GrossLoss only includes "losing" trades?

          Also, if we have defined slippage in Initialize(), how can we make sure that slippage has been properly deducted--and more importantly, where was it deducted (if at all)?

          Thanks!

          Aventeren

          Comment


            #6
            Hello,

            Yes, the gross profit will only include winning trades and the gross loss will only include losing trades.

            I am not quite sure what you mean by defining the slippage. What code are you using to set this? How is this code being used in the script?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello,

              Yes, the gross profit will only include winning trades and the gross loss will only include losing trades.

              I am not quite sure what you mean by defining the slippage. What code are you using to set this? How is this code being used in the script?
              Thanks for the clarification on .GrossProfit and .GrossLoss.

              In the NinjaScript Language Reference > Strategy > Slippage, it shows an example where Slippage can be defined within Initialize() like this:

              Code:
              protected override void Initialize()
              { 
                  Slippage = 2; 
              }
              But where is the defined Slippage then applied (i.e. what TradePerformance metric)?

              Thanks,

              Aventeren

              Comment


                #8
                Hi aventeren,

                I understand now.

                The slippage works with historical trades and does not work with real time trades.

                This affects the fill type and where the trade exits. Basically it will add 2 ticks of loss to the exit price.

                The default fill type code comments mention this will only affect market orders and stop market orders but will not affect limit orders.

                Also, slippage can only be applied if the bar the order is filled on could have accommodated slippage.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hi aventeren,

                  I understand now.

                  The slippage works with historical trades and does not work with real time trades.

                  This affects the fill type and where the trade exits. Basically it will add 2 ticks of loss to the exit price.

                  The default fill type code comments mention this will only affect market orders and stop market orders but will not affect limit orders.

                  Also, slippage can only be applied if the bar the order is filled on could have accommodated slippage.
                  Thanks, Chelsea.

                  As you might suspect, I'm asking these questions to understand how and where NT7 accounts for commission and slippage with the Strategy Analyzer's Optimization and Walk Forward Optimization.

                  Given the above, if I was to include $2.50 as the defined Commission under the Tools > Options > Commission > Futures - Simulator, check the "Apply commission to PnL calculations"box in the Tools > Options > General tab and then include 2 under the "Historical Fill Processing" within the Strategy Analyzer, will commission and slippage then be included in the backtested, optimized or walk forward optimized results?

                  Also, under the above assumption, would the .AvgProfit, .GrossProfit and .GrossLoss then include commission and slippage?

                  As you can tell, I am trying to get to a net profit number to back test on--rather than a gross profit number.

                  Thanks,

                  Aventeren
                  Last edited by aventeren; 02-20-2014, 03:32 PM.

                  Comment


                    #10
                    Hi aventeren,

                    As long as Include commission is true in the backtest parameters, the slippage will be included with AvgPrice and GrossProfit.

                    The commission will not be included with the AvgPrice or GrossProfit. You'll need to add the commission with TradesPerformance.Commission.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hi aventeren,

                      As long as Include commission is true in the backtest parameters, the slippage will be included with AvgPrice and GrossProfit.

                      The commission will not be included with the AvgPrice or GrossProfit. You'll need to add the commission with TradesPerformance.Commission.
                      Okay, great--I think we're getting closer.

                      Can you please confirm that my understanding is correct:

                      1. For a Strategy Analyzer run to include slippage within the resulting .GrossProfit and .GrossLoss values, one needs to enter a Slippage value in the "Historical Fill Processing" line and make sure that "Include Commissions" is set to true in the dialog box prior to running the backtest, optimization or walk forward optimization?

                      2. NT7 will not output a net profit figure, therefore you have to manually calculate net profit by .GrossProfit + .GrossLoss - .Commission?

                      3. From my understanding, entering a value of 2 for Slippage in the dialog box is a single side slippage value that reduces the .GrossProfit or .GrossLoss by 2 ticks/side * 2 sides = 4 ticks. However, how does NT7 handle this value of 2? For instance, if I wanted 3 round trip ticks to of assumed slippage, does NT7 wait to apply the slippage until the .GrossProfit and .GrossLoss values are calculated, or would it instead try and apply 1.5 ticks to each side, which is impossible due to rounding errors. How does NT7 handle fractional slippage values--and furthermore, how does one insure that an odd tick number of slippage is included?

                      Thanks,

                      Aventeren

                      Comment


                        #12
                        MaxNetProfit Optimize Type - No Commission?

                        Also, to confirm, the @MaxNetProfit.cs optimizer type (which can be selected in the "Optimize on..." line in the Strategy Analyzer dialog box) does not include commission?

                        Based on the code located in NinjaTrader 7 > bin > Custom > Type (below), it looks like slippage would be included (assuming that one entered a Slippage value and selected True for "Include Commission"), but that commission would not--in which case your definition of "net profit" is only net of slippage and not commission (which would probably be a more accurate representative of "net profit"). Correct?

                        Code:
                        // 
                        // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
                        //
                        #region Using declarations
                        using System;
                        using System.ComponentModel;
                        using System.Drawing;
                        using NinjaTrader.Cbi;
                        using NinjaTrader.Data;
                        using NinjaTrader.Indicator;
                        using NinjaTrader.Strategy;
                        #endregion
                        
                        // This namespace holds all strategies and is required. Do not change it.
                        namespace NinjaTrader.Strategy
                        {
                        	/// <summary>
                        	/// </summary>
                        	[Gui.Design.DisplayName("max. net profit")]
                        	public class MaxNetProfit : OptimizationType
                        	{
                        		/// <summary>
                        		/// Return the performance value of a backtesting result.
                        		/// </summary>
                        		/// <param name="systemPerformance"></param>
                        		/// <returns></returns>
                        		public override double GetPerformanceValue(SystemPerformance systemPerformance)
                        		{
                        			return systemPerformance.AllTrades.TradesPerformance.GrossProfit + systemPerformance.AllTrades.TradesPerformance.GrossLoss;
                        		}
                        	}
                        }

                        Comment


                          #13
                          Hi aventeren,

                          To be honest, I wasn't sure about the commissions with gross profit / net profit and have been asking our development department what the expected behavior is and they have said to me that the gross profit / loss does not include commissions and this is what I have reported back to you.

                          After your further questions I decided to do a test to get definitive proof of the behavior and have found that this was incorrect.

                          Gross profit and loss does actually include commisssions in both the code and Strategy Analyzer results.

                          Attached are my test results and the strategy I used to test this. I tested this over ES 03-14 60 Minute from 2/18/2014 to 2/18/2014.

                          The results from output window with Include Comission set to True:
                          AvgProfit: 437.5
                          Gross Profit: 437
                          Gross Loss: 0
                          Total Commission: 0.5

                          Results with Include Commission set to False:
                          AvgProfit: 437.5
                          Gross Profit: 437.5
                          Gross Loss: 0
                          Total Commission: 0

                          So I was wrong about this. Commissions are included.
                          Attached Files
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hi aventeren,

                            To be honest, I wasn't sure about the commissions with gross profit / net profit and have been asking our development department what the expected behavior is and they have said to me that the gross profit / loss does not include commissions and this is what I have reported back to you.

                            After your further questions I decided to do a test to get definitive proof of the behavior and have found that this was incorrect.

                            Gross profit and loss does actually include commisssions in both the code and Strategy Analyzer results.

                            Attached are my test results and the strategy I used to test this. I tested this over ES 03-14 60 Minute from 2/18/2014 to 2/18/2014.

                            The results from output window with Include Comission set to True:
                            AvgProfit: 437.5
                            Gross Profit: 437
                            Gross Loss: 0
                            Total Commission: 0.5

                            Results with Include Commission set to False:
                            AvgProfit: 437.5
                            Gross Profit: 437.5
                            Gross Loss: 0
                            Total Commission: 0

                            So I was wrong about this. Commissions are included.
                            Chelsea--

                            I can't tell you how much I appreciate your follow through on my questions. I'm impressed. Thanks. No exclamation points. Just simply thank you.

                            I'm currently working to make sure that I clearly understand the back test engine before back testing, as if I don't understand the engine, then how can I possibly understand and have confidence in the results. So your answers to my questions are helping me to reach that level of confidence that I'm seeking.

                            At an absolute minimum, I would STRONGLY suggest that you revise the .AvgProfit, .GrossProfit and .GrossLoss documentation sections to reflect the concepts in our discussion--at least an update for the NT8 documentation. In any event, that is what it is.

                            So, can you please confirm the following:

                            1. For a Strategy Analyzer run to include commission and slippage within the resulting .AvgProfit, .GrossProfit and .GrossLoss values, the following must be done:
                            a: Include a commission value defined under the Tools > Options > Commission > Futures - Simulator parameter,

                            b: Check the "Apply commission to PnL calculations"box in the Tools > Options > General

                            c: Set "Include Commissions" to true in the Strategy Analyzer dialog box, and

                            d: Enter a Slippage value in the "Historical Fill Processing" line in the Strategy Analyzer dialog box.

                            I still don't have a firm handle on how NT deals with a non integer Slippage value in the Strategy Analyzer dialog box. The NT7 documentation page at Operations > Strategy Analyzer > Understanding backtest options > Slippage states that the value entered "Sets the slippage amount in ticks per execution". However, how does the back end code actually work? For instance, how would the NT Strategy Analyzer code deal with the following Slippage values:

                            1. Slippage = 1.01
                            2. Slippage = 1.49
                            3. Slippage = 1.50
                            4. Slippage = 1.51
                            5. Slippage = 1.99

                            How does the back test engine round Slippage to generate a result that has been rounded to TickSize? Is the engine simply rounding (i.e., 1.01 to 1.49 becomes 1, 1.51 to 1.99 becomes 2) or is the rounding always down (i.e., 1.01 to 1.99 becomes 1).

                            Also, how does the code process these double values? Is Slippage first applied to the entry and then the exit or just once at the exit by multiplying Slippage by 2?

                            Finally, if one was to set a Slippage value within the strategy's Initialize() method (as I showed before--let's assume a value of 2 here) but have a different value for Slippage in the Strategy Analyzer dialog box (let's assume a value of 1 here), which Slippage value is used by the engine code (i.e., 2 or 1).

                            My hope is that others will come across this post and come away with a better understanding of the NT7 Strategy Analyzer results.

                            Thanks for your help,

                            Aventeren

                            Comment


                              #15
                              Hi aventeren,

                              At an absolute minimum, I would STRONGLY suggest that you revise the .AvgProfit, .GrossProfit and .GrossLoss documentation sections to reflect the concepts in our discussion--at least an update for the NT8 documentation. In any event, that is what it is.
                              I'll send a message to our development, first letting them know of my test results, and to request to have the documentation edited to inform about the commissions.

                              a: Include a commission value defined under the Tools > Options > Commission > Futures - Simulator parameter,

                              b: Check the "Apply commission to PnL calculations"box in the Tools > Options > General

                              c: Set "Include Commissions" to true in the Strategy Analyzer dialog box, and

                              d: Enter a Slippage value in the "Historical Fill Processing" line in the Strategy Analyzer dialog box.
                              Yes, you do have the steps correct to include commission and slippage with backtest results.

                              I still don't have a firm handle on how NT deals with a non integer Slippage value in the Strategy Analyzer dialog box. The NT7 documentation page at Operations > Strategy Analyzer > Understanding backtest options > Slippage states that the value entered "Sets the slippage amount in ticks per execution". However, how does the back end code actually work? For instance, how would the NT Strategy Analyzer code deal with the following Slippage values:

                              1. Slippage = 1.01
                              2. Slippage = 1.49
                              3. Slippage = 1.50
                              4. Slippage = 1.51
                              5. Slippage = 1.99

                              How does the back test engine round Slippage to generate a result that has been rounded to TickSize? Is the engine simply rounding (i.e., 1.01 to 1.49 becomes 1, 1.51 to 1.99 becomes 2) or is the rounding always down (i.e., 1.01 to 1.99 becomes 1).

                              Also, how does the code process these double values? Is Slippage first applied to the entry and then the exit or just once at the exit by multiplying Slippage by 2?

                              Finally, if one was to set a Slippage value within the strategy's Initialize() method (as I showed before--let's assume a value of 2 here) but have a different value for Slippage in the Strategy Analyzer dialog box (let's assume a value of 1 here), which Slippage value is used by the engine code (i.e., 2 or 1).
                              The slippage (whole number in ticks) will be multiplied by the ticksize from the master instrument settings.

                              If the ticksize is .25 and slippage is 2, price slippage will be .50.
                              This needs to be a whole number as it is defining a number of ticks.

                              However, I am seeing that the number is not an integer, it is actually a double.
                              I checked without our development department about this and they said they would change the documentation but won't be changing the variable type in NinjaTrader.

                              I still highly recommend you keep this a whole number. I think you would get arbitrary values that do not relate to an actual tick value if you use a decimal.

                              Regarding setting the property in the code vs the strategy analyzer, the strategy analyzer will overwrite this. The property in the code will set the default that the strategy analyzer will be set to for this. So if you set it to 3 in the code it will be 3 by default when you run in the strategy analyzer.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Taddypole, 04-26-2024, 02:47 PM
                              5 responses
                              35 views
                              0 likes
                              Last Post eDanny
                              by eDanny
                               
                              Started by kujista, 04-23-2024, 06:23 AM
                              6 responses
                              48 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by giulyko00, 04-24-2024, 12:03 PM
                              7 responses
                              36 views
                              0 likes
                              Last Post eDanny
                              by eDanny
                               
                              Started by NM_eFe, Today, 10:13 AM
                              0 responses
                              12 views
                              0 likes
                              Last Post NM_eFe
                              by NM_eFe
                               
                              Started by hdge4u, Yesterday, 12:23 PM
                              1 response
                              11 views
                              0 likes
                              Last Post hdge4u
                              by hdge4u
                               
                              Working...
                              X