PINE LIBRARY
ZigZag ATR Pct

ZigZag ATR % Library
A PineScript v6 library for detecting price pivots based on ATR percentage change (volatility shifts) rather than fixed ATR multiples.
How It Works
Traditional ZigZag indicators use a fixed price threshold to detect pivots. This library takes a different approach: pivots are detected when volatility is changing significantly.
The ATR % change measures how much the Average True Range has shifted over a lookback period:
Pine Script®
Pivots form when |ATR %| exceeds your threshold, capturing turning points during volatility transitions.
Exported Types
Exported Functions
Usage Example
Pine Script®
Parameters
Use Cases
A PineScript v6 library for detecting price pivots based on ATR percentage change (volatility shifts) rather than fixed ATR multiples.
How It Works
Traditional ZigZag indicators use a fixed price threshold to detect pivots. This library takes a different approach: pivots are detected when volatility is changing significantly.
The ATR % change measures how much the Average True Range has shifted over a lookback period:
atrPct = 100 * (atr / atr[lookback] - 1)
- Positive ATR % = Volatility expanding (market becoming more volatile)
- Negative ATR % = Volatility contracting (market calming down)
Pivots form when |ATR %| exceeds your threshold, capturing turning points during volatility transitions.
Exported Types
- Settings - Configuration (ATR length, lookback, threshold, display options)
- Pivot - Pivot point data (price, time, direction, volume, ATR %)
- ZigZag - Main state container
Exported Functions
- newInstance(settings) - Create a new ZigZag instance
- update(zz, atr, atrPct) - Update on each bar
- getLastPivot(zz) - Get the most recent pivot
- getPivot(zz, index) - Get pivot at specific index
- getPivotCount(zz) - Get total number of pivots
- calcTR() - Calculate True Range
- calcATR(length) - Calculate ATR using EMA
- calcATRPct(atr, atrPrev) - Calculate ATR % change
- calcPricePct(startPrice, endPrice) - Calculate price % change
Usage Example
//@version=6
indicator("My ZigZag", overlay = true)
import DeepEntropy/ZigZagATRPct/1 as zz
// Settings
var zz.Settings settings = zz.Settings.new(
atrLength = 14,
atrLookback = 14,
atrPctThreshold = 5.0,
depth = 10
)
var zz.ZigZag zigZag = zz.newInstance(settings)
// Calculate ATR %
float atr = zz.calcATR(14)
float atrPct = zz.calcATRPct(atr, atr[14])
// Update
zigZag := zz.update(zigZag, atr, atrPct)
// Access pivots
int count = zz.getPivotCount(zigZag)
if count > 0
zz.Pivot last = zz.getLastPivot(zigZag)
label.new(last.point, text = str.tostring(last.atrPct, "#.##") + "%")
Parameters
- ATR Length - Period for ATR calculation (default: 14)
- ATR Lookback - Bars to look back for ATR % change (default: 14)
- ATR % Threshold - Minimum |ATR %| to trigger pivot detection (default: 5.0)
- Depth - Minimum bars between pivots (default: 10)
Use Cases
- Identify reversals during volatility regime changes
- Filter noise during low-volatility consolidation
- Detect breakout pivots when volatility expands
- Build volatility-aware trading systems
This library detects when the market's behavior is changing, not just how much price has moved.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
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.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
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.