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

Improved MACD Indicator how to...

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

    Improved MACD Indicator how to...

    Hi!
    My question is: Can I rewrite the MACD in the way that will use Weighted moving averages(SlowMA and FastMA). For example in ThinkOrSwim platform MACD comes with 4th parameter and you can choice the AverageType - The type of moving average to be used in calculations for MACD : simple, exponential, weighted, Wilder's, or Hull.). I will need only weighted....

    Thank You in advance.

    #2
    Hello krasisoft,

    Thanks for your post and welcome to the forums!

    Yes, it is certainly possible to edit the Macd's ninjascript file to change from ema to the ma of your choosing. You may also want to review the file sharing section of the forums to see if there is something there that will meet your needs or something that may provide an added feature that you might want to incorporate in your version of the macd.

    Here is a link to the helpguide section concerning developing indicators: http://ninjatrader.com/support/helpG...indicators.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      This is what I changed:

      fastEma.Set((2.0 / (1 + Fast)) * Weighted[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
      slowEma.Set((2.0 / (1 + Slow)) * Weighted[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);

      // fastEma.Set((2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
      // slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);

      The last two lines are the original code from MACD indicator, but doesn't do the job....may be need more experienced programer in ninja script... HELP how can i weighted the fast and slowMA?
      Last edited by krasisoft; 07-24-2015, 12:45 PM.

      Comment


        #4
        Hello krasisoft,

        Thanks for your post.

        The lines you changed are the equation for the EMA itself so comment them out.

        Change this line: double macd = fastEma[0] - slowEma[0];

        To this: double macd = WMA(fast)[0] - WMA(slow)[0];
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          I think i did it!!!!!!! I made a mistake the EMA = Exponential moving avrg....and i stilll calculated the EMA on instead WMA but with this code below it did what i need (i think) i will test it later on.....

          this is the code:
          protected override void OnBarUpdate()
          {

          if (CurrentBar == 0)
          {
          fastEma.Set(Input[0]);
          slowEma.Set(Input[0]);
          Value.Set(0);
          Avg.Set(0);
          Diff.Set(0);
          }
          else
          {
          int back = Math.Min(fast - 1, CurrentBar);
          double val = 0;
          int weight = 0;
          for (int idx = back; idx >=0; idx--)
          {
          val += (idx + 1) * Input[back - idx];
          weight += (idx + 1);
          }
          fastEma.Set(val / weight);


          back = Math.Min(slow - 1, CurrentBar);
          val = 0;
          weight = 0;
          for (int idx = back; idx >=0; idx--)
          {
          val += (idx + 1) * Input[back - idx];
          weight += (idx + 1);
          }
          slowEma.Set(val / weight);

          double macd = (fastEma[0] - slowEma[0]);
          double macdAvg = (2.0 / (1 + Smooth)) * macd + (1 - (2.0 / (1 + Smooth))) * Avg[1];

          Value.Set(macd);
          Avg.Set(macdAvg);
          Diff.Set((macd - macdAvg));
          }
          }

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by mjairg, 07-20-2023, 11:57 PM
          3 responses
          213 views
          1 like
          Last Post PaulMohn  
          Started by TheWhiteDragon, 01-21-2019, 12:44 PM
          4 responses
          544 views
          0 likes
          Last Post PaulMohn  
          Started by GLFX005, Today, 03:23 AM
          0 responses
          3 views
          0 likes
          Last Post GLFX005
          by GLFX005
           
          Started by XXtrader, Yesterday, 11:30 PM
          2 responses
          12 views
          0 likes
          Last Post XXtrader  
          Started by Waxavi, Today, 02:10 AM
          0 responses
          7 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Working...
          X