PINE LIBRARY
Aggiornato ZigZag ATR

ZigZag ATR Library
A volatility-adaptive ZigZag indicator that uses Average True Range (ATR) instead of fixed percentage deviation to detect pivot points. This makes the ZigZag dynamically adjust to market conditions — tighter during low volatility, wider during high volatility.
Why ATR instead of Percentage?
The standard ZigZag uses a fixed percentage threshold (e.g., 5%) to determine when price has reversed enough to form a new pivot. This approach has limitations:
ATR-based deviation solves these issues by measuring reversals in terms of actual volatility, not arbitrary percentages.
Key Features
Settings
How to Use
Pine Script®
Exported Types
Exported Functions
Recommended Multiplier Values
Based on TradingView's official ZigZag library, modified to use ATR-based deviation threshold.
A volatility-adaptive ZigZag indicator that uses Average True Range (ATR) instead of fixed percentage deviation to detect pivot points. This makes the ZigZag dynamically adjust to market conditions — tighter during low volatility, wider during high volatility.
Why ATR instead of Percentage?
The standard ZigZag uses a fixed percentage threshold (e.g., 5%) to determine when price has reversed enough to form a new pivot. This approach has limitations:
- A 5% move means very different things for a $10 stock vs a $500 stock
- During high volatility, fixed percentages create too many pivots (noise)
- During low volatility, fixed percentages may miss significant structure
ATR-based deviation solves these issues by measuring reversals in terms of actual volatility, not arbitrary percentages.
Key Features
- Volatility-adaptive pivot detection using ATR × multiplier threshold
- Automatic adjustment to changing market conditions
- Full customization of ATR length and multiplier
- Optional line extension to current price
- Pivot labels showing price, volume, and price change
- Clean library structure for easy integration
Settings
- ATR Length — Period for ATR calculation (default: 14)
- ATR Multiplier — How many ATRs price must move to confirm a new pivot (default: 2.0)
- Depth — Bars required for pivot detection (default: 10)
- Extend to Last Bar — Draw provisional line to current price
- Display options — Toggle price, volume, and change labels
How to Use
import YourUsername/ZigZagATR/1 as zz
// Create settings
var zz.Settings settings = zz.Settings.new(
14, // ATR length
2.0, // ATR multiplier
10 // Depth
)
// Create ZigZag instance
var zz.ZigZag zigZag = zz.newInstance(settings)
// Calculate ATR and update on each bar
float atrValue = ta.atr(14)
zigZag.update(atrValue)
Exported Types
- Settings — Configuration for calculation and display
- Pivot — Stores pivot point data, lines, and labels
- ZigZag — Main object maintaining state and pivot history
Exported Functions
- newInstance(settings) — Creates a new ZigZag object
- update(atrValue) — Updates the ZigZag with current ATR (call once per bar)
- lastPivot() — Returns the most recent pivot point
Recommended Multiplier Values
1.0 - 1.5 → More sensitive, more pivots, better for scalping
2.0 - 2.5 → Balanced, good for swing trading (default)
3.0+ → Less sensitive, major pivots only, better for position trading
Based on TradingView's official ZigZag library, modified to use ATR-based deviation threshold.
Note di rilascio
v2Update v2: Added xATR Display Mode
What's New
Added a new display mode for the Reversal Price Change label: ATR multiples.
You can now see each swing's magnitude expressed as a multiple of ATR (e.g., "+2.35x ATR"), giving you instant insight into the significance of each move relative to current volatility.
New Display Modes
The differencePriceMode setting now accepts three options:
- Absolute — Raw price difference (e.g., "+$5.25")
- Percent — Percentage change (e.g., "+2.5%")
- ATR — ATR multiples (e.g., "+2.35x ATR") ← NEW
Why xATR?
- A 3x ATR move is significant regardless of absolute price or percentage
- Instantly compare swing magnitudes across different volatility regimes
- Better context for position sizing and stop placement
- Natural complement to ATR-based pivot detection
Usage Example
var zz.Settings settings = zz.Settings.new(
14, // ATR length
2.0, // ATR multiplier
10, // Depth
color.blue,
true, // Extend to last bar
true, // Display reversal price
true, // Display cumulative volume
true, // Display reversal price change
"ATR" // ← Set to "ATR" for xATR display
)
Default Changed
The default differencePriceMode is now "ATR" instead of "Absolute" to better align with the ATR-based nature of this library.
Backward compatible — existing code using "Absolute" or "Percent" modes continues to work unchanged.
Libreria Pine
Nello spirito di TradingView, l'autore ha pubblicato questo codice Pine come libreria open source affinché altri programmatori della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open source, ma il riutilizzo di questo codice nelle pubblicazioni è soggetto al Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Libreria Pine
Nello spirito di TradingView, l'autore ha pubblicato questo codice Pine come libreria open source affinché altri programmatori della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open source, ma il riutilizzo di questo codice nelle pubblicazioni è soggetto al Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.