Indicator · Pine Script v5 · On the chart

Multi ZigZag

by WISEPLAT· code credited to HeWhoMustNotBeNamed

This indicator calculates and plots up to four independent ZigZag lines on the price chart simultaneously using customizable length periods.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code204
Settings16inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago20 Jun 2023

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

204 lines of Pine Script v5 from ZigZag - ishod/Multi ZigZag - 8.pine.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Multi ZigZag - 8.pine.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __ 
//  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
//  $$ |  $$ |  ______  $$ | / \ $$ |$$ |____    ______  $$  \   /$$ | __    __   _______  _$$ |_   $$  \ $$ |  ______   _$$ |_   $$$$$$$  |  ______  $$  \ $$ |  ______   _____  ____    ______    ____$$ |
//  $$ |__$$ | /      \ $$ |/$  \$$ |$$      \  /      \ $$$  \ /$$$ |/  |  /  | /       |/ $$   |  $$$  \$$ | /      \ / $$   |  $$ |__$$ | /      \ $$$  \$$ | /      \ /     \/    \  /      \  /    $$ |
//  $$    $$ |/$$$$$$  |$$ /$$$  $$ |$$$$$$$  |/$$$$$$  |$$$$  /$$$$ |$$ |  $$ |/$$$$$$$/ $$$$$$/   $$$$  $$ |/$$$$$$  |$$$$$$/   $$    $$< /$$$$$$  |$$$$  $$ | $$$$$$  |$$$$$$ $$$$  |/$$$$$$  |/$$$$$$$ |
//  $$$$$$$$ |$$    $$ |$$ $$/$$ $$ |$$ |  $$ |$$ |  $$ |$$ $$ $$/$$ |$$ |  $$ |$$      \   $$ | __ $$ $$ $$ |$$ |  $$ |  $$ | __ $$$$$$$  |$$    $$ |$$ $$ $$ | /    $$ |$$ | $$ | $$ |$$    $$ |$$ |  $$ |
//  $$ |  $$ |$$$$$$$$/ $$$$/  $$$$ |$$ |  $$ |$$ \__$$ |$$ |$$$/ $$ |$$ \__$$ | $$$$$$  |  $$ |/  |$$ |$$$$ |$$ \__$$ |  $$ |/  |$$ |__$$ |$$$$$$$$/ $$ |$$$$ |/$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ \__$$ |
//  $$ |  $$ |$$       |$$$/    $$$ |$$ |  $$ |$$    $$/ $$ | $/  $$ |$$    $$/ /     $$/   $$  $$/ $$ | $$$ |$$    $$/   $$  $$/ $$    $$/ $$       |$$ | $$$ |$$    $$ |$$ | $$ | $$ |$$       |$$    $$ |
//  $$/   $$/  $$$$$$$/ $$/      $$/ $$/   $$/  $$$$$$/  $$/      $$/  $$$$$$/  $$$$$$$/     $$$$/  $$/   $$/  $$$$$$/     $$$$/  $$$$$$$/   $$$$$$$/ $$/   $$/  $$$$$$$/ $$/  $$/  $$/  $$$$$$$/  $$$$$$$/ 
//                                                                                                                                                                                                          
//                                                                                                                                                                                                          
//
//@version=5
indicator('Multi ZigZag', shorttitle='MZigZag', overlay=true, max_bars_back=1000, max_lines_count=500, max_labels_count=500)

useAlternativeSource = input.bool(false, title='', group='Source', inline='src')
source = input.source(close, title='Alternative Source', group='Source', inline='src')

showStatTable = input.bool(true, title='Show Stats', group='Misc', inline='stats')
max_array_size = input.int(20, step=20, title='', group='Misc', inline='stats')

showZigZag1 = input.bool(true, title='L1 Length', group='Zigzag', inline='main')
zigzag1Length = input.int(5, step=1, minval=3, title='', group='Zigzag', inline='main')
zigzag1Color = input.color(color.rgb(66, 165, 245, 0), title='', group='Zigzag', inline='main')

showZigZag2 = input.bool(true, title='L2 Length', group='Zigzag', inline='sub1')
zigzag2Length = input.int(8, step=1, minval=2, title='', group='Zigzag', inline='sub1')
zigzag2Color = input.color(color.rgb(153, 204, 102, 0), title='', group='Zigzag', inline='sub1')

showZigZag3 = input.bool(true, title='L3 Length', group='Zigzag', inline='sub2')
zigzag3Length = input.int(13, step=1, minval=2, title='', group='Zigzag', inline='sub2')
zigzag3Color = input.color(color.rgb(251, 192, 45, 0), title='', group='Zigzag', inline='sub2')

showZigZag4 = input.bool(true, title='L4 Length', group='Zigzag', inline='sub3')
zigzag4Length = input.int(21, step=1, minval=2, title='', group='Zigzag', inline='sub3')
zigzag4Color = input.color(color.rgb(244, 143, 177, 0), title='', group='Zigzag', inline='sub3')


var zigzagpivots1 = array.new_float(0)
var zigzagpivotbars1 = array.new_int(0)
var zigzagpivotdirs1 = array.new_int(0)

var zigzagpivots2 = array.new_float(0)
var zigzagpivotbars2 = array.new_int(0)
var zigzagpivotdirs2 = array.new_int(0)

var zigzagpivots3 = array.new_float(0)
var zigzagpivotbars3 = array.new_int(0)
var zigzagpivotdirs3 = array.new_int(0)

var zigzagpivots4 = array.new_float(0)
var zigzagpivotbars4 = array.new_int(0)
var zigzagpivotdirs4 = array.new_int(0)

var zigzag1lines = array.new_line(0)
var zigzag2lines = array.new_line(0)
var zigzag3lines = array.new_line(0)
var zigzag4lines = array.new_line(0)

add_to_array(arr, val, maxItems) =>
    array.unshift(arr, val)
    if array.size(arr) > maxItems
        array.pop(arr)

pivots(length) =>
    highsource = useAlternativeSource ? source : high
    lowsource = useAlternativeSource ? source : low
    float phigh = ta.highestbars(highsource, length) == 0 ? highsource : na
    float plow = ta.lowestbars(lowsource, length) == 0 ? lowsource : na
    [phigh, plow, bar_index, bar_index]

addnewpivot(zigzagpivots, zigzagpivotbars, zigzagpivotdirs, value, bar, dir) =>
    newDir = dir
    if array.size(zigzagpivots) >= 2
        LastPoint = array.get(zigzagpivots, 1)
        newDir := dir * value > dir * LastPoint ? dir * 2 : dir
        newDir
    add_to_array(zigzagpivots, value, max_array_size)
    add_to_array(zigzagpivotbars, bar, max_array_size)
    add_to_array(zigzagpivotdirs, newDir, max_array_size)

zigzagcore(phigh, plow, phighbar, plowbar, zigzagpivots, zigzagpivotbars, zigzagpivotdirs) =>
    pDir = 1
    newZG = false
    doubleZG = phigh and plow
    if array.size(zigzagpivots) >= 1
        pDir := array.get(zigzagpivotdirs, 0)
        pDir := pDir % 2 == 0 ? pDir / 2 : pDir
        pDir

    if (pDir == 1 and phigh or pDir == -1 and plow) and array.size(zigzagpivots) >= 1
        pivot = array.shift(zigzagpivots)
        pivotbar = array.shift(zigzagpivotbars)
        pivotdir = array.shift(zigzagpivotdirs)
        value = pDir == 1 ? phigh : plow
        bar = pDir == 1 ? phighbar : plowbar

        useNewValues = value * pivotdir > pivot * pivotdir
        value := useNewValues ? value : pivot
        bar := useNewValues ? bar : pivotbar
        newZG := newZG or useNewValues
        addnewpivot(zigzagpivots, zigzagpivotbars, zigzagpivotdirs, value, bar, pDir)

    if pDir == 1 and plow or pDir == -1 and phigh
        value = pDir == 1 ? plow : phigh
        bar = pDir == 1 ? plowbar : phighbar
        dir = pDir == 1 ? -1 : 1
        newZG := true
        addnewpivot(zigzagpivots, zigzagpivotbars, zigzagpivotdirs, value, bar, dir)

    [newZG, doubleZG]

zigzag(length, zigzagpivots, zigzagpivotbars, zigzagpivotdirs) =>
    [phigh, plow, phighbar, plowbar] = pivots(length)
    zigzagcore(phigh, plow, phighbar, plowbar, zigzagpivots, zigzagpivotbars, zigzagpivotdirs)

draw_zg_line(idx1, idx2, zigzaglines, zigzagpivots, zigzagpivotbars, zigzagcolor, zigzagwidth, zigzagstyle) =>
    if array.size(zigzagpivots) > 2
        y1 = array.get(zigzagpivots, idx1)
        y2 = array.get(zigzagpivots, idx2)
        x1 = array.get(zigzagpivotbars, idx1)
        x2 = array.get(zigzagpivotbars, idx2)
        zline = line.new(x1=x1, y1=y1, x2=x2, y2=y2, color=zigzagcolor, width=zigzagwidth, style=zigzagstyle)
        if array.size(zigzaglines) >= 1
            lastLine = array.get(zigzaglines, 0)
            if x2 == line.get_x2(lastLine) and y2 == line.get_y2(lastLine)
                line.delete(lastLine)
        array.unshift(zigzaglines, zline)
        if array.size(zigzaglines) > max_array_size * 10
            line.delete(array.pop(zigzaglines))

draw_zigzag(doubleZG, zigzaglines, zigzagpivots, zigzagpivotbars, zigzagcolor, zigzagwidth, zigzagstyle) =>
    if doubleZG
        draw_zg_line(1, 2, zigzaglines, zigzagpivots, zigzagpivotbars, zigzagcolor, zigzagwidth, zigzagstyle)

    if array.size(zigzagpivots) >= 2
        draw_zg_line(0, 1, zigzaglines, zigzagpivots, zigzagpivotbars, zigzagcolor, zigzagwidth, zigzagstyle)

getCellColorByDirection(dir) =>
    dir == 2 ? color.green : dir == 1 ? color.lime : dir == -1 ? color.orange : dir == -2 ? color.red : color.silver

[level1ZG, doublel1ZG] = zigzag(zigzag1Length, zigzagpivots1, zigzagpivotbars1, zigzagpivotdirs1)
[level2ZG, doublel2ZG] = zigzag(zigzag2Length, zigzagpivots2, zigzagpivotbars2, zigzagpivotdirs2)
[level3ZG, doublel3ZG] = zigzag(zigzag3Length, zigzagpivots3, zigzagpivotbars3, zigzagpivotdirs3)
[level4ZG, doublel4ZG] = zigzag(zigzag4Length, zigzagpivots4, zigzagpivotbars4, zigzagpivotdirs4)


if level1ZG and showZigZag1
    draw_zigzag(doublel1ZG, zigzag1lines, zigzagpivots1, zigzagpivotbars1, zigzag1Color, 1, line.style_solid)

if level2ZG and showZigZag2
    draw_zigzag(doublel2ZG, zigzag2lines, zigzagpivots2, zigzagpivotbars2, zigzag2Color, 1, line.style_solid)

if level1ZG and showZigZag3
    draw_zigzag(doublel3ZG, zigzag3lines, zigzagpivots3, zigzagpivotbars3, zigzag3Color, 1, line.style_solid)

if level2ZG and showZigZag4
    draw_zigzag(doublel4ZG, zigzag4lines, zigzagpivots4, zigzagpivotbars4, zigzag4Color, 1, line.style_solid)

var stats = table.new(position=position.top_right, columns=8, rows=max_array_size + 2, border_width=1)
text_size = size.tiny
if barstate.islast and showStatTable
    if showZigZag1
        table.cell(table_id=stats, column=0, row=0, text='Zigzag ' + str.tostring(zigzag1Length), bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=0, row=1, text='Price', bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=1, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white, text_size=text_size)

        for i = 0 to array.size(zigzagpivots1) - 1 by 1
            bgcolor = getCellColorByDirection(array.get(zigzagpivotdirs1, i))
            table.cell(table_id=stats, column=0, row=i + 2, text=str.tostring(array.get(zigzagpivots1, i)), bgcolor=bgcolor, text_size=text_size)
            table.cell(table_id=stats, column=1, row=i + 2, text=str.tostring(array.get(zigzagpivotbars2, i)), bgcolor=bgcolor, text_size=text_size)

    if showZigZag2
        table.cell(table_id=stats, column=2, row=0, text='Zigzag ' + str.tostring(zigzag2Length), bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=2, row=1, text='Price', bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=3, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white, text_size=text_size)

        for i = 0 to array.size(zigzagpivots2) - 1 by 1
            bgcolor = getCellColorByDirection(array.get(zigzagpivotdirs2, i))
            table.cell(table_id=stats, column=2, row=i + 2, text=str.tostring(array.get(zigzagpivots2, i)), bgcolor=bgcolor, text_size=text_size)
            table.cell(table_id=stats, column=3, row=i + 2, text=str.tostring(array.get(zigzagpivotbars2, i)), bgcolor=bgcolor, text_size=text_size)

    if showZigZag3
        table.cell(table_id=stats, column=4, row=0, text='Zigzag ' + str.tostring(zigzag3Length), bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=4, row=1, text='Price', bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=5, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white, text_size=text_size)

        for i = 0 to array.size(zigzagpivots3) - 1 by 1
            bgcolor = getCellColorByDirection(array.get(zigzagpivotdirs3, i))
            table.cell(table_id=stats, column=4, row=i + 2, text=str.tostring(array.get(zigzagpivots3, i)), bgcolor=bgcolor, text_size=text_size)
            table.cell(table_id=stats, column=5, row=i + 2, text=str.tostring(array.get(zigzagpivotbars3, i)), bgcolor=bgcolor, text_size=text_size)

    if showZigZag4
        table.cell(table_id=stats, column=6, row=0, text='Zigzag ' + str.tostring(zigzag4Length), bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=6, row=1, text='Price', bgcolor=color.black, text_color=color.white, text_size=text_size)
        table.cell(table_id=stats, column=7, row=1, text='BarIndex', bgcolor=color.black, text_color=color.white, text_size=text_size)

        for i = 0 to array.size(zigzagpivots4) - 1 by 1
            bgcolor = getCellColorByDirection(array.get(zigzagpivotdirs4, i))
            table.cell(table_id=stats, column=6, row=i + 2, text=str.tostring(array.get(zigzagpivots4, i)), bgcolor=bgcolor, text_size=text_size)
            table.cell(table_id=stats, column=7, row=i + 2, text=str.tostring(array.get(zigzagpivotbars4, i)), bgcolor=bgcolor, text_size=text_size)

Overview

Multi ZigZag identifies high and low pivot points across multiple time horizons to display market structure and trend swings. Users can adjust the length periods, colors, and alternative price source options for each ZigZag line, as well as enable a statistics table. Because ZigZag calculations rely on confirming high and low extremes, historical pivot connections repaint as new price highs or lows form.

Details

Published by

WISEPLAT on GitHub

Code credit

HeWhoMustNotBeNamed, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Support & resistance, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
useAlternativeSourceboolfalseuseAlternativeSource
Alternative Sourcesourceclosesource
Show StatsbooltrueshowStatTable
max_array_sizeint20max_array_size
L1 LengthbooltrueshowZigZag1
zigzag1Lengthint5zigzag1Length
zigzag1Colorcolorcolor.rgb(66, 165, 245, 0)zigzag1Color
L2 LengthbooltrueshowZigZag2
zigzag2Lengthint8zigzag2Length
zigzag2Colorcolorcolor.rgb(153, 204, 102, 0)zigzag2Color
L3 LengthbooltrueshowZigZag3
zigzag3Lengthint13zigzag3Length
Show all 16 settings
SettingTypeDefaultVariable
zigzag3Colorcolorcolor.rgb(251, 192, 45, 0)zigzag3Color
L4 LengthbooltrueshowZigZag4
zigzag4Lengthint21zigzag4Length
zigzag4Colorcolorcolor.rgb(244, 143, 177, 0)zigzag4Color
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - Linear Regression

AI Trend

by dearvn
257 stars

AI Trend is an indicator that plots pivot points, trend lines, and support and resistance levels directly on the price chart. It marks high and low pivots based on user-defined length, draws trend lines that check for violations at specified points, and identi

Indicator v5 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of AI Trend

Ichimoku Cloud

by harryguiacorn
219 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud
219 stars

This indicator identifies local swing highs and swing lows to construct a Zig Zag pattern on the main price chart. It evaluates pivot points based on a customizable number of surrounding left and right bars. Using persistent variables, the script tracks previo

Indicator v4 Support & resistanceTrend Repaints
Updated 5 years ago · None stated GitHub repository of Teaching: Finding Highs and Lows 6

Dynamic Linear Regression

by TradersPost
218 stars

Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Dynamic Linear Regression

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers