Indicator · Pine Script v4 · On the chart

Test 012 ZZ

by ricardosantos79· code credited to RicardoSantos

This experimental script detects high and low pivot points to establish a basic ZigZag structure.

View on GitHub

Repaints

GitHub stars261on the repository
Pine versionv4older, still runs
Lines of code109
Settings1inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

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

109 lines of Pine Script v4 from beta/arrays/test-012-zz.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

test-012-zz.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=4
study("test-012-", overlay=true)
// bug max_bars_back error..

bool show_debug = input(false)
var label debug = label.new(x=bar_index, y=open, text='', style=label.style_label_left)
f_print(_A, _label)=>
    if show_debug
        int _size = array.size(id=_A) - 1
        string _t = ''
        if _size > 0
            for _i = _size to 0
                _t := _t + tostring(_i, '# : ') + tostring(array.get(id=_A, index=_i), '#') + ' \n'
        label.set_xy(id=_label, x=bar_index, y=0.0)
        label.set_text(id=_label, text=_t)


float h = pivothigh(high, 2, 2)
float l = pivotlow(low, 2, 2)

var int z_bar_index = 0
var bool direction = false
//plot(direction?1:0)
if direction
    if h
        direction := false
        z_bar_index := bar_index
    if l
        z_bar_index := bar_index
else
    if l
        direction := true
        z_bar_index := bar_index
    if h
        z_bar_index := bar_index

plot(h, color=color.red, linewidth=2, style=plot.style_circles)
plot(l, color=color.lime, linewidth=2, style=plot.style_circles)

float zigzag = na
if direction 
    zigzag := l[min(0, bar_index-z_bar_index)]
else
    zigzag := h[min(0, bar_index-z_bar_index)]
plot(zigzag, color=color.gray)

// int array_maximum_storage = input(10)
// var int[] zz = array.new_int(size=1, initial_value=0)
// int current_size = array.size(id=zz)

// var float avg = open
// var float deriv = 0.

// if change(z_bar_index) != 0
//     if direction != direction[1]
//         if current_size <= array_maximum_storage
//             array.insert(id=zz, index=0, value=z_bar_index)
//         else
//             array.remove(id=zz, index=current_size-1)//array_maximum_storage-1)
//             array.insert(id=zz, index=0, value=z_bar_index)
//     else
//         array.set(id=zz, index=0, value=z_bar_index)

//     float sum = 0.    
//     for _i = 0 to current_size-1
//         _bar = min(0, bar_index - array.get(id=zz, index=_i))
//         sum := sum + (direction[_bar] ? l[_bar] : h[_bar])
    
//     f_print(zz, debug)
    
//     avg := sum / current_size
    
//     float _max = zigzag[min(0, bar_index - array.get(id=zz, index=0) + 1)]
//     float _min = zigzag[min(0, bar_index - array.get(id=zz, index=0) + 1)]
//     //label.set_text(id=debug, text=tostring(_min))
//     for _i = 0 to current_size-1
//         _bar = min(0, bar_index - array.get(id=zz, index=_i))
//         float _current_h = h[_bar]
//         float _current_l = l[_bar]
//         if _current_h >= _max
//             _max := _current_h
//         if _current_l <= _min
//             _min := _current_l
//     deriv := (_max - _min) * 0.5

// deriv := fixnan(deriv)
// plot(deriv)

// var line center = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, extend=extend.both, color=color.silver, width=1)
// var line top = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, extend=extend.both, color=color.silver, width=1)
// var line bot = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, extend=extend.both, color=color.silver, width=1)

// float multiplier = input(1.)
// if current_size > 1
//     int previous_bar = array.get(id=zz, index=1)
//     int current_bar = array.get(id=zz, index=0)
//     line.set_xy1(id=center, x=previous_bar, y=avg[bar_index - previous_bar])
//     line.set_xy2(id=center, x=current_bar, y=avg[bar_index - current_bar])
//     line.set_xy1(id=top, x=previous_bar, y=avg[bar_index - previous_bar] + deriv * multiplier)
//     line.set_xy2(id=top, x=current_bar, y=avg[bar_index - current_bar] + deriv * multiplier)
//     line.set_xy1(id=bot, x=previous_bar, y=avg[bar_index - previous_bar] - deriv * multiplier)
//     line.set_xy2(id=bot, x=current_bar, y=avg[bar_index - current_bar] - deriv * multiplier)

// plot(avg, color=color.yellow, transp=40)
// plot(avg+deriv, color=color.yellow, transp=40)
// plot(avg-deriv, color=color.yellow, transp=40)

Overview

Test 012 ZZ plots red and lime circles on the chart to highlight detected high and low pivots, along with a gray line tracking the ZigZag trajectory based on bar index offsets. A debug toggle is included to print array values via label elements. Note that pivot detection introduces inherent visual repainting due to confirmation lag.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Support & resistance

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
show_debugboolfalseshow_debug
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

948 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 10 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

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