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

MAE/MFE moving average

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

    MAE/MFE moving average

    Hello,

    I currently have a strategy that utilizes MAE of the total trade average to set a stop. "SystemPerformance.AllTrades.TradesPerformance.Cur rency.AverageMae"

    I would like to create a stop that utilizes a moving average of the individual trade MAE (for example, the last 30 trades) rather than the alltrades average. Can someone point me in the right direction on how I would code this?

    Thanks,
    eleven

    #2
    Hello eleven,

    Unfortunately, the TradesPerformance does not have this value for individual trades.


    This means you would need to custom calculate this with code.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Here is the code that I came up with. I think it works.

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class TestMAEAvg : Strategy
      {
      private int i;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Period = 4;
      }
      }
      protected override void OnBarUpdate() ////////////////////////////////////////////////////////////////////////////////////
      {
      double itCalc = 0;
      double avgMAE = 0;

      if (SystemPerformance.AllTrades.Count > (Period - 1))
      {

      for (i = 1; i <= Period; i++)
      {
      itCalc = itCalc + lastTrade.MaeCurrency;
      }
      }
      avgMAE = itCalc / Period;

      } //OnBarUpdate

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(ResourceType = typeof(Custom.Resource), Name="Period", Order=1, GroupName="Strategy risk")]
      public int Period
      { get; set; }

      #endregion
      }
      }

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello eleven,

        Unfortunately, the TradesPerformance does not have this value for individual trades.


        This means you would need to custom calculate this with code.
        https://ninjatrader.com/support/help...dingAverageMae
        Hey Chelsea. Then what do MaeTicks and MfeTicks do in System Performance? Found them in the Help Guide.
        Thanks.

        Comment


          #5
          Hello Trader17,

          Was looking at the wrong page of the documentation. They are listed there.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello eleven,

            Unfortunately, the TradesPerformance does not have this value for individual trades.


            This means you would need to custom calculate this with code.
            https://ninjatrader.com/support/help...dingAverageMae
            Hey Chelsea. When you said TradesPerformance does not have it for individual trades then how does it show up for each trade in the Strategy Report? I thought MaeTicks and MfeTicks are values for each completed trade and not the average.

            Comment


              #7
              Hello Trader17,

              I was incorrect. This is available for trade objects.
              Hello, I currently have a strategy that utilizes MAE of the total trade average to set a stop. &quot;SystemPerformance.AllTrades.TradesPerformance.Currency.AverageMae&quot; I would like to create a stop that utilizes a moving average of the individual trade MAE (for example, the last 30 trades) rather than the alltrades
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello Trader17,

                I was incorrect. This is available for trade objects.
                https://ninjatrader.com/support/foru...29#post1189929
                Thanks Chelsea. Now will MaeTicks and MfeTicks only work in System Performance Real Time Trades or will also work with System Performance All Trades to see these values printed on a historical chart too?
                Thanks.

                Comment


                  #9
                  Hello Trader17,

                  SystemPerformance.AllTrades will include both historical and real-time trades.
                  https://ninjatrader.com/support/help...timetrades.htm

                  SystemPerformance.RealTimeTrades will include only real-time trades.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Chelsea. So MaeTicks/MfeTicks will work on historical chart data too then. Was not too sure so asked. Looking at the example in the Guide I used Mae/MfeTicks and got error saying name does not exist.

                    protected override void OnBarUpdate()
                    {
                    if (SystemPerformance.RealTimeTrades.Count > 0)
                    {
                    Trade lastTrade = SystemPerformance.RealTimeTrad es[SystemPerformance.RealTimeTrades.Count - 1];


                    double lastMAE = lastTrade.MaeTicks;

                    double lastMFE = lastTrade.MfeTicks;
                    }

                    if(Close[0]>Open[0])
                    {
                    EnterLong();
                    Draw.Text(this, "MAE"+CurrentBar, "MAE="+ lastMAE.ToString(), 0, Low[0]-15 * TickSize);
                    Draw.Text(this, "MFE"+CurrentBar, "MFE="+ lastMFE.ToString(), 0, Low[0]-20 * TickSize);
                    }
                    }

                    Comment


                      #11
                      Hello Trader17,

                      What is the full error message?
                      What is the line of code indicated in the error?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post
                        Hello Trader17,

                        What is the full error message?
                        What is the line of code indicated in the error?
                        Thanks Chelsea. Says lastMAE and lastMFE not in context.

                        Comment


                          #13
                          Trader17,

                          It looks like you are declaring double lastMAE in an if statement. It can only be used in that if statements action block and not outside of it.

                          Perhaps declare these above the if statement?

                          If these are not intended to persist, maybe don't use those variables at all?

                          Code:
                          if( Close[0] > Open[0])
                          {
                              EnterLong();
                          
                              if (SystemPerformance.RealTimeTrades.Count > 0)
                              {
                                  Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];
                          
                                  Draw.Text(this, "MAE"+CurrentBar, "MAE="+ lastTrade.MaeTicks.ToString(), 0, Low[0] - 15 * TickSize);
                                  Draw.Text(this, "MFE"+CurrentBar, "MFE="+ lastTrade.MfeTicks.ToString(), 0, Low[0] - 20 * TickSize);
                              }
                          }
                          Also, note, the SystemPerformance collection is not updated until the position updates with OnPositionUpdate().
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Thanks Chelsea. It was a typo. I had made them private. It is working fine with just one issue. When I print values under a bar I see a lag. The values are not from the latest closed trade but a closed trade just before it. Why is there a lag in NT8 when we are updating the values on each bar close? I print them during opening a new trade as I do not want the values printed under every bar. And there are quite a few bars between a closed trade and a new execution which makes me think is enough time for updated values. In real time I see it updating on the bar after the entry bar. Is there a way to make it work as intended in NT8? I want to see the MAE and MFE values from the last closed trade preferably on the bar that closed the trade or on the next bar that a new trade is opened.
                            Congratulations on your 10 year anniversary with NinjaTrader Chelsea!!

                            Comment


                              #15
                              Hello Trader17,

                              Trades are formed when we have an entry execution and an exit execution. If the trade you are referencing is:

                              Code:
                              Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];
                              and the position is open, we would not have a new Trade, because that position has not yet exited. So you would not be able to get Trade details when a position is open.

                              As Chelsea mentions, if you check the last Trade in OnPositionUpdate, you could see the details from the last completed as soon as that trade is completed.
                              JimNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by inanazsocial, Today, 01:15 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by rocketman7, Today, 02:12 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post rocketman7  
                              Started by dustydbayer, Today, 01:59 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post dustydbayer  
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              5 responses
                              23 views
                              0 likes
                              Last Post trilliantrader  
                              Started by Davidtowleii, Today, 12:15 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Davidtowleii  
                              Working...
                              X