Deviation param from standard ZigZag

 

Hi everyone,

I've been studying the built-in ZigZag indicator and I can't figure the situations where the "Deviation" parameter has an impact on how the lines are drawn. Maybe you can point to me what am I missing or let me know if you have the same feeling.

For those which see this post and are wondering about the meaning of the input params of this indicator, here what I was able to find on forums: https://www.mql5.com/en/forum/100475#comment_2956661 and also this https://www.forexfactory.com/thread/post/11196587#post11196587.

After playing several hours with the ZigZagColor indicator in MetaTrader version 5 on several markets/timeframes, and after also debugging it in MetaEditor, I'm starting to question that the deviation input param has any effect whatsoever. The "Back step" will make sure that if a new low (high) is found, then it removes any lows (highs) that may have been found in the last 3 bars (if default backstep value was used). And this is done always regardless of the value of the deviation parameter. 


Below I've copied the code from ZigZagColor.mq5 which searches for the local lows.

Some points, if you don't have experience with the implementation:
- val is the local minimum (too bad that the variable was not named properly)
- a new found low is added to the buffer only if it is the last candle in the array window of candles (see: if(low[shift]==val)         LowMapBuffer[shift]=val;)
- the code that uses the deviation is accessed only when a new low is found;
- "backstep" must be smaller than "depth", otherwise an "array out of range" exception is thrown.

//--- searching for high and low extremes
   for(shift=start; shift<rates_total && !IsStopped(); shift++)
     {
      //--- low
      val=Lowest(low,InpDepth,shift);
      if(val==last_low)
         val=0.0;
      else
        {
         last_low=val;
         if((low[shift]-val)>(InpDeviation*_Point))
            val=0.0;
         else
           {
            for(back=InpBackstep; back>=1; back--)
              {
               res=LowMapBuffer[shift-back];
               //---
               if((res!=0) && (res>val))
                  LowMapBuffer[shift-back]=0.0;
              }
           }
        }
      if(low[shift]==val)
         LowMapBuffer[shift]=val;
      else
         LowMapBuffer[shift]=0.0;
 
Please insert the code correctly: when editing a message, press the button       Codeand paste your code into the pop-up window.
 
zeratus2001: I've been studying the built-in ZigZag indicator and I can't figure the situations where the "Deviation" parameter has an impact on how the lines are drawn. Maybe you can point to me what am I missing or let me know if you have the same feeling.

You are not missing anything. The built-in ZigZag by MetaQuotes is in fact flawed and the "Deviation" parameter does nothing. It has been reported before but nothing has been done about it.

I too fell for the same problem when I made my own version using the original code as my starting point. Only after many years when a user pointed it out to me, did I understand that the "Deviation" had no effect at all and that the logic of MetaQuotes' ZigZag code was flawed.

It might be best for you to look at other versions of ZigZag in the CodeBase that don't suffer from the same problem.

 

Hi Fernando,

Thank you for the answer.

The built in implementation is the only one that I could find that uses the depth + deviation + backstep inputs. If you are aware of a version that fixed the built-in implementation, I would appreciate if you post the link. I am curious how would an elegant implementation look like. 

 
zeratus2001: The built in implementation is the only one that I could find that uses the depth + deviation + backstep inputs. If you are aware of a version that fixed the built-in implementation, I would appreciate if you post the link. I am curious how would an elegant implementation look like. 

Since I don't use ZigZag at all, I never did try to find a valid one nor did I try to fix my one either. Sorry!

 
In de zigzagcolor latest version the InpDeviation input parameter is used to check if the "step" is bigger or smaller than the InpDeviation*Point() Deviation in mql5 is slippage in mql4 it has to do with room for placing an order in pips.
 
Probably a bug in the code
Reason: