Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to manually calculate the MAE and MFE values?

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

    How to manually calculate the MAE and MFE values?

    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);
    }
    These values especially seem off when the trade has only a duration of a few bars. Trying to correct for such trades using various 'if' statements hasn't helped. Could someone give me an suggestion as how to calculate the MAE and MFE of an individual trade?

    Regards,
    Last edited by J_o_s; 02-16-2011, 08:52 AM. Reason: Problem solved

    #2
    Hello,

    Thanks for your forum post.

    Looks pretty good for the mot part. If it was me trying to figure this out I would need to take one trade and focus on the math and values of this one trade.

    So if you would please, Add in Print() statements and Print out every variable such as the low found, the high found etc etc and calculated the MFE MAE manually/ see where the values are off.

    We need to identify the variable that is off here to find out what needs to be fixed first and foremost.

    Let me know if I can be of further assistance.
    BrettNinjaTrader Product Management

    Comment


      #3
      Originally posted by NinjaTrader_Brett View Post
      Hello,

      Thanks for your forum post.

      Looks pretty good for the mot part. If it was me trying to figure this out I would need to take one trade and focus on the math and values of this one trade.

      So if you would please, Add in Print() statements and Print out every variable such as the low found, the high found etc etc and calculated the MFE MAE manually/ see where the values are off.

      We need to identify the variable that is off here to find out what needs to be fixed first and foremost.

      Let me know if I can be of further assistance.
      Thanks for your reply Bret.

      After some trail and error with Print() I've uncovered the error. Because NinjaTrader exits on the open of the ExitBar, the high and low of the ExitBar shouldn't be included in the manual calculation of the MAE and MFE values. Well, obvious in hindsight, but I didn't correct for it. That was also why some values were off, and the rest of the values where just fine. Luckily, the great guys of BMT forum pointed me in the right direction, and with some "debugging" with Print() did I discover the error.

      I've attached the commented code in case someone else could use it,

      Regards,
      Attached Files

      Comment


        #4
        Hello,

        Excellent! Thanks for posting and sharing!
        BrettNinjaTrader Product Management

        Comment


          #5
          Thanks very much!!!!
          Only one remind:
          foreach()
          {}
          must be in protected override void OnTermination()

          Comment


            #6
            Hello all,

            I have tested this script (calculateMAEandMFE.cs) and found it to be giving the incorrect values (at least when compared to Performance.AllTrades.TradesPerformance.Currency.A vgMae).

            What is missing is the point size multiplication when calculating the mae and mfe.

            Also, this script does not take into consideration intrabar buys and sells. This means it is possible to include low and high values that were hit just before or just after the trade took place.

            I have created and attached a sample strategy that addresses these issues.
            Attached Files
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, Yesterday, 10:06 AM
            0 responses
            18 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            17 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            14 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            9 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            38 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X