ZigZag
ZigZag in ProBuilder filters price swings smaller than a percentage threshold, connecting significant highs and lows. Syntax, parameters, examples, gotchas.
Syntax
ZigZag[Zr](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Zr | number | none | Reversal threshold in percent. A new pivot is confirmed only after price moves at least Zr percent against the current swing. |
price | price source | close | The series pivots are detected on. Accepts close, open, high, low, or a custom variable. |
Formula
There is no closed-form formula. The construction rule is:
1. Track the extreme of the current swing (highest or lowest price since the last pivot).
2. When price retraces at least Zr percent from that extreme, the extreme becomes
a confirmed pivot and a new swing starts in the opposite direction.
3. The output on each bar is the value of the straight line joining consecutive pivots.How it works
The indicator compresses price action into alternating up and down legs. While price keeps extending in the current direction, the provisional pivot keeps moving with it. Only when a retracement of at least Zr percent occurs does the pivot lock in and the line change direction. Everything smaller than the threshold is discarded, which is why the output looks like a clean sequence of swings connecting significant highs and lows.
The defining property, and the main hazard, is repainting. The last segment is provisional: if price resumes the prior direction before completing a Zr percent reversal, the recent pivot is removed and the segment is redrawn. Historical charts therefore look far cleaner than anything that was knowable in real time. The indicator is a tool for describing past structure, swing analysis, wave counting, support and resistance mapping, not a live signal generator.
Larger Zr values keep only major swings, smaller values retain finer structure. The percentage basis means the filter adapts to price level, a 5 percent filter means the same thing at 100 as at 10000, unlike the fixed-point variant ZigZagPoint.
Examples
Example 1, 5 percent zig-zag on closing prices (Indicator)
// Keep only swings larger than 5 percent
myZigZag = ZigZag[5](close)
RETURN myZigZagThe source example: swings smaller than 5 percent from the last pivot are filtered out, leaving only the significant legs of the move.
Example 2, Measuring the current swing length (Indicator)
// Estimate how far price has travelled in the current zig-zag leg
zz = ZigZag[3](close)
IF zz > zz[1] THEN
direction = 1 // rising leg
ELSIF zz < zz[1] THEN
direction = -1 // falling leg
ELSE
direction = direction[1]
ENDIF
RETURN direction AS "ZigZag leg direction"Derives the direction of the current zig-zag leg by comparing consecutive values of the line. Useful for labelling past swing structure in studies.
Example 3, Screening by position in the swing (ProScreener)
// Instruments whose 5 percent zig-zag is in a rising leg
zz = ZigZag[5](close)
risingLeg = zz > zz[1]
SCREENER[risingLeg](((close / zz) - 1) * 100 AS "% vs ZigZag")Filters for instruments currently in a rising zig-zag leg. Because the last leg repaints, results describe the present reading, not a stable historical signal.
Interpretation
The zig-zag line is descriptive. Alternating pivots mark the swing highs and lows that survived the Zr filter, which makes the indicator a common base layer for Elliott Wave counting, harmonic pattern measurement, and mapping support and resistance from prior pivots.
Rising pivots (higher highs and higher lows) describe an uptrend in the filtered structure, falling pivots describe a downtrend. None of this predicts the next move: the final leg is always provisional, so any reading taken from the most recent segment can change retroactively.
Common errors and gotchas
- Backtesting on the repainting leg. Strategy rules that reference recent ZigZag values see pivots that were only confirmed later. Backtests built this way overstate results badly and cannot be reproduced live.
- Threshold in the wrong unit.
Zris a percentage. To filter by an absolute number of points, useZigZagPointinstead, the two are not interchangeable on the same setting. - Threshold too small. Values well under 1 percent on volatile instruments produce a line that flips nearly every bar, defeating the purpose of the filter.
- Reading the line as a moving average. The output is a piecewise-linear path between pivots, not a smoothed price. Distance from the line has no standard-deviation-style meaning.
Related instructions
ZigZagPoint, the same construction with a fixed point threshold.Highest, highest value over N bars, a non-repainting swing tool.Lowest, lowest value over N bars.SAR, parabolic stop and reverse, a non-repainting trend tracker.Supertrend, trend-following line based on average true range.Average, moving average for conventional smoothing.DRAWLINE, draws custom segments between chart points.Cycle, cycle-detection indicator for periodic structure.
