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 to access the MFE and MAE for an individual trade?

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

    How to access the MFE and MAE for an individual trade?

    Hi All,


    Does anybody know how to access the MFE and MAE encountered by a single trade from within NinjaScript?

    The context of my question is for use in a custom optimization type, where my override of the GetPerformanceValue method needs to access the MAE (Maximum Adverse Excursion) and MFE (Maximum Favourable Excursion) values for each individual trade.

    The TradesPerformance interface permits access to AvgMfe and AvgMaE values for TradeCollection objects (through either .Points or .Percent or .Currency, e.g. systemPerformance.AllTrades.TradesPerformance.Perc ent.AvgMfe). However, no such interface seems to exist for Trade objects (e.g. systemPerformance.AllTrades[0])

    So, we seem to have the situation where I can access an average of MFE and MAEs but not the individual values themselves! Obviously, NinjaTrader itself can access these values, as it can chart them in StrategyAnalyzer, and give the value for each if you roll the mouse over any individual datapoint.

    Is there a method I have overlooked or an undocumented access that anybody is aware of?

    I understand this may be outside the scope of support NinjaTrader folks can offer, but all help is greatly appreciated!


    Kind Regards,

    Dan

    #2
    Hi JustCallMeDan,

    Correct, you would need to calculate these explicitly from your code.
    TimNinjaTrader Customer Service

    Comment


      #3
      I think you can get to these values via this code:

      Code:
      double mae = systemPerformance.AllTrades.TradesPerformance.Currency.AvgMae;
      double mfe = systemPerformance.AllTrades.TradesPerformance.Currency.AvgMfe;
      JD

      Comment


        #4
        Originally posted by jdfagan View Post
        I think you can get to these values via this code:

        Code:
        double mae = systemPerformance.AllTrades.TradesPerformance.Currency.AvgMae;
        double mfe = systemPerformance.AllTrades.TradesPerformance.Currency.AvgMfe;
        JD
        JD, these values represent the arithmetic mean (simple average) of MAE and MFE values for all trades. Because I required more detailed information regarding the distribution of these values, I had to calculate and store these values within the strategy along with each trade (the key there is to update your extra per-trade statistics on an increase in the size of the AllTrades trade collection to ensure you remain in sync with trades NinjaTrader has identified as complete, rather than all trade signals, which can result in values recorded for failed orders and incomplete trades etc.

        Naturally, for average MFE and average MAE only, that code is sufficient.


        Dan

        Comment


          #5
          Originally posted by jdfagan View Post
          I think you can get to these values via this code:

          Code:
          double mae = systemPerformance.AllTrades.TradesPerformance.Currency.AvgMae;
          double mfe = systemPerformance.AllTrades.TradesPerformance.Currency.AvgMfe;
          JD
          How can these values of an individual trade be accessed? I've experienced some success with the following code, though it is in de minority of the cases (5-10%) way off, compared with the StrategyAnalyzer output:

          Code:
          foreach (Trade t in Performance.AllTrades)
          {
              if (t.Entry.MarketPosition == MarketPosition.Long)
              {
                  bestPriceReached = MAX(High, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)];
                  worstPriceReached = MIN(Low, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)];
              }
              else if (t.Entry.MarketPosition == MarketPosition.Short)
              {
                  bestPriceReached = MIN(Low, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)];
                  worstPriceReached = MAX(High, ((t.ExitExecution.BarIndex + 1) - t.EntryExecution.BarIndex))[(Count - 1) - (t.ExitExecution.BarIndex - 1)]; ;
              }
              maeTrade = Math.Abs(worstPriceReached - t.Entry.Price);
              mfeTrade = Math.Abs(bestPriceReached - t.Entry.Price);
              etdTrade = Math.Abs(mfeTrade - t.ProfitCurrency);
          }
          Could someone give me an suggestion as how to calculate the MAE and MFE of an individual trade?

          Regards,

          Comment


            #6
            Hi J_o_s,

            There is no direct way to calculate the statistic on the last trade, you'll need to calculate them manually in your code by grabbing the entry/exit values and price data between them.

            Find more information on the formulas used at: http://www.ninjatrader.com/support/h...efinitions.htm
            TimNinjaTrader Customer Service

            Comment


              #7
              Hi,

              I came across this thread as I was searching for a formula for how MAE is calculated. And what I find peculiar about this thread is J_o_s is looking for the MAE for individual threads.

              If you run your strategy through the strategy analyzer, and then click on the "Trades" tab, you can find the MAE, MFE and ETD of each individual thread. Or am I mistaken?

              Regards

              Kay Wai

              Comment


                #8
                Hi Kay,


                It's true that you can access the MAE, MFE and ETD for an individual trade via the "Trades" tab. However, this appears to be calculated by NinjaTrader on a post-hoc basis, i.e. when you click on the tab rather than when the trades are performed in the original backtest. Also, there appears to be no way to access this within NinjaScript, post-hoc or otherwise.

                If you want your strategy logic to use these values, rather than just view them from the NinjaTrader Trades tab after your simulation, you are limited to calculating them yourself in NinjaScript.



                Kind Regards,

                Dan

                Comment


                  #9
                  Blast from the past!

                  Thanks for this thread. The meaning is very clear, but... it is a pity, of course! Of course again, NT calculates those values to come to the average and to output them into the performance grid, but then...

                  ...why not to expose them also as properties of some class/collection??? [I mean: single trade MAE and MFE, why not also ETD!].

                  I strongly vote for the insertion of this feature in the next developments, ... please! It shouldn't be a lot of work, because the code is already there...!

                  In the meanwhile... going on "hard coding" custom methods... (i.e. repeating the work and eating resources).

                  Bye

                  fsprea
                  Last edited by fsprea; 05-07-2012, 04:28 PM. Reason: typo

                  Comment


                    #10
                    Is it correct, that also 9 years after the post above it is not possible to access MAE,MFE and ETD per trade within the script? Why?

                    Comment


                      #11
                      Hello Beauregard,

                      MFE and MAE can be read from a Trade object in NinjaTrader 8. ETD can be calculated by subtracting the PnL of the Trade from the MFE of the Trade. These additional Trade Performance statistics are not available in Trade objects in NinjaTrader 7.

                      Trade object — https://ninjatrader.com/support/help...n-us/trade.htm

                      Understanding ETD (How it can be calculated) — https://ninjatrader.com/support/help...dingAverageEtd

                      Please let us know if you have any additional questions.
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Hi

                        I've tried to access MFE and MAE using the example from https://ninjatrader.com/support/help...n-us/trade.htm
                        However, I get the following error:

                        An object reference is required for the non-static field method or property 'NinjaTrader.Cbi.SystemPerformance.RealTimeTrades. get'

                        Am I missing something or is not possible to use the SystemPerformance class within an indicator?

                        Comment


                          #13
                          Originally posted by FatCanary View Post
                          ... or is not possible to use the SystemPerformance class within an indicator?
                          Correct, that is for strategies, not indicators.

                          Indicators don't take trades and therefore don't have trade performance.

                          Comment


                            #14
                            As I suspected.

                            Many thanks for the clarification.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by jclose, Today, 09:37 PM
                            0 responses
                            4 views
                            0 likes
                            Last Post jclose
                            by jclose
                             
                            Started by WeyldFalcon, 08-07-2020, 06:13 AM
                            10 responses
                            1,413 views
                            0 likes
                            Last Post Traderontheroad  
                            Started by firefoxforum12, Today, 08:53 PM
                            0 responses
                            9 views
                            0 likes
                            Last Post firefoxforum12  
                            Started by stafe, Today, 08:34 PM
                            0 responses
                            10 views
                            0 likes
                            Last Post stafe
                            by stafe
                             
                            Started by sastrades, 01-31-2024, 10:19 PM
                            11 responses
                            169 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Working...
                            X