ZigZag Segments
The classic percentage ZigZag: straight lines between swing highs and lows that reverse by at least a threshold percentage, each swing labelled with its percentage move.
- ZigZag %
- Updates at bar close
- zigzag
- swings
- swing high
- swing low
- percent
- lines
MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.

Overview
The ZigZag filters out moves smaller than a percentage threshold and joins the remaining swing highs and swing lows with straight lines. A swing is confirmed once price moves the threshold against it; the leg after the last confirmed swing is still open and drawn dashed. Each confirmed swing is labelled with the percentage move from the swing before it.
How to read it
The solid lines show the swings that mattered at your threshold: higher highs and higher lows describe an uptrend, lower highs and lower lows a downtrend, and the labels measure each move. The dashed last leg follows price to its latest extreme and can change, or disappear, until a reversal of the threshold confirms it; the confirmed lines never move. The ZigZag looks back: it describes swings, it does not predict them.
Settings and limitations
Reversal % is the smallest move that counts (1 percent suits intraday stocks; StockCharts uses 5 percent on daily charts). Swing labels and Last leg switch those parts; the line and label colours are appearance settings.
Notes
Rule (StockCharts ChartSchool, ZigZag, high/low version): after a swing high, a higher high replaces it and a low at least Reversal % below it confirms it and starts a swing low; after a swing low, a lower low replaces it and a high at least Reversal % above it confirms it. When one bar does both, the bar extends the current swing (the trend is favoured). The first swing is the first bar's high or low that price then moves Reversal % away from (the earlier one when both happen on the same bar). Labels read the change from the previous swing price (none on the first swing). Completed bars only (live: none). The built-in Zig Zag follows TradingView's depth-and-deviation version instead.
Settings
Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.
| Setting | Type | Default | Range or options | What it does |
|---|---|---|---|---|
| Reversal % | Number | 1 | 0.1 – 50, step 0.1 | Smallest move against the last swing, in percent, that confirms it and starts a new swing. |
| Swing labels | On / off | On | — | Label every confirmed swing with its percentage move from the swing before it. |
| Last leg | On / off | On | — | Draw the unconfirmed leg from the last confirmed swing to the latest extreme, dashed. |
What it draws
Text labels
- Swing highstext · above · colors from Swing (2 colors)
#3FB950 #F85149 - Swing lowstext · below · colors from Swing (2 colors)
#F85149 #3FB950
Line segments
- Confirmed legssolid · 2 px
#58A6FF - Last legdashed · 1.5 px
#58A6FF
Source
Save it as zigzag-segments.js and add it with Indicators → Custom indicators → Add file…. The format is explained in the custom indicators guide.
// Copyright (c) 2026 Spectral
// SPDX-License-Identifier: MIT-0
// Free to use, copy, modify and share: https://spectral.trade/docs/custom-indicators/LICENSE.txt
/** Spectral draws at most 2,000 segments and labels per key: the most recent swings win. */
const MAX_ITEMS = 2000;
/** @type {import('./spectral-indicator').IndicatorV3} */
export default {
schemaVersion: 3,
id: 'zigzag-segments',
name: 'ZigZag Segments',
shortName: 'ZigZag %',
version: '1.0.0',
author: 'Spectral',
description: 'The classic percentage ZigZag: straight lines between swing highs and lows that reverse by at least a threshold percentage, each swing labelled with its percentage move.',
pane: 'price',
live: 'none',
inputs: {
threshold: { type: 'number', label: 'Reversal %', default: 1, min: 0.1, max: 50, step: 0.1, description: 'Smallest move against the last swing, in percent, that confirms it and starts a new swing.' },
showLabels: { type: 'boolean', label: 'Swing labels', default: true, description: 'Label every confirmed swing with its percentage move from the swing before it.' },
showLastLeg: { type: 'boolean', label: 'Last leg', default: true, description: 'Draw the unconfirmed leg from the last confirmed swing to the latest extreme, dashed.' },
},
palettes: {
swing: { label: 'Swing', colors: ['#3FB950', '#F85149'] },
},
segments: {
legs: { label: 'Confirmed legs', color: '#58A6FF', lineWidth: 2 },
lastLeg: { label: 'Last leg', color: '#58A6FF', lineWidth: 1.5, lineStyle: 'dashed' },
},
labels: {
highs: { label: 'Swing highs', color: '#3FB950', palette: 'swing', style: 'none', placement: 'above', textSize: 'small', hideBelowBarWidth: 1, maxVisible: 40 },
lows: { label: 'Swing lows', color: '#F85149', palette: 'swing', style: 'none', placement: 'below', textSize: 'small', hideBelowBarWidth: 1, maxVisible: 40 },
},
docs: {
overview: 'The ZigZag filters out moves smaller than a percentage threshold and joins the remaining swing highs and swing lows with straight lines. A swing is confirmed once price moves the threshold against it; the leg after the last confirmed swing is still open and drawn dashed. Each confirmed swing is labelled with the percentage move from the swing before it.',
howToRead: 'The solid lines show the swings that mattered at your threshold: higher highs and higher lows describe an uptrend, lower highs and lower lows a downtrend, and the labels measure each move. The dashed last leg follows price to its latest extreme and can change, or disappear, until a reversal of the threshold confirms it; the confirmed lines never move. The ZigZag looks back: it describes swings, it does not predict them.',
settings: 'Reversal % is the smallest move that counts (1 percent suits intraday stocks; StockCharts uses 5 percent on daily charts). Swing labels and Last leg switch those parts; the line and label colours are appearance settings.',
notes: 'Rule (StockCharts ChartSchool, ZigZag, high/low version): after a swing high, a higher high replaces it and a low at least Reversal % below it confirms it and starts a swing low; after a swing low, a lower low replaces it and a high at least Reversal % above it confirms it. When one bar does both, the bar extends the current swing (the trend is favoured). The first swing is the first bar\'s high or low that price then moves Reversal % away from (the earlier one when both happen on the same bar). Labels read the change from the previous swing price (none on the first swing). Completed bars only (live: none). The built-in Zig Zag follows TradingView\'s depth-and-deviation version instead.',
},
calculate(ctx) {
const { bars, inputs, format } = ctx;
const up = 1 + inputs.threshold / 100;
const down = 1 - inputs.threshold / 100;
const swings = [];
// direction 0: no swing yet (both extremes tracked); 1: the open swing is a high; -1: it is a low.
let direction = 0;
let high = { index: 0, price: bars.length > 0 ? bars[0].high : 0 };
let low = { index: 0, price: bars.length > 0 ? bars[0].low : 0 };
for (let index = 1; index < bars.length; index += 1) {
const bar = bars[index];
if (direction === 0) {
const fromHigh = bar.low <= high.price * down;
const fromLow = bar.high >= low.price * up;
if (fromHigh && (!fromLow || high.index <= low.index)) {
swings.push({ ...high, isHigh: true });
direction = -1;
low = { index, price: bar.low };
} else if (fromLow) {
swings.push({ ...low, isHigh: false });
direction = 1;
high = { index, price: bar.high };
} else {
if (bar.high > high.price) high = { index, price: bar.high };
if (bar.low < low.price) low = { index, price: bar.low };
}
} else if (direction === 1) {
if (bar.high > high.price) high = { index, price: bar.high };
else if (bar.low <= high.price * down) {
swings.push({ ...high, isHigh: true });
direction = -1;
low = { index, price: bar.low };
}
} else if (bar.low < low.price) {
low = { index, price: bar.low };
} else if (bar.high >= low.price * up) {
swings.push({ ...low, isHigh: false });
direction = 1;
high = { index, price: bar.high };
}
}
const legs = [];
const highs = [];
const lows = [];
for (let k = 0; k < swings.length; k += 1) {
const swing = swings[k];
if (k > 0) legs.push({ x1: swings[k - 1].index, y1: swings[k - 1].price, x2: swing.index, y2: swing.price });
if (!inputs.showLabels || k === 0) continue;
const change = swing.price / swings[k - 1].price - 1;
const text = `${change >= 0 ? '+' : ''}${format.percent(change)}`;
(swing.isHigh ? highs : lows).push({ index: swing.index, value: swing.price, text, palette: swing.isHigh ? 0 : 1 });
}
const lastLeg = [];
if (inputs.showLastLeg && swings.length > 0) {
const last = swings[swings.length - 1];
const open = last.isHigh ? low : high;
if (open.index > last.index) lastLeg.push({ x1: last.index, y1: last.price, x2: open.index, y2: open.price });
}
return {
segments: { legs: legs.slice(-MAX_ITEMS), lastLeg },
labels: { highs: highs.slice(-MAX_ITEMS), lows: lows.slice(-MAX_ITEMS) },
};
},
};Adapt it with an LLM
Paste this into your assistant and replace the second-to-last line with the change you want.
Start from this Spectral custom indicator: https://spectral.trade/indicators/zigzag-segments.js Format reference: https://spectral.trade/docs/custom-indicators.llm.md Keep the file format and every rule of the reference; give the result a new id, name, shortName and version, and update docs and input descriptions to match. Keep its live updates exact and fast (section "Fast live updates" of the reference): carry values from bar to bar only through ctx.ta, and raise live.lookback if the change reads further back; Spectral rejects a file whose live updates differ from a full recalculation or are too slow. Change I want: <describe the change> Answer with the complete file in one code block, ready to save as <id>.js.


