PINE LIBRARY
Aggiornato

Absolute ZigZag Lib

5 989
Library "Absolute_ZigZag_Lib"

This ZigZag Library is a Bit different. Instead of using percentages or looking more than 1 bar left or right, this Zigzag library calculates pivots by just looking at the current bar highs and lows and the ones of one bar earlier.
This is the most accurate way of calculating pivots and it also eliminates lag.

The library also features a solution for bars that have both a higher high and a higher low like seen below.
https://www.tradingview.com/x/NbrUz34F/

You can also use your own colors for the labels and the lines.
istantanea

You can also quickly select a one-colored theme without changing all colors at once
istantanea


method isHigherHigh(this)
  Checks if current pivot is a higher high
  Namespace types: Pivot
  Parameters:
    this (Pivot): (Pivot) The object to work with.
return (bool) True if the pivot is a higher high, false if not.

method isLowerHigh(this)
  Checks if current pivot is a lower high
  Namespace types: Pivot
  Parameters:
    this (Pivot): (Pivot) The object to work with.
return (bool) True if the pivot is a lower high, false if not.

method isHigherLow(this)
  Checks if current pivot is a higher low
  Namespace types: Pivot
  Parameters:
    this (Pivot): (Pivot) The object to work with.
return (bool) True if the pivot is a higher low, false if not.

method isLowerLow(this)
  Checks if current pivot is a lower low
  Namespace types: Pivot
  Parameters:
    this (Pivot): (Pivot) The object to work with.
return (bool) True if the pivot is a lower low, false if not.

method getLastPivotHigh(this)
  Gets the last Pivot High
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The latest Pivot High

method getLastPivotLow(this)
  Gets the last Pivot Low
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The latest Pivot Low

method prev(this, index)
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[])
    index (int)

method last(this, throwError)
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[])
    throwError (bool)

new(highFirst, theme)
  Parameters:
    highFirst (bool)
    theme (Theme)

getLowerTimeframePeriod()

Theme
  Used to create a (color) theme to draw Zigzag
  Fields:
    colorDefault (series color)
    colorNeutral (series color)
    colorBullish (series color)
    colorBearish (series color)
    coloredLines (series bool)

Point
  Used to determine a coordination on the chart
  Fields:
    x (series int)
    y (series float)

Pivot
  Used to determine pivots on the chart
  Fields:
    point (Point)
    isHigh (series bool)
    isHigher (series bool)
    ln (series line)
    lb (series label)
Note di rilascio
v2: Fixes bug that would draw wrong color
Note di rilascio
v3 | Minor (commenting) updates and fixes in script. The new() function now returns the pivots array.
Note di rilascio
v4 | Major overhaul of functions and added extra functions to get latest and previous pivot high / low

Added:
method first(this)
  Gets the first Pivot
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The first Pivot in the array or NA if not available.

method getHigh(this, prev)
  Gets the latest Pivot High based on prev number Pivot high back. Default the latest
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
    prev (int): (int) Number of prev pivot highs back. (1 = latest, 1 < is previous ones).
return (Pivot) The latest Pivot High or NA if not available.

method getLastHigh(this)
  Gets the latest pivot high
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The latest Pivot high in the array

method getPrevHigh(this)
  Gets the previous pivot high
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The previous Pivot high in the array or NA if not available

method getLow(this, prev)
  Gets the Pivot Low
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
    prev (int)

method getLastLow(this)
  Gets the latest pivot low
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The latest Pivot low in the array or NA if not available

method getPrevLow(this)
  Gets the previous pivot low
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The previous Pivot low in the array or NA if not available

Updated:
method last(this)
  Gets previous Pivot
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The last Pivot in the array or NA if not available.

method prev(this)
  Gets previous Pivot
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The previous Pivot or NA if not available.

Removed:
method getLastPivotHigh(this)
  Gets the last Pivot High

method getLastPivotLow(this)
  Gets the last Pivot Low
Note di rilascio
v5 | In this update, I've added new features and changed some array methods to avoid confusion. The library no longer overrides the get(), last(), first() functions for the array<Pivot> object.
Added new Features:
  • Now let's you select custom source
  • Now keeps track of current close price of pivot
  • Can disable/enable the feature to show close price info of pivots


Added:
method getFirst(this)
  Gets the first Pivot
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The first Pivot in the array or NA if not available.

method getLast(this)
  Gets previous Pivot
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The last Pivot in the array or NA if not available.

method getPrev(this, index)
  Gets previous Pivot by index number.
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
    index (int): (int) The index number. (optional, default is 1)
return (Pivot) The previous Pivot or NA if not available.

method barIsPivot(pivots)
  Namespace types: Pivot[]
  Parameters:
    pivots (Pivot[])

Updated:
method getLastLow(this)
  Gets the latest pivot low
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The latest Pivot low in the array or NA if not available

method getPrevLow(this)
  Gets the previous pivot low
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The previous Pivot low in the array or NA if not available

new(sourceHigh, sourceLow, highFirst, theme)
  Parameters:
    sourceHigh (float)
    sourceLow (float)
    highFirst (bool)
    theme (Theme)

Theme
  Used to create a (color) theme to draw Zigzag
  Fields:
    colorDefault (series color)
    colorNeutral (series color)
    colorBullish (series color)
    colorBearish (series color)
    coloredLines (series bool)
    showCloseInfo (series bool)

Pivot
  Used to determine pivots on the chart
  Fields:
    point (Point)
    isHigh (series bool)
    isHigher (series bool)
    close (series float)
    closeAbovePrevPivot (series bool)
    closeAbovePrevPivotClose (series bool)
    ln (series line)
    lb (series label)

Removed:
method first(this)
  Gets the first Pivot

method last(this)
  Gets previous Pivot

method prev(this)
  Gets previous Pivot
Note di rilascio
v6 | Fixed a bug at pivot lows, that would draw lines from high instead of low
Note di rilascio
v7 | This update brings:
  • Enable/disable enable features
  • Tooltips with all available data for all pivots. Great for feedback on mouse-hover. You can customize the what is shown or hidden.



Added:
method isAtCurrentBar(this)
  Checks if the current pivot is at the current bar
  Namespace types: Pivot
  Parameters:
    this (Pivot): (Pivot) The object to work with.
return (bool) Wether the current pivot is at the current bar or not.

Source
  The source to use
  Fields:
    high (series float)
    low (series float)
    priority (series string)

Features
  Features to enable
  Fields:
    closeBreaksClose (series bool)
    closeBreaksPivot (series bool)

Tooltips
  Tooltips to show
  Fields:
    name (series bool)
    price (series bool)
    close (series bool)
    closeBreaksClose (series bool)
    closeBreaksPivot (series bool)

Settings
  All settings for the indicator
  Fields:
    source (Source)
    features (Features)
    theme (Theme)

Updated:
method getLast(this)
  Gets the latest Pivot
  Namespace types: Pivot[]
  Parameters:
    this (Pivot[]): (array<Pivot>) The object to work with.
return (Pivot) The last Pivot in the array or NA if not available.

new(customSettings)
  Creates a new ZigZag Instance
  Parameters:
    customSettings (Settings): (Settings) The settings for this instance.

getLowerTimeframePeriod()
  Helper function to generate a lower timeframe period (string) for current timeframe period.
You can use this on conjuction with request.security_lower_tf() to figure out if current high of
of this bar was created before the low of this bar.
return (string) Timeframe period for lower timeframe.

Theme
  Used to create a (color) theme to draw Zigzag
  Fields:
    colorDefault (series color)
    colorNeutral (series color)
    colorBullish (series color)
    colorBearish (series color)
    coloredLines (series bool)
    showCloseInfo (series bool)
    showTooltips (series bool)
    tooltips (Tooltips)

Pivot
  Used to determine pivots on the chart.
  Fields:
    point (Point)
    isHigh (series bool)
    isHigher (series bool)
    name (series string)
    abbr (series string)
    close (series float)
    comulativeVolume (series float)
    closeBreaksPivot (series bool)
    closeBreaksClose (series bool)
    isLast (series bool)
    ln (series line)
    lb (series label)
Note di rilascio
v8 | You can now customize the size of the pivot text and the thickness of the lines.

Updated:
Theme
  Used to create a (color) theme to draw Zigzag
  Fields:
    colorDefault (series color)
    colorNeutral (series color)
    colorBullish (series color)
    colorBearish (series color)
    labelSize (series string)
    lineWidth (series int)
    coloredLines (series bool)
    showCloseInfo (series bool)
    showTooltips (series bool)
    tooltips (Tooltips)
Note di rilascio
v9 | Fixed bug where pivot lows checked close breaks close, and close breaks pivot wrong way around.
Note di rilascio
v10 | Can now enable/disable the build-in drawings. You can set theme.enable to false if you want to build your own lines / labels.

Updated:
Theme
  Used to create a (color) theme to draw Zigzag
  Fields:
    enabled (series bool)
    colorDefault (series color)
    colorNeutral (series color)
    colorBullish (series color)
    colorBearish (series color)
    labelSize (series string)
    lineWidth (series int)
    coloredLines (series bool)
    showCloseInfo (series bool)
    showTooltips (series bool)
    tooltips (Tooltips)
Note di rilascio
v11 added support for custom maximum amount of pivots

Updated:
Settings
  All settings for the indicator
  Fields:
    source (Source)
    features (Features)
    theme (Theme)
    limit (series int)
Note di rilascio
v12

Fixed 1m chart error caused by users that have no support for seconds charts due to lower tier or free subscription plan
Note di rilascio
v13
Summary:
Updated to Pine v6


Updated:
method getPrev(this, index)
  Gets previous Pivot by index number.
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
    index (int): (int) The index number (optional, default is 1)
return (Pivot) The previous Pivot or NA if not available.

method isAtCurrentBar(this)
  Checks if the current pivot is at the current bar
  Namespace types: Pivot
  Parameters:
    this (Pivot): (Pivot) The object to work with.
return (bool) Whether the current pivot is at the current bar or not.

method getHigh(this, prev)
  Gets the latest Pivot High based on previous number of Pivot highs back. Default is the latest.
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
    prev (int): (int) Number of previous pivot highs back (1 = latest, >1 is earlier ones).
return (Pivot) The latest Pivot High or NA if not available.

method getLastHigh(this)
  Gets the latest pivot high
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
return (Pivot) The latest Pivot high in the array or NA if not available.

method getPrevHigh(this)
  Gets the previous pivot high
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
return (Pivot) The previous Pivot high in the array or NA if not available.

method getLow(this, prev)
  Gets the Pivot Low
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
    prev (int): (int) Number of previous pivot lows back (1 = latest, >1 is earlier ones).
return (Pivot) The latest Pivot Low or NA if not available.

method getLastLow(this)
  Gets the latest pivot low
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
return (Pivot) The latest Pivot low in the array or NA if not available.

method getPrevLow(this)
  Gets the previous pivot low
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
return (Pivot) The previous Pivot low in the array or NA if not available.

new(customSettings)
  Creates a new ZigZag Instance
  Parameters:
    customSettings (Settings): (Settings) The settings for this instance.
return (array<Pivot>) The array of pivots.

getLowerTimeframePeriod()
  Helper function to generate a lower timeframe period (string) for current timeframe period.
You can use this in conjunction with request.security_lower_tf() to figure out if current high
of this bar was created before the low of this bar.
return (string) Timeframe period for lower timeframe.
Note di rilascio
v14
Update: export add method
Added:
method add(this, point, isHigh, settings)
  Adds a new pivot to the pivots array.
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
    point (Point): (Point) The point coordinates of the new pivot.
    isHigh (bool): (bool) Whether the pivot is a high or not (then it is a low).
    settings (Settings): (Settings) The global settings.
return (Pivot) The latest Pivot.
Note di rilascio
v15

Update Summary:
- Feature: A new feature is added so that you can choose to ignore inside candles in the calculation of new pivots. This will filter out a lot of useless noise. This feature is ON by default.
- Bugfix: labeling and pivot calculation bugs that creeped into the code during the previous few updates.
- Bugfix: Fixed a bug where pivot scanning was ignored when previous pivot price was equal.

Hope you enjoy this new version!


Updated Code summary:
method getFirst(this)
  Gets the first Pivot
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)

method getLast(this)
  Gets the latest Pivot
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)

method getPrev(this, index)
  Gets previous Pivot by index number.
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)
    index (int)

method isAtCurrentBar(this)
  Checks if the current pivot is at the current bar
  Namespace types: Pivot
  Parameters:
    this (Pivot)

method isHigherHigh(this)
  Checks if current pivot is a higher high
  Namespace types: Pivot
  Parameters:
    this (Pivot)

method isLowerHigh(this)
  Checks if current pivot is a lower high
  Namespace types: Pivot
  Parameters:
    this (Pivot)

method isHigherLow(this)
  Checks if current pivot is a higher low
  Namespace types: Pivot
  Parameters:
    this (Pivot)

method isLowerLow(this)
  Checks if current pivot is a lower low
  Namespace types: Pivot
  Parameters:
    this (Pivot)

method getHigh(this, prev)
  Gets the latest Pivot High based on previous number of Pivot highs back.
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)
    prev (int)

method getLastHigh(this)
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)

method getPrevHigh(this)
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)

method getLow(this, prev)
  Gets the Pivot Low
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)
    prev (int)

method getLastLow(this)
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)

method getPrevLow(this)
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)

method add(this, point, isHigh, settings)
  Adds a new pivot to the pivots array.
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>)
    point (Point)
    isHigh (bool)
    settings (Settings)

method barIsPivot(this)
  Checks if the current bar is the latest pivot.
  Namespace types: array<Pivot>
  Parameters:
    this (array<Pivot>): (array<Pivot>) The object to work with.
return (bool) True if the current bar matches the latest pivot's time.

new(customSettings)
  Creates a new ZigZag Instance
  Parameters:
    customSettings (Settings)

getLowerTimeframePeriod()

Features
  Features to enable
  Fields:
    closeBreaksClose (series bool)
    closeBreaksPivot (series bool)
    ignoreInsideBars (series bool)

Theme
  Theme
  Fields:
    enabled (series bool)
    colorDefault (series color)
    colorNeutral (series color)
    colorBullish (series color)
    colorBearish (series color)
    lineWidth (series int)
    labelSize (series string)
    showLabelL (series bool)
    showLabelLL (series bool)
    showLabelHL (series bool)
    showLabelH (series bool)
    showLabelLH (series bool)
    showLabelHH (series bool)
    coloredLines (series bool)
    showCloseInfo (series bool)
    showTooltips (series bool)
    tooltips (Tooltips)

Pivot
  Used to determine pivots on the chart.
  Fields:
    point (Point)
    isHigh (series bool)
    isHigher (series bool)
    hasPrev (series bool)
    name (series string)
    abbr (series string)
    close (series float)
    cumulativeVolume (series float)
    closeBreaksPivot (series bool)
    closeBreaksClose (series bool)
    isLast (series bool)
    ln (series line)
    lb (series label)
Note di rilascio
v16

Ignoring Inside Bars is now disabled by default
Note di rilascio
v17: Full Documentation, ATR Logic & Structural Stability
This update marks a major maturity milestone for the Absolute ZigZag Library. We have introduced advanced logic to handle volatility anomalies and finalized the internal documentation for ease of use.
New Features
  1. ATR Anomaly Filter: Added atr to the Source and atrThreshold to Features. This allows the script to detect "Black Swan" or "Anomaly" candles. When a candle's range exceeds the ATR threshold, the "Ignore Inside Bars" rule is temporarily suspended, preventing the ZigZag from freezing during massive volatility events.
  2. Docstrings: Added comprehensive Pine Script documentation (Function, param, return) to all exported methods and types. Tooltips will now appear correctly when using this library in other scripts.

