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

Zero Lag MACD - programming hint

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

    Zero Lag MACD - programming hint

    I'd like to build a zerolag MACD but run into a problem, perhaps somebody can give me a hint (Zerolag is defined by calculating the EMA and then the EMA of that EMA):

    The MACD I got which is:

    double EMA1 = EMA(Input, Fast)[0];
    double EMA2 = EMA(EMA(Input, Fast), Fast)[0];
    zeroEMA.Set(EMA1 + (EMA1 - EMA2));

    double EMA3 = EMA(Input, Slow)[0];
    double EMA4 = EMA(EMA(Input, Slow), Slow)[0];
    zeroEMAs.Set(EMA3 + (EMA3 - EMA4));

    double macd = zeroEMA[0] - zeroEMAs[0] ;


    Now I need the same using the "macd" as input aboveto calculate the trigge for which I put:
    double EMA8 = EMA(macd, smooth)[0];
    double EMA9 = EMA(EMA(macd, Smooth), smooth)[0];
    macdAvg.Set(EMA8 + (EMA8 - EMA9));

    but that doesn't work.

    Does somebody perhaps know what I have to put so it recognizes the "macd" as a "input" variable. I tried putting a "private Dataseries macd" under variables but that does nothing.

    Any help would be appreciated.
    Martin

    #2
    I don't know exactly but why not use the ZeroLagEMA indicator already included with NinjaTrader as the basis for your indicator? It might help simply things

    Simply add two ZeroLagEMAs then a third plot with a bar line style for the difference.


    I hope that helps, I look forward to the result.
    Last edited by Elliott Wave; 05-21-2008, 05:06 PM.

    Comment


      #3
      To have the MACD as a DataSeries input for the EMA you need to do something like EMA(MACD(12, 26, 9), Fast)[0].
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Thank you, but you need to work around it. I think I found the solution and I'll go through it and maybe you can check it to see if you think it's right:

        The original MACD is this:
        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]);

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

        Which is the same as:
        fastEma.Set((EMA(Input,Fast)[0]) - (EMA(Input,Slow)[0]));
        double macd = fastEma[0] ;

        macdAvg2.Set(EMA(fastEma,Smooth)[0]);
        double macdAvg = macdAvg2[0];

        The "slowEma" is gone as I already put it into the "fastEma" set. Build a new "macdAvd2" set and then use the fastEma as the input as seen above. Under variables add "private DataSeries macdAvg2;" and under initialize add "macdAvg2 = new DataSeries(this);" The "new" code above and the origial MACD as given in NT plot the same thing. So it's right.

        So now I just changed the EMA to ZeroLagEMA and the final code looks like this:
        fastEma.Set((ZeroLagEMA(Input,Fast)[0]) - (ZeroLagEMA(Input,Slow)[0]));
        double macd = fastEma[0] ;

        macdAvg2.Set(ZeroLagEMA(fastEma,Smooth)[0]);
        double macdAvg = macdAvg2[0];

        The rest of the MACD is the same as the original indicator. This "seems" to work except I don't have another charting programme to see if it is really the right thing.

        Could you verify this ? If it is right it could be put into the indicator collection here in the forum.

        Martin



        Originally posted by Elliott Wave View Post
        I don't know exactly but why not use the ZeroLagEMA indicator already included with NinjaTrader as the basis for your indicator? It might help simply things

        Simply add two ZeroLagEMAs then a third plot with a bar line style for the difference.


        I hope that helps, I look forward to the result.

        Comment


          #5
          Due to bandwidth reasons we cannot provide support in checking every indicator for accuracy. I suggest you just run through it manually with a calculator for a few values and if they match up then you probably have it right.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            I'm not aware of any program that has a ZeroLagMACD as standard so the best thing to do is export it and post it as an attachment in this thread. Once its been tested a bit and there's a chance to iron out any kinks, then you can post it in the file sharing section and everyone can benefit.

            Comment


              #7
              Thank you. A reference can be found here: http://www.meta-formula.com/Metastock-Formulas-Z.html

              I'll try to verify my formula and if it is correct I'l post the results.



              Originally posted by Elliott Wave View Post
              I'm not aware of any program that has a ZeroLagMACD as standard so the best thing to do is export it and post it as an attachment in this thread. Once its been tested a bit and there's a chance to iron out any kinks, then you can post it in the file sharing section and everyone can benefit.

              Comment


                #8
                Unfortunately I cannot export the indicator from my computer, I get an error message. It says "error CS0103" . I think I need to add the EMA to the export file but it's not on the list when I select the files(indicators) for export.

                Is there a trick to this ? Basically I think I need to export my indicator, the ZeroLagEMA and the EMA.

                Any hint ?

                Comment


                  #9
                  AKIAK you just need to give a name to the file at the top, add the ZeroLag MACD and when you click export it should automatically include any other required indicators.

                  Comment


                    #10
                    I'm not sure what formula you've used, but I found this tidbit regarding low lag MACD...

                    ...a common first-approach toward creating a MACD with JMA is to use it for both the fast and slow crossover lines as follows:
                    MyMACD=fast JMA - slow JMA
                    However, the result is mediocre. This is because MACD actually *requires* lag, but only *between* the two crossing lines. Therefore, for best results, it's better that the slower line have both lag and momentum so that it does not weaken when price reverses. Otherwise, the crossover is delayed as the faster line tries to meet the slower one (which is turning away). A simple moving average (SMA) has more lag and momentum than JMA. So a better MACD would be as follows:
                    MyMACD=fast JMA - slow SMA
                    This revised formula simultaneously maximizes lag between the two filters as well as minimizes the delay in attaining crossover signals. Its surprising how many customers discover this on their own...

                    Comment


                      #11
                      Still can't export it. I could post the code.

                      Regarding JMA: I have actually built "a" JMA MACD (basically a MACD with 3 JMAs which resulted in nothing => meaning I couldn't find any good values for the JMAs. But I think I did it wrong anyway because it's not a MACD with 3 JMAs but rather the difference of 2 JMA with a trailing JMA (I suppose this could also be done with a ZeroLagEMA as well). So when I did this a month ago I didn't realize that what he (Jurik) describes as a "MACD" is not quite the same as the traditional MACD.

                      Still learning but getting there...

                      Comment


                        #12
                        I need to correct that: the JMA MACD I did is right, but finding good values has been difficult. Haven't tried optimizing it though.

                        Comment


                          #13
                          Originally posted by total3 View Post
                          Still can't export it. I could post the code.
                          Good idea. Then I'll compile it and see if I can export it...

                          By the way, how are the Jurik indicators? Is the JMA much different than the ZeroLagEMA or T3? I'm considering getting them, but there doesn't seem to be much feedback on them in the forum. If know one is talking about them, I guess that means they probably work well
                          Last edited by Elliott Wave; 06-05-2008, 02:11 PM.

                          Comment


                            #14
                            Originally posted by Elliott Wave View Post
                            Good idea. Then I'll compile it and see if I can export it...

                            By the way, how are the Jurik indicators? Is the JMA much different than the ZeroLagEMA or T3? I'm considering getting them, but there doesn't seem to be much feedback on them in the forum. If know one is talking about them, I guess that means they probably work well

                            Okay, so here comes the code for the ZeroLag MACD and also I am adding the "Leader of th MACD" code. This is a new indicator which TASC is featuring next month (it's not out yet, but since Worden already is using it in Blocks I quickly did the code...hopefully I'm not gonna get shot for this. Perhaps you could compile this as well for the community....)

                            Regarding the JMA: I guess it depends on your "system", what is good is the DMX (which is a better version than the DM (DM+/DM-), however one has to admit that whatever indicators you are using, there will always be false signals no matter what. The JMA uses a "Phase" and a length, so if you put this into a MACD for example, rather than having 3 inputs (fast, slow, smooth) you have to figure out 6 >= fast with phase & length and so on. This is why I said in my earlier post that I have not found any "good" values..and there are no hints anywhere either. I found one (!) set of example values for the JMA MACD on some obscure russian board... but they made no sense either.

                            Also the numbers are very different from regular MA, so let's say if you use an EMA of 20 the JMA might actually be 30 in length to get a "similar" picture and then you can start playing around with the phase which runs from -100 to +100 in order to change the lag. So you got more room to work with which can be good but I guess also frustrating. I bought a 3 month liscence for about $150 which I think is good in order to try it. So far I have to admit that I haven't made up my mind if I'm going to keep them. However the JMA does "hug" the price action better than a ZeroLagEMA and certainly T3 or TEMA (they will just over- or undershoot like heck...). I could post a screen shot of a JMA compared to say ZeroLag so let me know.

                            Anyway, now here's the code for ZeroLagMACD and the "Leader f MACD"

                            variables:
                            private int fast = 12;
                            private int slow = 26;
                            private int smooth = 9;
                            private DataSeries fastEma;
                            private DataSeries diffArr;

                            initialize:
                            Add(new Plot(Color.Green, "Macd"));
                            Add(new Plot(Color.DarkViolet, "Avg"));
                            Add(new Plot(new Pen(Color.DodgerBlue, 2), PlotStyle.Bar, "Diff"));

                            Add(new Line(Color.DarkGray, 0, "zero line"));

                            fastEma = new DataSeries(this);
                            macdAvg2 = new DataSeries(this);
                            diffArr = new DataSeries(this);

                            onbarupdate:
                            if (CurrentBar == 0)
                            {
                            fastEma.Set(Input[0]);
                            macdAvg2.Set(Input[0]);
                            Value.Set(0);
                            Avg.Set(0);
                            Diff.Set(0);
                            }
                            else
                            {
                            fastEma.Set((ZeroLagEMA(Input,Fast)[0]) - (ZeroLagEMA(Input,Slow)[0]));
                            double macd = fastEma[0] ;

                            macdAvg2.Set(ZeroLagEMA(fastEma,Smooth)[0]);
                            double macdAvg = macdAvg2[0];

                            Value.Set(macd);
                            Avg.Set(macdAvg);

                            Diff.Set(macd - macdAvg);
                            }

                            Properties:
                            /// <summary>
                            /// </summary>
                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Avg
                            {
                            get { return Values[1]; }
                            }

                            /// <summary>
                            /// </summary>
                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Default
                            {
                            get { return Values[0]; }
                            }

                            /// <summary>
                            /// </summary>
                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Diff
                            {
                            get { return Values[2]; }
                            }

                            /// <summary>
                            /// </summary>
                            [Description("Number of bars for fast EMA")]
                            [Category("Parameters")]
                            public int Fast
                            {
                            get { return fast; }
                            set { fast = Math.Max(1, value); }
                            }

                            /// <summary>
                            /// </summary>
                            [Description("Number of bars for slow EMA")]
                            [Category("Parameters")]
                            public int Slow
                            {
                            get { return slow; }
                            set { slow = Math.Max(1, value); }
                            }

                            /// <summary>
                            /// </summary>
                            [Description("Number of bars for smoothing")]
                            [Category("Parameters")]
                            public int Smooth
                            {
                            get { return smooth; }
                            set { smooth = Math.Max(1, value); }
                            }

                            "Leader of the MACD":
                            variables:
                            private int fast = 12;
                            private int slow = 26;
                            private int smooth = 9;

                            private DataSeries fastMACD;
                            private DataSeries macdAvg2;
                            private DataSeries fastEma;
                            private DataSeries slowEma;
                            private DataSeries slow1S;
                            private DataSeries slowF;
                            private DataSeries fast1S;
                            private DataSeries fastF;

                            initialize:
                            Add(new Plot(Color.Green, "Macd"));
                            Add(new Plot(Color.DarkViolet, "Avg"));
                            Add(new Plot(new Pen(Color.Navy, 2), PlotStyle.Bar, "Diff"));
                            Add(new Plot(Color.Red, "Leader"));

                            Add(new Line(Color.DarkGray, 0, "Zero line"));

                            fastMACD = new DataSeries(this);
                            macdAvg2 = new DataSeries(this);

                            fastEma = new DataSeries(this);
                            slowEma = new DataSeries(this);

                            slow1S = new DataSeries(this);
                            slowF = new DataSeries(this);

                            fast1S = new DataSeries(this);
                            fastF = new DataSeries(this);

                            onbarupdate:
                            if (CurrentBar == 0)
                            {

                            fastMACD.Set(Input[0]);
                            macdAvg2.Set(Input[0]);

                            fastEma.Set(Input[0]);
                            slowEma.Set(Input[0]);

                            slow1S.Set(Input[0]);
                            slowF.Set(Input[0]);

                            fast1S.Set(Input[0]);
                            fastF.Set(Input[0]);

                            Value.Set(0);
                            Avg.Set(0);
                            Diff.Set(0);
                            Leader.Set(0);
                            }
                            else
                            {

                            slowEma.Set(Close[0] - EMA(Input,Slow)[0]) ;
                            slow1S.Set(EMA(slowEma,Slow)[0]);
                            slowF.Set(slow1S[0] + EMA(Input,Slow)[0]);
                            double slowFX = slowF[0];

                            fastEma.Set(Close[0] - EMA(Input,Fast)[0]) ;
                            fast1S.Set(EMA(fastEma,Fast)[0]);
                            fastF.Set(fast1S[0] + EMA(Input,Fast)[0]);
                            double fastFX = fastF[0];



                            fastMACD.Set((EMA(Input,Fast)[0]) - (EMA(Input,Slow)[0]));
                            double macd = fastMACD[0] ;

                            macdAvg2.Set(EMA(fastMACD,Smooth)[0]);
                            double macdAvg = macdAvg2[0];


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

                            Leader.Set(fastFX - slowFX);
                            }

                            Properties:
                            /// <summary>
                            /// </summary>
                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Avg
                            {
                            get { return Values[1]; }
                            }

                            /// <summary>
                            /// </summary>
                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Default
                            {
                            get { return Values[0]; }
                            }


                            /// <summary>
                            /// </summary>
                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Diff
                            {
                            get { return Values[2]; }
                            }


                            /// <summary>
                            /// </summary>
                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Leader
                            {
                            get { return Values[3]; }
                            }


                            /// <summary>
                            /// </summary>
                            [Description("Number of bars for fast EMA")]
                            [Category("Parameters")]
                            public int Fast
                            {
                            get { return fast; }
                            set { fast = Math.Max(1, value); }
                            }

                            /// <summary>
                            /// </summary>
                            [Description("Number of bars for slow EMA")]
                            [Category("Parameters")]
                            public int Slow
                            {
                            get { return slow; }
                            set { slow = Math.Max(1, value); }
                            }

                            /// <summary>
                            /// </summary>
                            [Description("Number of bars for smoothing")]
                            [Category("Parameters")]
                            public int Smooth
                            {
                            get { return smooth; }
                            set { smooth = Math.Max(1, value); }
                            }

                            Enjoy......

                            Comment


                              #15
                              Originally posted by total3 View Post

                              Regarding the JMA: I guess it depends on your "system", what is good is the DMX (which is a better version than the DM (DM+/DM-), however one has to admit that whatever indicators you are using, there will always be false signals no matter what.
                              The DM is one of my favorite indicators for sure. I've experimented with creating DMX inspired versions based on the ZeroLagEMA and T3 and its much better than the original, so I figured the Jurik version would be even better.

                              Originally posted by total3 View Post
                              I bought a 3 month liscence for about $150 which I think is good in order to try it. So far I have to admit that I haven't made up my mind if I'm going to keep them. However the JMA does "hug" the price action better than a ZeroLagEMA and certainly T3 or TEMA (they will just over- or undershoot like heck...). I could post a screen shot of a JMA compared to say ZeroLag so let me know.
                              I'm thinking of doing the same (3 months) , what ticks me off though is that users of other platforms can buy the tools, but NT users must 'subscribe' to them.

                              A couple screen shots would be cool if you get a chance, maybe comparing the JMA to the ZeroLagEMA and T3 with the DM, and DMX on panels below.

                              By the way recently in TASC, there was a ZeroLagTEMA and ZeroLAGHATEMA (Heiken Ashi) which you may find of use.

                              According to Jurik's own site the "Modified Optimal Elliptical Filter" is about the closest thing to the JMA. I'm trying to convert it to NT. I've posted the code in another thread so hopefully if I can't figure it out soon someone will...

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bmartz, 03-12-2024, 06:12 AM
                              4 responses
                              32 views
                              0 likes
                              Last Post bmartz
                              by bmartz
                               
                              Started by Aviram Y, Today, 05:29 AM
                              4 responses
                              12 views
                              0 likes
                              Last Post Aviram Y  
                              Started by algospoke, 04-17-2024, 06:40 PM
                              3 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by gentlebenthebear, Today, 01:30 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by cls71, Today, 04:45 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X