Indicator · Pine Script v5 · On the chart

ZigZag with Retracement Levels

by WISEPLAT· code credited to PtGambler

This indicator calculates pivot highs and lows using a defined period to draw ZigZag lines directly on the price chart.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code151
Settings21inputs
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

151 lines of Pine Script v5 from ZigZag - ishod/ZigZag with Retracement Levels - Fibo - 9.pine.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

ZigZag with Retracement Levels - Fibo - 9.pine.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PtGambler

// This source code is a modification to 'ZigZag with Fibonacci Levels' published by © LonesomeTheBlue

//@version=5
indicator('ZigZag with Retracement Levels', overlay=true, max_bars_back=500, max_lines_count=300)
prd = input.int(defval=10, title='ZigZag Period', minval=2, maxval=50)
showzigzag = input(defval=true, title='Show Zig Zag')
showfibo = input(defval=true, title='Show Retracement Ratios')
labelcol = input(defval=color.white, title='Text Color for Levels')
fibolinecol = input(defval=color.white, title='Line Color for Levels')
showcurrent = input(defval=true, title='Show Retracement Ratios based on latest pivot')
labelcol2 = input(defval=color.yellow, title='Text Color for Current Levels')
fibolinecol2 = input(defval=color.yellow, title='Line Color for Current Levels')
upcol = input.color(defval=color.lime, title='Zigzag Colors', inline='zzcol')
dncol = input.color(defval=color.red, title='', inline='zzcol')
labelloc = input.string(defval='Left', title='Label Location', options=['Left', 'Right'])

// Modified from © LonesomeTheBlue origial code, added options to enable/disable/customize levels ------------------------------------
enable1 = input(defval=true, title='Enable Level 1', inline ='level1')
level1 = input.float(defval=0.236, title='', inline='level1')
enable2 = input(defval=true, title='Enable Level 2', inline ='level2')
level2 = input.float(defval=0.382, title='', inline='level2')
enable3 = input(defval=true, title='Enable Level 3', inline ='level3')
level3 = input.float(defval=0.500, title='', inline='level3')
enable4 = input(defval=true, title='Enable Level 4', inline ='level4')
level4 = input.float(defval=0.618, title='', inline='level4')
enable5 = input(defval=true, title='Enable Level 5', inline ='level5')
level5 = input.float(defval=0.786, title='', inline='level5')

var fibo_ratios = array.new_float(0)
var shownlevels = 1
if barstate.isfirst
    array.push(fibo_ratios, 0.0)
    if enable1
        array.push(fibo_ratios, level1)
        shownlevels += 1
        shownlevels
    if enable2
        array.push(fibo_ratios, level2)
        shownlevels += 1
        shownlevels
    if enable3
        array.push(fibo_ratios, level3)
        shownlevels += 1
        shownlevels
    if enable4
        array.push(fibo_ratios, level4)
        shownlevels += 1
        shownlevels
    if enable5
        array.push(fibo_ratios, level5)
        shownlevels += 1
        shownlevels
    // for x = 1 to 5 by 1
        // array.push(fibo_ratios, x)
    //     array.push(fibo_ratios, x + 0.272)
    //     array.push(fibo_ratios, x + 0.414)
    //     array.push(fibo_ratios, x + 0.618)
    array.push(fibo_ratios, 1.0)
// -------------------------------------------------------------------------------


float ph = ta.highestbars(high, prd) == 0 ? high : na
float pl = ta.lowestbars(low, prd) == 0 ? low : na
var dir = 0
iff_1 = pl and na(ph) ? -1 : dir
dir := ph and na(pl) ? 1 : iff_1
var max_array_size = 10
var zigzag = array.new_float(0)
oldzigzag = array.copy(zigzag)

add_to_zigzag(value, bindex) =>
    array.unshift(zigzag, bindex)
    array.unshift(zigzag, value)
    if array.size(zigzag) > max_array_size
        array.pop(zigzag)
        array.pop(zigzag)

update_zigzag(value, bindex) =>
    if array.size(zigzag) == 0
        add_to_zigzag(value, bindex)
    else
        if dir == 1 and value > array.get(zigzag, 0) or dir == -1 and value < array.get(zigzag, 0)
            array.set(zigzag, 0, value)
            array.set(zigzag, 1, bindex)
        0.

bool dirchanged = dir != dir[1]
if ph or pl
    if dirchanged
        add_to_zigzag(dir == 1 ? ph : pl, bar_index)
    else
        update_zigzag(dir == 1 ? ph : pl, bar_index)

if showzigzag and array.size(zigzag) >= 4 and array.size(oldzigzag) >= 4
    var line zzline = na
    if array.get(zigzag, 0) != array.get(oldzigzag, 0) or array.get(zigzag, 1) != array.get(oldzigzag, 1)
        if array.get(zigzag, 2) == array.get(oldzigzag, 2) and array.get(zigzag, 3) == math.round(array.get(oldzigzag, 3))
            line.delete(zzline)
        zzline := line.new(x1=math.round(array.get(zigzag, 1)), y1=array.get(zigzag, 0), x2=math.round(array.get(zigzag, 3)), y2=array.get(zigzag, 2), color=dir == 1 ? upcol : dncol, width=2)
        zzline

var fibolines = array.new_line(0)
var fibolabels = array.new_label(0)

// Addition to © LonesomeTheBlue origial code ------------------------------------
var fibolines2 = array.new_line(0)
var fibolabels2 = array.new_label(0)
// -------------------------------------------------------------------------------

