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 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.

    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!

        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 cls71, Today, 04:45 AM
            2 responses
            9 views
            0 likes
            Last Post eDanny
            by eDanny
             
            Started by proptrade13, Today, 11:06 AM
            0 responses
            2 views
            0 likes
            Last Post proptrade13  
            Started by kulwinder73, Today, 10:31 AM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by RookieTrader, Today, 09:37 AM
            3 responses
            15 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by terofs, Yesterday, 04:18 PM
            1 response
            24 views
            0 likes
            Last Post terofs
            by terofs
             
            Working...
            X