Indicator · Pine Script v4 · On the chart

Test-017-zz

by ricardosantos79· code credited to RicardoSantos

This indicator draws a zigzag overlay on the price chart.

View on GitHub

GitHub stars261on the repository
Pine versionv4older, still runs
Lines of code195
Settings6inputs
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

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

test-017-feature.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(title='test-017-zz', overlay=true)

// ZigZag options:
bool show_real_pivots = input(defval=true, title="Show real zigzag pivots:", type=input.bool)
bool show_zigzag_lines = input(defval=true, title="Show zigzag lines:", type=input.bool)

//  |--------------------------------------------------------------------------||
//  |   ZigZag:                                                                ||
//  |--------------------------------------------------------------------------||
//  |{

string m_000 = '(MANUAL) Percent price move over X * Y'
string m_001 = 'Percent price move over atr(X) * Y'
string m_002 = 'Percent price move over max(tr) * Y'
string m_003 = 'high or low pivot(X, Y)'
string zigzag_method = input(
         defval=m_000, 
         title="Method to use for the zigzag:", 
         options=[m_000, m_001, m_002, m_003]
         )

float param_x = input(defval=1.0, title='parameter X')
float param_y = input(defval=1.0, title='parameter Y')

bool show_real_pivots_zz = input(defval=true, title="Show real pivots:", type=input.bool)

//  ||-------------------------------------------------------------------------||
//  ||  zigzag function:
//  ||-------------------------------------------------------------------------||
//  |{
f_zz(_method, _x, _y)=>

    //  note: linkage to global variables (m000, m001, m002)
    //      string m_000 = '(MANUAL) Percent price move over X * Y'
    //      string m_001 = 'Percent price move over atr(X) * Y'
    //      string m_002 = 'Percent price move over max(tr) * Y'

    var float _percent = na
    if _method == m_001
        _percent := atr(int(_x)) / open * _y
    else if _method == m_002
        float _tr = (tr / open) * _y
        if na(_percent)
            _percent := _tr
        if _tr > _percent
            _percent := _tr
    else
        _percent := _x * _y


    //  direction after last pivot
    var bool _is_direction_up = na
    //  track highest price since last lower pivot
    var float _htrack = na
    //  track lowest price since last higher pivot
    var float _ltrack = na
    //  zigzag variable for ploting
    var float _pivot = na
    //  real pivot time
    var int _real_pivot_time = na
    var int _htime = na
    var int _ltime = na
    //  reverse line
    var float _reverse_line = 0.0


    //  range needed for reaching reversal threshold
    float _reverse_range = 0.0
    //  pivots high / low for method m_003
    // string m_003 = 'high or low pivot(X, Y)'
    _pivot_h = pivothigh(_x, _y)
    _pivot_l = pivotlow(_x, _y)

    if bar_index >= 1
        
        if na(_is_direction_up)
            _is_direction_up := true
        
        _reverse_range := nz(_pivot[1]) * _percent
        
        if _is_direction_up
            _ltrack := na
            _ltime := time
            
            if na(_htrack)
                _htrack := high
                _htime := time
            else
                if high >= _htrack
                    _htrack := high
                    _htime := time

            if _method == m_003
                _reverse_line := _htrack
                if _pivot_h
                    _pivot := _htrack
                    _real_pivot_time := _htime
                    _is_direction_up := false
            else
                _reverse_line := (_htrack - _reverse_range)
                if close <= _reverse_line
                    _pivot := _htrack
                    _real_pivot_time := _htime
                    _is_direction_up := false

        if not _is_direction_up
            _htrack := na
            _htime := na
            
            if na(_ltrack)
                _ltrack := low
                _ltime := time
            else
                if low <= _ltrack
                    _ltrack := low
                    _ltime := time
                
            if _method == m_003
                _reverse_line := _ltrack
                if _pivot_l
                    _pivot := _ltrack
                    _real_pivot_time := _ltime
                    _is_direction_up := true
            else
                _reverse_line := (_ltrack + _reverse_range)
                if close >= _reverse_line
                    _pivot := _ltrack
                    _real_pivot_time := _ltime
                    _is_direction_up := true

    [_pivot, _is_direction_up, _reverse_line, _real_pivot_time]

// ||   |}---------------------------------------------------------------------<•

//  ||-------------------------------------------------------------------------||
//  ||  zigzag data:
//  ||-------------------------------------------------------------------------||
//  |{
[price_a, is_up, reverse, _rl_time] = f_zz(zigzag_method, param_x, param_y)
alt_time = show_real_pivots_zz ? _rl_time : time

zz_color = is_up ? color.orange : color.teal
is_new_zig = change(price_a) != 0 ? price_a : na
//  
//plot(is_new_zig, title="Z", color=zz_color, linewidth=1, transp=80)

plot(reverse, title="R", color=color.gray, style=plot.style_stepline, linewidth=1, transp=40, offset=1)
//plot(reverse, title="R", color=color.white, style=plot.style_circles, linewidth=4, transp=40, offset=1, show_last=1)

//  |   Get Pivots:
var int time_a = na
var int time_b = na, var float price_b = na
var int time_c = na, var float price_c = na
var int time_d = na, var float price_d = na
var int time_e = na, var float price_e = na
var int time_f = na, var float price_f = na
var int time_g = na, var float price_g = na

if is_new_zig
    time_a := alt_time
    time_b := time_a[1], price_b := price_a[1]
    time_c := time_b[1], price_c := price_b[1]
    time_d := time_c[1], price_d := price_c[1]
    time_e := time_d[1], price_e := price_d[1]
    time_f := time_e[1], price_f := price_e[1]
    time_g := time_f[1], price_g := price_f[1]
    

float AB_price_difference = abs(price_a - price_b)
//float AC_price_difference = abs(price_a - price_c)

int AB_time_difference = time_a - time_b
//int AC_time_difference = time_a - time_c

//  ||-------------------------------------------------------------------------||
//  ||  Draw zigzag:
//  ||-------------------------------------------------------------------------||
//  |{
f_draw_zigzag_lines()=>
    var line _li = na
    _li_color = price_a > price_b ? color.teal : color.orange
    if is_new_zig
        _li := line.new(
             time_a, price_a, 
             time_b, price_b, 
             xloc.bar_time, extend=extend.none, color=_li_color, width=2
             )

if show_zigzag_lines
    f_draw_zigzag_lines()
//  ||  |}---------------------------------------------------------------------<•

Overview

Test-017-zz offers four reversal methods: a manual percent move from parameters X and Y, a percent of ATR, a running maximum true range, or confirmed high and low pivots. The function tracks extremes since the last pivot and stores a new pivot when the chosen threshold is reached. Toggles show or hide zigzag lines and real pivot marks. Parameters X and Y set the reversal size or the pivot lengths. The supplied code ends inside the zigzag function, so later drawing logic is not visible.

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

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
Show real zigzag pivots:booltrueshow_real_pivots
Show zigzag lines:booltrueshow_zigzag_lines
Method to use for the zigzag:inputm_000zigzag_method
parameter Xfloat1.0param_x
parameter Yfloat1.0param_y
Show real pivots:booltrueshow_real_pivots_zz
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