Home Technical Indicator Post

Unlocking the Professional ZigZag Indicator for MetaTrader 5

Attachments
263.zip (5.64 KB, Download 0 times)

Heads up! This version of the ZigZag indicator is based on ZigZag code and has been published here with the permission of its author, PPC. I've tested this version and fixed an issue in the algorithm.

This is the MQL5 version of the ZigZag indicator for MetaTrader 4.

Note: The AlexSTAL_OutsideBar.mqh library should be placed in the ..\MQL5\Include folder of your MetaTrader 5 terminal.

Is that all there is to it?

For me, the ZigZag indicator is like compressing the price chart. It can be calculated using the bar's price (like the Close price) or the High/Low prices of the bar, which is the most common method.

While using the standard ZigZag from MetaQuotes, I came across a couple of issues. The main downside is that the standard ZigZag doesn't account for outside bars.

Back in 2007, Rashid Umarov (Rosh) published the Zigzag2 R code which includes outside bar handling (using the DRAW_ZIGZAG style instead of DRAW_SECTION).

Now, let’s outline the features of a correctly implemented ZigZag:

  • The Highs and Lows must alternate strictly;
  • The High must be higher (or equal) than all bars on the right until the next Low;
  • The Low must be lower (or equal) than all bars on the left until the next High.

To illustrate, let’s look at an example (EURUSD, H1, December 8-13, 2006). Here, we have two cases with an outside bar. The standard ZigZag fails to construct the “correct” picture.

Figure 1. Typical ZigZag with input parameters [12;5;3] and [12;0;0]

Figure 1. Typical ZigZag with input parameters [12;5;3] and [12;0;0]

Figure 2. Zigzag2 R with input parameters [12;5;3]

Figure 2. Zigzag2 R with input parameters [12;5;3]


Figure 3. Zigzag2 R with input parameters [12;0;0]

Figure 3. Zigzag2 R with input parameters [12;0;0]

At first glance, Figure 3 looks correct, but there are some caveats:

  1. While the construction is accurate due to the input parameters, the algorithm should work effectively with any parameters.
  2. The outside bar on the left side is incorrect due to the order of High/Low formation of the bar:


The ZigZag segments need to be constructed in this manner.

I’ve tested numerous ZigZag algorithms: ZigZag MT3, ZigZag, Zigzag R, Zigzag2 R, ZigZag_NK, ZigZag_ZUP89_nen, ZigZag_new_nen4, CZigZag, Swing_ZZ_1, DT_ZZ_nen, ZigZag_Talex, Reverse_ZZ, Points_ZZ_1, i_MF_Fractal_ZZ_3in1, ang_Amp_ZZ, ZigzagFr_v1, NonLagZigZag_v2, ZZ_FF_v3, ZigZag Ensign, ZigZag tauber, SQZZ Товаровед, ZZ_2L_nen wellx, Channel ZZ, Azzx_larryzz_3_0, RPoint, RPoint-m v2, ZigZagTF. But none of them resolved the issue accurately.


Here are some figures showcasing this indicator:

Figure 4. Professional ZigZag with input parameters [12;0]

Figure 4. Professional ZigZag with input parameters [12;0]

Figure 5. Professional ZigZag with input parameters [23;0]

Figure 5. Professional ZigZag with input parameters [23;0]


Features of this version include:

  • One-pass algorithm
  • It recalculates and redraws only the last segment (the segment can’t be canceled unless there’s a history update)
  • It uses an optimization algorithm for calculations inside the bar (it doesn’t recalculate on the tick if the price has changed within the current bar or if the price change is below the threshold defined by the MinMotion input parameter)
  • It offers two methods for ordering High/Low formation: one using the bar type (fast but less precise) and another using data from a lower timeframe (which requires history verification or modifications to the OutsideBar library).
The ProExtra version isn't published yet, as the accuracy of the MetaTrader 5 quotes hasn’t been verified.


Here are more examples of incorrect ZigZag constructions:

Figure 6. EURUSD, H1, November 29-December 7. (Red line: ZigZag with input parameters [7;0;0]. Blue line: Zigzag2 R with input parameters [7;0;0].)

Figure 6. EURUSD, H1, November 29-December 7. (Red line: ZigZag with input parameters [7;0;0]. Blue line: Zigzag2 R with input parameters [7;0;0].)

Figure 7. EURUSD, H1, 2010, November 29 - December 7. The correct construction of ZigZag segments.

Figure 7. EURUSD, H1, 2010, November 29 - December 7. The correct construction of ZigZag segments.


Conclusion

While this version isn’t perfect, I’m actively working on it.

Due to the channel algorithm (iHighest/iLowest), sometimes it skips the highs.

Please let me know about any bugs or features of this indicator.

Related Posts

Comments (0)