if showfibo and array.size(zigzag) >= 6 and barstate.islast
    if array.size(fibolines) > 0
        for x = 0 to array.size(fibolines) - 1 by 1
            line.delete(array.get(fibolines, x))
            label.delete(array.get(fibolabels, x))
    if array.size(fibolines2) > 0
        for x = 0 to array.size(fibolines2) - 1 by 1
            line.delete(array.get(fibolines2, x))
            label.delete(array.get(fibolabels2, x))

    diff = array.get(zigzag, 4) - array.get(zigzag, 2)
    stopit = false
    for x = 0 to array.size(fibo_ratios) - 1 by 1
        if stopit and x > shownlevels
            break
        array.unshift(fibolines, line.new(x1=math.round(array.get(zigzag, 5)), y1=array.get(zigzag, 2) + diff * array.get(fibo_ratios, x), x2=bar_index, y2=array.get(zigzag, 2) + diff * array.get(fibo_ratios, x), color=fibolinecol, extend=extend.right, width=2))
        label_x_loc = labelloc == 'Left' ? math.round(array.get(zigzag, 5)) - 1 : bar_index + 15
        array.unshift(fibolabels, label.new(x=label_x_loc, y=array.get(zigzag, 2) + diff * array.get(fibo_ratios, x), text=str.tostring(array.get(fibo_ratios, x), '#.###') + '(' + str.tostring(math.round_to_mintick(array.get(zigzag, 2) + diff * array.get(fibo_ratios, x))) + ')', textcolor=labelcol, style=label.style_none))
        if dir == 1 and array.get(zigzag, 2) + diff * array.get(fibo_ratios, x) > array.get(zigzag, 0) or dir == -1 and array.get(zigzag, 2) + diff * array.get(fibo_ratios, x) < array.get(zigzag, 0)
            stopit := true
            stopit



// Addition to © LonesomeTheBlue origial code ------------------------------------
if showcurrent and array.size(zigzag) >= 6 and barstate.islast
    diff2 = array.get(zigzag, 0) - array.get(zigzag, 2)
    stopit2 = false
    for x = 0 to array.size(fibo_ratios) - 1 by 1
        if stopit2 and x > shownlevels
            break
        array.unshift(fibolines2, line.new(x1=math.round(array.get(zigzag, 3)), y1=array.get(zigzag, 2) + diff2 * (1-array.get(fibo_ratios, x)), x2=bar_index, y2=array.get(zigzag, 2) + diff2 * (1-array.get(fibo_ratios, x)), color=fibolinecol2, extend=extend.right))
        label_x_loc2 = labelloc == 'Left' ? math.round(array.get(zigzag, 3)) - 1 : bar_index + 15
        array.unshift(fibolabels2, label.new(x=label_x_loc2, y=array.get(zigzag, 2) + diff2 * (1-array.get(fibo_ratios, x)), text=str.tostring(array.get(fibo_ratios, x), '#.###') + '(' + str.tostring(math.round_to_mintick(array.get(zigzag, 2) + diff2 * (1-array.get(fibo_ratios, x)))) + ')', textcolor=labelcol2, style=label.style_none))
        if dir == 1 and array.get(zigzag, 2) + diff2 * array.get(fibo_ratios, x) > array.get(zigzag, 0) or dir == -1 and array.get(zigzag, 2) + diff2 * array.get(fibo_ratios, x) < array.get(zigzag, 0)
            stopit2 := true
            stopit2
    
// -------------------------------------------------------------------------------

Overview

ZigZag with Retracement Levels projects customizable Fibonacci retracement levels and ratio labels across identified pivot swings. It also displays real-time retracement levels relative to the latest active pivot. Users can adjust the ZigZag evaluation period, toggle lines or labels, customize colors, set label positions, and individually enable or disable specific Fibonacci ratios.

Details

Published by

WISEPLAT on GitHub

Code credit

PtGambler, as written in the script's header

Draws

On the price chart

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
ZigZag Periodint10prd
Show Zig Zagbooltrueshowzigzag
Show Retracement Ratiosbooltrueshowfibo
Text Color for Levelscolorcolor.whitelabelcol
Line Color for Levelscolorcolor.whitefibolinecol
Show Retracement Ratios based on latest pivotbooltrueshowcurrent
Text Color for Current Levelscolorcolor.yellowlabelcol2
Line Color for Current Levelscolorcolor.yellowfibolinecol2
Zigzag Colorscolorcolor.limeupcol
dncolcolorcolor.reddncol
Label LocationstringLeftlabelloc
Enable Level 1booltrueenable1
Show all 21 settings
SettingTypeDefaultVariable
level1float0.236level1
Enable Level 2booltrueenable2
level2float0.382level2
Enable Level 3booltrueenable3
level3float0.500level3
Enable Level 4booltrueenable4
level4float0.618level4
Enable Level 5booltrueenable5
level5float0.786level5
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

947 stars

Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us

Indicator v4 Moving averagesSupport & resistance
Updated 12 days ago · GPL-3.0 GitHub repository of Moving Average Channel

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

This overlay indicator draws a fixed-range volume profile on the price chart, splitting traded activity into up and down volume at each price level. It also plots a sentiment profile, a developing point of control, and value-area high and low around a configur

Indicator v5 VolumeSupport & resistance
Updated 2 months ago · MIT GitHub repository of Volume Profile and Indicator by DGT

BZ-tracker

by Allen1212
303 stars

The BZ-tracker indicator marks ARK Invest buy and sell transactions on stock charts with labels. It plots green labels for purchases and red labels for sales, each annotated with a multiplier such as X1 or X2 to show the number of ARK ETFs trading the stock on

Indicator v4 Support & resistanceScreeners & dashboards
Updated 2 years ago · None stated GitHub repository of BZ-tracker

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

More Support & resistance scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers