Indicator · Pine Script v5 · On the chart

Price Action Candles with Zig Zag

by g-moe· code credited to goodgains

This indicator draws supply and demand zones and break-of-structure markers on the price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code328
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 2026

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

328 lines of Pine Script v5 from Tradingview/price-action-candles-zigzag.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

price-action-candles-zigzag.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("Price Action Candles with Zig Zag", overlay = true, max_labels_count = 500, max_boxes_count = 500, max_lines_count = 500, max_bars_back = 1000)

// @keywords: price-action-candles-zigzag, supply-demand-zones, break-of-structure, zigzag-swing-lines, swing-high-low-labels, hh-hl-lh-ll-labels, point-of-interest, atr-zone-width, double-top-bottom, swing-length-pivots, demand-supply-boxes, bos-markers, price-action-labels, poi-sensitivity, swing-structure, array-zone-tracking, fractal-swing-detection, smc-structure, zone-overlap-check, overlay-zones

//
//SETTINGS
//

//      INDICATOR SETTINGS
swing_length = input.int(5, title = 'Swing High/Low Length', group = 'Settings', minval = 1, maxval = 50)
poi_sensitvity = input.float(2.5, title = 'POI Sensitivity', group = 'Settings', minval = 1, maxval = 10, step = 0.5)



//      INDICATOR VISUAL SETTINGS
show_zigzag = input.bool(false, title = 'Show Zig Zag', group = 'Visual Settings', inline = '1')
show_price_action_labels = input.bool(false, title = 'Show Price Action Labels', group = 'Visual Settings', inline = '2')


supply_color = input.color(color.new(#EDEDED,70), title = 'Supply', group = 'Visual Settings', inline = '3')
supply_outline_color = input.color(color.new(color.white,75), title = 'Outline', group = 'Visual Settings', inline = '3')

demand_color = input.color(color.new(#00FFFF,70), title = 'Demand', group = 'Visual Settings', inline = '4')
demand_outline_color = input.color(color.new(color.white,75), title = 'Outline', group = 'Visual Settings', inline = '4')

bos_color = input.color(color.new(#EDEDED,70), title = 'BOS', group = 'Visual Settings', inline = '5')
bos_label_color = input.color(color.white, title = 'Label', group = 'Visual Settings', inline = '5')

poi_label_color = input.color(color.white, title = 'POI Label', group = 'Visual Settings', inline = '7')
poi_width = input.float(2.5, title = 'Width', group = 'Visual Settings', minval = 1, maxval = 10, step = 0.5, inline = '7')
swing_type_color = input.color(color.gray, title = 'Price Action Label', group = 'Visual Settings', inline = '8')
zigzag_color = input.color(color.new(#000000,0), title = 'Zig Zag', group = 'Visual Settings', inline = '9')


//
//END SETTINGS
//

//
//FUNCTIONS
//

//      FUNCTION TO ADD NEW AND REMOVE LAST IN ARRAY
f_array_add_pop(array, new_value_to_add) =>
    array.unshift(array, new_value_to_add)
    array.pop(array)

//      FUNCTION SWING H & L LABELS
f_sh_sl_labels(array, swing_type) =>

    var string label_text = na
    if swing_type == 1
        if array.get(array, 0) >= array.get(array, 1)
            label_text := 'HH'
        else
            label_text := 'LH'
        label.new(bar_index - swing_length, array.get(array,0), text = label_text, style=label.style_label_down, textcolor = swing_type_color, color = color.new(swing_type_color, 100), size = size.tiny)
    
    else if swing_type == -1
        if array.get(array, 0) >= array.get(array, 1)
            label_text := 'HL'
        else
            label_text := 'LL'
        label.new(bar_index - swing_length, array.get(array,0), text = label_text, style=label.style_label_up, textcolor = swing_type_color, color = color.new(swing_type_color, 100), size = size.tiny)


//      FUNCTION CHECK FOR DOUBLE TOP
f_check4_dt(array, atr_buffer) =>

    new_swing_level = array.get(array, 0)
    previous_swing_level = array.get(array, 1)

    upper_box = previous_swing_level + atr_buffer
    lower_box = previous_swing_level - atr_buffer

    var bool is_double_lvl = na
    is_double_lvl := new_swing_level >= lower_box and new_swing_level <= upper_box ? true : false
    is_double_lvl


//      FUNCTION FOR CALCULATING / DRAWING  INITIAL POI
f_calculate_boxes(value_array, bn_array, box_array, box_type, atr_buffer) =>

    box_left = array.get(bn_array, 1)
    box_right = array.get(bn_array, 0)

    //delete oldest box, and then create a new box and add it to the array
    if box_type == 1
        poi = math.max( array.get(value_array, 1), array.get(value_array, 0) )
        box_top = poi + atr_buffer
        box_bottom = poi - atr_buffer
        box.delete( array.get(box_array, array.size(box_array) - 1) )
        f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = supply_outline_color,
             bgcolor = supply_color, extend = extend.right, text = 'POI', text_halign = text.align_left, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
    else if box_type == -1
        poi = math.min( array.get(value_array, 1), array.get(value_array, 0) )
        box_top = poi + atr_buffer
        box_bottom = poi - atr_buffer
        box.delete( array.get(box_array, array.size(box_array) - 1) )
        f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = demand_outline_color,
             bgcolor = demand_color, extend = extend.right,  text = 'POI', text_halign = text.align_left, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))

    
//      FUNCTION TO MANAGE POI BOX VALUES
f_manage_poi(box_array, box_finished_array, box_type) =>

    //depending on box we will loop through all extended pois and if the high/low crosses it then we will copy it to the finished boxes and then delete it from the extended boxes
    for i = 0 to array.size(box_array) - 1
        box_high = box.get_top( array.get(box_array, i) )
        box_low =  box.get_bottom( array.get(box_array, i) )
        if high > box_high and box_type == 1 or low < box_low and box_type == -1
            
            box_to_copy = box.copy( array.get(box_array, i) )
            f_array_add_pop(box_finished_array, box_to_copy)
            box.set_extend( array.get(box_finished_array,0), extend.none)
            box.set_right( array.get(box_finished_array,0), bar_index)
            box.set_text( array.get(box_finished_array,0), 'POI' )
            box.set_text_color( array.get(box_finished_array,0), poi_label_color)
            box.set_text_size( array.get(box_finished_array,0), size.small)
            box.set_text_halign( array.get(box_finished_array,0), text.align_left)
            box.set_text_valign( array.get(box_finished_array,0), text.align_center)
            box.delete( array.get(box_array, i) )
   


//      FUNCTION FOR MOVING OPPOSITE BOS TO FINISHED, DELETING FROM EXTENDED AND THEN CALCULATING / DRAWING 3 INITIAL BOS'S
f_calculate_bos_new_poi(unfinished, values, bns) =>

    //create new bos's
    box_left = array.get(bns, 0)
    box_right = array.get(bns, 0) + 1
    box_top = array.get(values, 0)
    box_bottom = array.get(values, 0)

    array.unshift(unfinished, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = bos_color, 
         bgcolor = bos_color, extend = extend.right,  text = 'BOS', text_halign = text.align_left, text_valign = text.align_center, text_color = bos_label_color, text_size = size.small, xloc = xloc.bar_index))


//      FUNCTION TO MANAGE POI BOX VALUES
f_manage_bos(unfinished, finished, last_signal) =>

    //depending on box we will loop through all extended pois and if the high/low crosses it then we will copy it to the finished boxes and then delete it from the extended boxes
    if array.size(unfinished) > 0
        for i = 0 to array.size(unfinished) - 1
            box_high = box.get_top( array.get(unfinished, i) )
            box_low = box.get_bottom( array.get(unfinished, i) )
            box_left = box.get_bottom( array.get(unfinished, i) )
            // if ta.crossover(high, box_high) or ta.crossunder(low, box_low)
            if high > box_high and last_signal == -1 or low < box_low and last_signal == 1
                box_to_copy = box.copy( array.get(unfinished, i) )
                f_array_add_pop(finished, box_to_copy)
                box.set_extend( array.get(finished,0), extend.none)
                box.set_right( array.get(finished,0), bar_index)
                box.set_text( array.get(finished,0), 'BOS' )
                box.set_text_color( array.get(finished,0), bos_label_color)
                box.set_bgcolor( array.get(finished,0), bos_color)
                box.set_text_size( array.get(finished,0), size.small)
                box.set_text_halign( array.get(finished,0), text.align_left)
                box.set_text_valign( array.get(finished,0), text.align_center)
                box.delete( array.get(unfinished, i) )

//
//END FUNCTIONS
//  


//
//CALCULATIONS
//

//      CALCULATE ATR 
atr = ta.atr(50)

//      CALCULATE SWING HIGHS & SWING LOWS
swing_high = ta.pivothigh(high, swing_length, swing_length)
swing_low = ta.pivotlow(low, swing_length, swing_length)
// plotshape(swing_high, title = 'Swing High Marker', style=shape.circle, color = color.blue, location = location.absolute, offset = -swing_length)
// plotshape(swing_low, title = 'Swing Low Marker', style=shape.circle, color = color.blue, location = location.absolute, offset = -swing_length)


//      CREATE ARRAYS FOR SWING H/L & BN 
var swing_high_values = array.new_float(50,0.00)
var swing_low_values = array.new_float(50,0.00)

var swing_high_bns = array.new_int(50,0)
var swing_low_bns = array.new_int(50,0)

//      CREATE BOOLS TO STORE IF NEW DT OR DB 
var bool double_top = na
var bool double_bottom = na

//      CREATE ARRAYS FOR EXTENDED BOXES
var swing_high_poi = array.new_box(5, na)
var swing_low_poi = array.new_box(5, na)

//      CREATE ARRAYS FOR FINISHED BOXES
var finished_swing_high_poi = array.new_box(5, na)
var finished_swing_low_poi = array.new_box(5, na)

//      CREATE ARRAYS FOR BOS BOXES
var unfinished_bos = array.new_box(50, na)

//      CREATE ARRAYS FOR BOS BOXES
var finished_bos = array.new_box(50, na)


//WE STORE THE LAST SIGNAL BECAUSE DEPENDING ON THE LAST POI WE CAN ONLY DRAW BOS ONE WAY
var last_signal = 0
last_signal := double_top ? 1 : double_bottom ? -1 : last_signal[1]

//      NEW SWING HIGH
if not na(swing_high)

    //MANAGE SWING HIGH VALUES
    f_array_add_pop(swing_high_values, swing_high)
    f_array_add_pop(swing_high_bns, bar_index[swing_length])
    if show_price_action_labels
        f_sh_sl_labels(swing_high_values, 1)

    //CHECK TO SEE IF NEW SH IS A DOUBLE TOP
    double_top := f_check4_dt(swing_high_values, atr * (poi_sensitvity / 10))
    if double_top
        // CALCULATE NEW POI BOX VALUES & ADD THEM TO EXTENDED ARRAY
        f_calculate_boxes(swing_high_values, swing_high_bns, swing_high_poi, 1, atr * (poi_width / 10))
        
        // CALCULATE NEW BOS BOX VALUES & ADD THEM TO EXTENDED ARRAY
        f_calculate_bos_new_poi(unfinished_bos, swing_low_values, swing_low_bns)

//      NEW SWING LOW
else if not na(swing_low)

    //MANAGE SWING LOW VALUES
    f_array_add_pop(swing_low_values, swing_low)
    f_array_add_pop(swing_low_bns, bar_index[swing_length])
    if show_price_action_labels
        f_sh_sl_labels(swing_low_values, -1)

    //CHECK TO SEE IF NEW SH IS A DOUBLE TOP
    double_bottom := f_check4_dt(swing_low_values, atr * (poi_sensitvity / 10))

    // CALCULATE NEW POI BOX VALUES
    if double_bottom
        // CALCULATE NEW POI BOX VALUES & ADD THEM TO EXTENDED ARRAY
        f_calculate_boxes(swing_low_values, swing_low_bns, swing_low_poi, -1, atr * (poi_width / 10))

        // CALCULATE NEW BOS BOX VALUES & ADD THEM TO EXTENDED ARRAY  
        f_calculate_bos_new_poi(unfinished_bos, swing_high_values, swing_high_bns)

else
    //MANAGE POI'S
    f_manage_poi(swing_high_poi, finished_swing_high_poi, 1)
    f_manage_poi(swing_low_poi, finished_swing_low_poi, -1)

    //MANAGE BOS
    f_manage_bos(unfinished_bos, finished_bos, last_signal)


    // RESET VARS THAT NEED IT
    double_top := false
    double_bottom := false




//
//END CALCULATIONS
//


    
h = ta.highest(high, swing_length * 2 + 1)
l = ta.lowest(low, swing_length * 2 + 1)
f_isMin(len) =>
    l == low[len]
f_isMax(len) =>
    h == high[len]

var dirUp = false
var lastLow = high * 100
var lastHigh = 0.0
var timeLow = bar_index
var timeHigh = bar_index
var line li = na

f_drawLine() =>
    _li_color = show_zigzag ? zigzag_color : color.new(#ffffff,100)
    line.new(timeHigh - swing_length, lastHigh, timeLow - swing_length, lastLow, xloc.bar_index, color=_li_color, width=2)

if dirUp
    if f_isMin(swing_length) and low[swing_length] < lastLow
        lastLow := low[swing_length]
        timeLow := bar_index
        line.delete(li)
        li := f_drawLine()
        li

    if f_isMax(swing_length) and high[swing_length] > lastLow
        lastHigh := high[swing_length]
        timeHigh := bar_index
        dirUp := false
        li := f_drawLine()
        li

if not dirUp
    if f_isMax(swing_length) and high[swing_length] > lastHigh
        lastHigh := high[swing_length]
        timeHigh := bar_index
        line.delete(li)
        li := f_drawLine()
        li
    if f_isMin(swing_length) and low[swing_length] < lastHigh
        lastLow := low[swing_length]
        timeLow := bar_index
        dirUp := true
        li := f_drawLine()
        if f_isMax(swing_length) and high[swing_length] > lastLow
            lastHigh := high[swing_length]
            timeHigh := bar_index
            dirUp := false
            li := f_drawLine()


            li

Overview

Price Action Candles with Zig Zag detects swing highs and lows over a user-set length, then labels them as higher high, lower high, higher low, or lower low when price-action labels are enabled. A zigzag line between swings can be shown separately. Point-of-interest sensitivity and width control how zones are sized, using ATR in the zone logic. Zigzag and swing labels are off by default. It does not place trades or stops.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

SMC / ICT / price action, 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
Swing High/Low Lengthint5swing_length
POI Sensitivityfloat2.5poi_sensitvity
Show Zig Zagboolfalseshow_zigzag
Show Price Action Labelsboolfalseshow_price_action_labels
Supplycolorcolor.new(#EDEDED,70)supply_color
Outlinecolorcolor.new(color.white,75)supply_outline_color
Demandcolorcolor.new(#00FFFF,70)demand_color
Outlinecolorcolor.new(color.white,75)demand_outline_color
BOScolorcolor.new(#EDEDED,70)bos_color
Labelcolorcolor.whitebos_label_color
POI Labelcolorcolor.whitepoi_label_color
Widthfloat2.5poi_width
Show all 14 settings
SettingTypeDefaultVariable
Price Action Labelcolorcolor.grayswing_type_color
Zig Zagcolorcolor.new(#000000,0)zigzag_color
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

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

DE-tracker

by Allen1212
303 stars

This indicator plots labels on the TradingView price chart to mark ARK Invest's buy and sell trades for the DE stock. Buy transactions appear in green with X1 or X2 multipliers showing the number of ARK ETFs involved on the same day; sells appear in red. The m

Indicator v4 Support & resistanceSMC / ICT / price action
Updated 2 years ago · None stated GitHub repository of DE-tracker

This Pine Script indicator plots a ZigZag on the price chart. It identifies reversal points when price moves more than a set percentage from the last pivot. The script calculates forecasts using zigzag data and labels detected geometric patterns including harm

Indicator v4 TrendSupport & resistance

Golden Bullet Strategy

by TradersPost
218 stars

The Golden Bullet Strategy is an overlay signal indicator based on Smart Money Concepts and time-based liquidity models. It identifies liquidity sweeps of a designated reference session range during a specified hunt window. It then confirms Market Structure Sh

Indicator v6 SMC / ICT / price actionSessions & time
Updated 3 months ago · None stated GitHub repository of Golden Bullet Strategy

Open Source Fractal

by cantolab
183 stars

This indicator implements the Fractal Model concept to plot market structure shifts, liquidity sweeps, and Change in State of Delivery (CISD) levels. It overlays custom higher-timeframe candle previews, open prices, and time markers directly on the price chart

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · MIT GitHub repository of Open Source Fractal

Swing Failure Signals

by Ganador1
154 stars

This indicator calculates pivot highs and pivot lows to detect liquidity sweeps and Swing Failure Patterns. It identifies when price breaks past recent key swing highs or lows within a configurable historical window, then looks for a quick reversal or change i

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · Apache-2.0 GitHub repository of Swing Failure Signals

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