PINE LIBRARY
Updated

ZigZag ATR

698
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:

  • 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

  1. ATR Length — Period for ATR calculation (default: 14)
  2. ATR Multiplier — How many ATRs price must move to confirm a new pivot (default: 2.0)
  3. Depth — Bars required for pivot detection (default: 10)
  4. Extend to Last Bar — Draw provisional line to current price
  5. Display options — Toggle price, volume, and change labels


How to Use

Pine Script®
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.
Release Notes
v2
Update 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

Pine Script®
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.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.