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

Change MACD Moving Average Type

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

    Change MACD Moving Average Type

    I'm having difficultly changing the EMAs that make up the NT8 MACD to HMAs (Hull moving average). In other words, I'm trying to program a MACD that runs on HMAs instead of the standard EMAs.

    I've read the threads on this forum about this subject, and they involve changing the EMAs to SMAs. The HMA code is not as simple as the SMA code and I'm having trouble figuring it out based on those instructions.

    So any advice on how to change the MACD EMAs to HMAs would be greatly appreciated.

    As always, Thank you!

    #2
    Hello Tagliareni,

    Thanks for opening the thread.

    Giving advise in this field starts to enter the field of Trading Advise where we would not be qualified to offer advise for a mathematical study. I may be able to provide some direction for you based on our indicators source code, however.

    Before going further, I recommend examining NinjaTrader's MACD, EMA and HMA indicators.

    NinjaTrader's MACD indicator consists of a fast EMA and a slow EMA. You can observe that the EMA logic is baked into the MACD indicator and that the MACD's constant1 and constant2 are the equivalent to the EMA's constant1 and constant 2 for the fast EMA. The MACD's constant3 and constant4 are the equivalent to the constant1 and constant2 for the slow EMA. Finally constant5, constant6, and Smooth represent the constants and the period for the macdAvg variable (calculation is based on an EMA.)

    Thus, you may consider changing the logic for fastEma0 and slowEma0 to calculate an HMA based on Input (which is used to drive the EMA's in the MACD) and the Period for the Fast and Slow moving averages.

    Since macdAvg is an EMA calculation using the resulting "macd" variable based off of the difference between the two moving average values, this could be replaced with an HMA calculation using Smooth for the period.

    Please also be aware the indicators will require a Series<double> for input. If you are to replace the EMA calculations with HMA calulations, you may need to first create a Series<double> to hold any intermediary variables in a Series so the indicator can use them.

    Publicly available documentation on our indicators can be referenced below.

    Valid Input for System Indicators - https://ninjatrader.com/support/help..._indicator.htm
    HMA - https://ninjatrader.com/support/help...-_hull_hma.htm
    PriceSeries Input - https://ninjatrader.com/support/help...-us/?input.htm

    I'll leave this thread open ended in case another community member can provide more advise or an already working indicator.

    We can also have a representative of our EcoSystem reach out with additional information on NinjaScript Consultants who would be happy to create this indicator or any other at your request. If that is something that interests you, please let us know.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you Jim

      Thanks Jim that was a very helpful reply.

      Comment


        #4
        Hi folks, new to NT but not trading or developing strategies (although not in C++). Unsure if I should start a new thread but I have the identical challenge, would like to have a MACD indicator based on HULL averages rather than the built-in EMA.

        With the advise from this thread I changed the stock MACD to the following - the macd value itself seems in line but can't figure out how to calculate the macdAvg. I'm sure I'm missing something basic, all help appreciated!

        protected override void OnBarUpdate()
        {
        double input0 = Input[0];

        if (CurrentBar == 0)
        {
        fastEma[0] = input0;
        slowEma[0] = input0;
        Value[0] = 0;
        Avg[0] = 0;
        Diff[0] = 0;
        }
        else
        {

        /* Original calcs using EMA
        double fastEma0 = constant1 * input0 + constant2 * fastEma[1];
        double slowEma0 = constant3 * input0 + constant4 * slowEma[1];
        double macd = fastEma0 - slowEma0;
        double macdAvg = constant5 * macd + constant6 * Avg[1];
        */

        // Attempt to use Hull average instead
        double fastEma0 = HMA( Fast ) [0];
        double slowEma0 = HMA( Slow ) [0];
        double macd = fastEma0 - slowEma0;
        double macdAvg = HMA( Value, Smooth ) [0];

        fastEma[0] = fastEma0;
        slowEma[0] = slowEma0;
        Value[0] = macd;
        Avg[0] = macdAvg;
        Diff[0] = macd - macdAvg;

        }
        }

        Comment


          #5
          Hello DanPrado,

          Welcome to the NinjaTrader forums!

          The MACD indicator is calculating these values and is not calling another indicator.

          If its smoothing you want, you may want to supply the HMA as the input series to the SMA.
          SMA(HMA(Value, Smooth), 14)[0]
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks for your reply Chelsea. Right, the stock MACD indicator calculates the EMA directly, which is why I created a version that invokes the HMA() indicator instead of the direct calcs.

            Essentially the calculation that I am after is:
            macdValue = HMA( fastPeriod ) – HMA( slowPeriod )
            macdAvg = HMA( macdValue, macdPeriod )
            macdDiff = macdValue - macdAvg

            The following code returns the correct macdValue, but I have not figured out how to get the correct macdAvg:

            double macd = HMA( Fast ) [0] - HMA( Slow ) [0];
            double macdAvg = HMA( Value, Smooth ) [0];

            Value[0] = macd;
            Avg[0] = macdAvg;
            Diff[0] = macd - macdAvg;



            Thoughts?

            Comment


              #7
              Hello DanPrado,

              What is the actual calculation you are wanting?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Goal is to have a MACD indicator based on a Hull average rather than the stock Exponential.

                Comment


                  #9
                  Hello DanPrado,

                  That's an understandable goal. What calculation do you want to use for the macdAvg?

                  What calculation would you consider correct, if supplying the HMA() as the input series to the SMA() is not what you are looking for?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Sorry for not being more clear. In another platform I work with the calculations for its MACD indicator can be summarized as:

                    macdValue = HMA( close, fastLength ) - HMA( close, slowLength )
                    macdAvg = HMA( macdValue, macdLength)
                    macdDiff = macdValue – macdAvg

                    The resulting macdDiff is ultimately the value that I am after.

                    HMA() works well for calculating macdValue as it is based on the close bar value.

                    But for the macdAvg the calculation needs to consider the HMA of the macdValue for the defined period (macdLength). That is where I am failing.

                    Appreciate your patience on this!

                    Comment


                      #11
                      Hello DanPrado,

                      Is macdValue a Series<double> object or is this a double variable?

                      The HMA requires an input series and a period.

                      Perhaps you are wanting to save the values of the macdValue to a series for every bar and not just to a double variable which only holds a single value?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Yes, I thought i had tried that but possible I did it wrong. I will define a new series and try it again.

                        Comment


                          #13
                          Ok, making progress. Setting up the new input series works, although I am still not getting the expected results. But as validation I confirmed that the logic works using SMA(), so now just need to dig in and figure out why the HMA() is not producing the expected results (which could include that my benchmark is incorrect).

                          Thanks for your help and patience, I feel I have what I need to advance from here.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by rocketman7, Today, 09:41 AM
                          4 responses
                          15 views
                          0 likes
                          Last Post rocketman7  
                          Started by selu72, Today, 02:01 PM
                          1 response
                          9 views
                          0 likes
                          Last Post NinjaTrader_Zachary  
                          Started by WHICKED, Today, 02:02 PM
                          2 responses
                          12 views
                          0 likes
                          Last Post WHICKED
                          by WHICKED
                           
                          Started by f.saeidi, Today, 12:14 PM
                          8 responses
                          21 views
                          0 likes
                          Last Post f.saeidi  
                          Started by Mikey_, 03-23-2024, 05:59 PM
                          3 responses
                          56 views
                          0 likes
                          Last Post Sam2515
                          by Sam2515
                           
                          Working...
                          X