Logic Improvements & Fixes
  • Paired Pivot Comparison: Improved the add() method. New pivots are now immediately compared against their paired pivot (High vs. previous High, Low vs. previous Low) rather than just the immediate previous pivot. This ensures isHigherHigh / isLowerLow states are accurate from the very moment of creation, fixing rare edge-case coloring issues on V-reversals.
  • Usage Example: Updated the internal usage example to include the new UI grouping, Outside Bar priority logic, and ATR settings.
Note di rilascio
Version 18: Market Structure & Leg Dynamics Engine

This major update now captures the effort (volume) and time behind every move, specifically designed to empower SMC and Price Action indicators.

New Feature: "Leg Statistics" & Pressure Analysis
I have introduced a high-performance "Cumulative Snapshot" engine to calculate statistics for every zigzag leg without performance penalty.
  • Leg Volume & Delta: The library now calculates the total volume, buy volume, and sell volume for every leg (from Previous Low to Current High, etc.).
  • Smart Pressure Logic: Instead of simple "Green/Red" candle checks, the library now uses Wick-Adjusted Pressure to calculate Delta. It analyzes the position of the Close relative to the High/Low range to accurately estimate buying vs. selling pressure within every bar of the leg.
  • Duration: Accurate tracking of Time (milliseconds) and Bar Count per leg.


Structural Logic Improvements
  • Persistent "Close Break" (The Latch): Previously, structure breaks were determined by the final pivot candle. Now, the library uses "Latch Logic." If any candle during the formation of a leg closes beyond the previous structure, the break is recorded and remembered, ensuring valid BOS events are never lost due to retracements.
  • Breakout Timestamp: Added `pivot.breakBarTime` to record exactly when a structure break occurred.


Technical & QOL Updates
  • Full Candle Data: The `Pivot` object now carries the `open`, `high`, `low`, `vol` of the extreme pivot candle.
  • Granular Tooltips: Users can now individually toggle Leg Volume, Delta, Buy/Sell Vol, Bars, and Time in the settings.
  • Smart Time Formatting: Durations are now displayed in human-readable formats (e.g., "4h 30m", "2d") rather than raw seconds.
Note di rilascio
v19

Absolute ZigZag Library Update

This update brings significant improvements to noise filtering and pivot detection logic, specifically targeting complex "Outside Bar" (Engulfing) scenarios and "Inside Bar" edge cases.

New Features:
  • Advanced Outside Bar Filtering: Added a smart "Continuation Priority" logic. When filtering is enabled, the library now intelligently determines whether an Outside Bar represents a trend continuation or noise based on the intrabar sequence (High then Low vs Low then High). This prevents valid trend extensions from being filtered out.
  • Sentiment Override: New optional feature allowSentimentOverride. If enabled, the Outside Bar filter is aborted if the candle's close color opposes the trend (e.g., a Bullish Outside Bar in a Downtrend). This ensures significant reversal candles are captured immediately.
  • Local Outside Bar Override: New optional feature allowLocalOutside. This solves the edge case where a volatility expansion bar is technically "Inside" a massive Mother Bar but engulfs the immediate previous candle. This allows the ZigZag to react to local volatility even during consolidation.
  • Precise Edge-Case Statistics: Fixed an issue where pivots formed on the same bar (Outside Bars) showed 0 volume/time. They now correctly reflect the statistics of that specific bar.


Improvements:
  • Standardized naming conventions across the library (camelCase).
  • Improved tooltips and documentation for all exported types.
  • Optimized main loop performance.


Further notes:
The library expanded quite a lot since it's first creation. You can still get the exact same results as when this library version 1 came out: Just disable all the filters and features and you can set that configuration as the default template to use.

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.