Possible bug in the ZigZag indicator (MT5) - page 2

 
Vladislav Boyko #:
If the high isn't fully formed yet and a new high appears, the new high cancels out the previous high.
This is what makes a zigzag a zigzag. One of the fundamental principles of the algorithm.
Vladislav Boyko #:
If there was a low between those two highs, then you get a zigzag leg connecting the two lows.

This is a special case that sometimes arises. The leg connecting two lows is a display issue, not a behavior issue.

 
Vladislav Boyko #:
If the high isn't fully formed yet and a new high appears, the new high cancels out the previous high. If there was a low between those two highs, then you get a zigzag leg connecting the two lows.

Although, in theory, one could try to anticipate this case in the algorithm: a new high should not cancel out the previous one if there is a low between them.

 

I've had the idea of writing an article about developing an advanced version of the built-in zigzag in my head for a long time. That is, keeping the algorithm but adding additional features. I already have some experience with this and some ideas.

Now I've got an additional idea - to add this feature and the ability to enable/disable it in the settings:

Forum on trading, automated trading systems and testing trading strategies

Possible bug in the ZigZag indicator (MT5)

Vladislav Boyko, 2026.07.30 09:56

a new high should not cancel out the previous one if there is a low between them.

I won't have time for this in the next couple of months, but I'll implement it sooner or later.

 
Vladislav Boyko #:

I've had the idea of writing an article about developing an advanced version of the built-in zigzag in my head for a long time. That is, keeping the algorithm but adding additional features. I already have some experience with this and some ideas.

Now I've got an additional idea - to add this feature and the ability to enable/disable it in the settings:

I won't have time for this in the next couple of months, but I'll implement it sooner or later.

Ask an AI agent 😁
 
Vladislav Boyko #:
This is what makes a zigzag a zigzag. One of the fundamental principles of the algorithm.

This is a special case that sometimes arises. The leg connecting two lows is a display issue, not a behavior issue.


And that's what I call an obsolete algorithm.
We're not living in the past anymore. It's possible to make a zigzag which is truly stateful, and doesn't consider lows and highs at the same time, and leg erasures. Look at the code I posted, it won't form invalid legs that have to be overwritten. The original zigzag is a sieve which searches new lows and highs simultaneously - that was never necessary, and also causes more lag. 
 
Alain Verleyen #:
Ask an AI agent 😁
I tinkered with an AI agent before and found that it can't code working zigzags at all, and even if you give it a working zigzag code, it will break it for you. It seems that the AI is more efficient at coding more straightforward indicators like an ATR or moving average.
 
Conor Mcnamara #:
I tinkered with an AI agent before and found that it can't code working zigzags at all, and even if you give it a working zigzag code, it will break it for you. It seems that the AI is more efficient at coding more straightforward indicators like an ATR or moving average.

IMHO, MQL5 Lite AI's strongest suit is converting/rewriting indicator code from other languages.

One thing that I learned rather quickly is to prompt the prepositional phrase, "without editing my original indicator code." 😬

 
Julio Alonso Martinez:

Hello MQL Team, or anyone around,

I would like to report what appears to be an issue with the standard ZigZag indicator.

I loaded a ZigZag (Depth = 480, Deviation = 5, Backstep = 3) on the EURUSD H1 chart. A depth of 480 corresponds to approximately 20 trading days (24 hours × 5 trading days per week).

The indicator correctly plotted a downward segment between:

  • High: 17 April, 16:00 — 1.18490
  • Low: 24 June, 16:00 — 1.13240

(All times are MetaQuotes-Demo server time.)

From that point onward, however, the indicator unexpectedly draws a new segment directly from that low (1.13240) to the next low formed on 28 July, 13:00 — 1.13533.

This behaviour seems incorrect for two reasons:

  1. No trend reversal should have been confirmed, since the price never exceeded the highest high within the previous 480-bar lookback period. Therefore, I would not expect a new ZigZag segment to be created.
  2. The indicator connects two consecutive lows. By definition, a ZigZag should alternate between highs and lows, so a segment connecting one low directly to another low should not be possible.

Could you please confirm whether this is the expected behaviour of the standard ZigZag algorithm, or whether it is a bug?

Thank you in advance for your assistance.

Kind regards,

Hello

I analyzed the standard ZigZag.mq5 source code regarding your issue. The drawing bug when setting a large InpDepth (like 480) is caused by a hardcoded lookback limit of 100 bars inside the OnCalculate function.

The Root Cause:When a new tick arrives, the default code tries to find the last 3 extremes ( ExtRecalc = 3 ) to resume its calculation. However, it restricts the search to only the last 100 bars ( rates_total - 100 ). If your InpDepth is 480, it is mathematically impossible to find 3 extremes within just 100 bars. The loop gives up, the starting point of the calculation gets corrupted, and the indicator draws incorrectly (e.g., connecting a high to a high).

The Solution: You can easily fix this by modifying two lines in your ZigZag.mq5 file to remove the hardcoded limits.


Fix 1: Ensure enough bars exist before starting.
Original:

if(rates_total<100)

      return(0);

Modified:

if(rates_total<InpDepth)

      return(0);


Fix 2: Remove the 100-bar lookback limit.
Allow the while loop to search as far back as necessary to find the last 3 extremes.

Original:

while(extreme_counter<ExtRecalc && i>rates_total-100)

Modified:

while(extreme_counter<ExtRecalc && i>=0)


This is probably a known issue, but applying this fix should resolve it.