PINE LIBRARY
Actualizado 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
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.
Notas de prensa
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
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.
Biblioteca Pine
Fiel al espíritu de TradingView, el autor ha publicado este código de Pine como biblioteca de código abierto, para que otros programadores de nuestra comunidad puedan reutilizarlo. ¡Enhorabuena al autor! Puede usar esta biblioteca de forma privada o en otras publicaciones de código abierto, pero su reutilización en publicaciones está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.
Biblioteca Pine
Fiel al espíritu de TradingView, el autor ha publicado este código de Pine como biblioteca de código abierto, para que otros programadores de nuestra comunidad puedan reutilizarlo. ¡Enhorabuena al autor! Puede usar esta biblioteca de forma privada o en otras publicaciones de código abierto, pero su reutilización en publicaciones está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.