Pine Script v5 · Indicator
ZigZag
A Pine Script v5 indicator built on Pivothigh (high, 10) and Pivotlow (low, 10). It draws 2 marker series on the price chart. It declares 2 alerts. 3 settings; defaults include Pivot depth 10.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does ZigZag repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 300 of 300 bars.
- How many settings does ZigZag have?
- 3
- Defaults include Pivot depth 10, Label each swing true.
- What alerts does ZigZag have?
- 2
- Swing high confirmed; Swing low confirmed.
- Does ZigZag run in Pine Script v6?
- Yes
- Every plotted value identical on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does ZigZag draw on the price chart?
- Yes
- It is declared with overlay = true.
BTCUSDT · 1D · default settings
- Swing high
- Swing low
- Line colour
- Line colour
- 0.0%
- of bars
Share of 1,000 bars spent in each state with the default settings.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
Nothing to point out.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Pivot depth | 10 | int · min 2 | ph pl pivotBar plotshape |
| Label each swing | true | bool | line 34 |
| Line colour | #ffb74d | color | leg line 37 |
What it calculates
- Pivothighhigh, Pivot depth (10), Pivot depth (10)
ph · line 10 - Pivotlowlow, Pivot depth (10), Pivot depth (10)
pl · line 11
Signal flow
How price data and settings reach what is drawn. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
What it draws
| Output | Call | Value | By default | Line |
|---|---|---|---|---|
| plotshape | plotshape | not na(ph) | Shown | 42 |
| plotshape | plotshape | not na(pl) | Shown | 43 |
The shading takes one of 1 colours on every bar, each a state the script names in its settings: Line colour
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| Swing high confirmed | alertcondition | A swing high was confirmed | 45 |
| Swing low confirmed | alertcondition | A swing low was confirmed | 46 |
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip or realtime-only state. Values move while a bar forms and settle when it closes, as every indicator's do.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets, each the most recent 1,000 bars.
| Market | Line colour |
|---|---|
| BTCUSDT 1D | 0% of bars— |
| BTCUSDT 4h | 0% of bars— |
| ETHUSDT 1D | 0% of bars— |
| ETHUSDT 4h | 0% of bars— |
| SOLUSDT 1D | 0% of bars— |
| SOLUSDT 4h | 0% of bars— |
| BNBUSDT 1D | 0% of bars— |
| BNBUSDT 4h | 0% of bars— |
Second line: the average size of the move over the next 10 bars after the colour first appears, against the average 10-bar move on any bar.
Similar scripts
Indicators and strategies built from the same calculations.
| Script | Shares | Type |
|---|---|---|
| Pivot Point Breakout by EduardoMattje | pivotlow pivothigh | v5 strategy |
| Monthly Returns of a Strategy in a Chart by QuantNomad | pivothigh pivotlow | v5 strategy |
| RSI Centered Pivots by OztheWoz | pivotlow pivothigh | v4 strategy |
| ABCD Strategy by kerok3g | pivothigh pivotlow | v5 strategy |
| Steven Primo's bollinger bands strategy by EduardoMattje | pivothigh pivotlow | v5 strategy |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
Yes
Every plotted value identical on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © Zeal Terminal-//@version=5+//@version=6indicator("ZigZag", overlay = true, max_lines_count = 500, max_labels_count = 500)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 25 ms | Yes 1,000 bars in 26 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Written by Zeal Terminal and published under the Mozilla Public License 2.0.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Zeal Terminal//@version=5indicator("ZigZag", overlay = true, max_lines_count = 500, max_labels_count = 500) depth = input.int(10, "Pivot depth", minval = 2)showLabels = input.bool(true, "Label each swing")lineCol = input.color(#ffb74d, "Line colour") ph = ta.pivothigh(high, depth, depth)pl = ta.pivotlow(low, depth, depth) // Each confirmed pivot joins the previous one. Two highs in a row keep only// the higher, two lows only the lower.var float lastPrice = navar int lastBar = navar int lastDir = 0var line leg = na pivotPrice = not na(ph) ? ph : plpivotDir = not na(ph) ? 1 : not na(pl) ? -1 : 0pivotBar = bar_index - depth if pivotDir != 0 if pivotDir == lastDir and not na(leg) better = pivotDir == 1 ? pivotPrice > lastPrice : pivotPrice < lastPrice if better line.set_xy2(leg, pivotBar, pivotPrice) lastPrice := pivotPrice lastBar := pivotBar else if not na(lastPrice) leg := line.new(lastBar, lastPrice, pivotBar, pivotPrice, color = lineCol, width = 2) if showLabels label.new(pivotBar, pivotPrice, str.tostring(pivotPrice, format.mintick), style = pivotDir == 1 ? label.style_label_down : label.style_label_up, color = color.new(lineCol, 70), textcolor = color.white, size = size.tiny) lastPrice := pivotPrice lastBar := pivotBar lastDir := pivotDir plotshape(not na(ph), "Swing high", style = shape.triangledown, location = location.abovebar, color = #ef5350, size = size.tiny, offset = -depth)plotshape(not na(pl), "Swing low", style = shape.triangleup, location = location.belowbar, color = #26a69a, size = size.tiny, offset = -depth) alertcondition(not na(ph), "Swing high confirmed", "A swing high was confirmed")alertcondition(not na(pl), "Swing low confirmed", "A swing low was confirmed")