Indicator · Pine Script v5 · On the chart

Divergence-Support/Resistence

by regalouisei· code credited to HeWhoMustNotBeNamed

Divergence-Support/Resistence combines dual Zigzag pivot detection with momentum oscillators to identify divergence patterns and plot support and resistance levels directly on the price chart.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code105
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago25 Feb 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

105 lines of Pine Script v5 from pinescript/editors_picks/divergence-supportresistence.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

divergence-supportresistence.pine
// 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("Divergence-Support/Resistence", shorttitle="DSR", overlay=true, max_lines_count=500, max_labels_count=500, max_bars_back=500)
import HeWhoMustNotBeNamed/zigzag/3 as zg
import HeWhoMustNotBeNamed/enhanced_ta/8 as eta
import HeWhoMustNotBeNamed/supertrend/4 as st

maxItems = input.int(10, step=5, title="Max Depth")

showZigzag1 = input.bool(true, "", group="Zigzag", inline="z1")
zigzag1Length = input.int(8, "", group="Zigzag", inline="z1")

showZigzag2 = input.bool(true, "", group="Zigzag", inline="z2")
zigzag2Length = input.int(13, "", group="Zigzag", inline="z2")

var useAlternativeSource = true
source = close

oscillatorType = input.string('rsi', title='Oscillator Source       ', options=["cci", "cmo", "cog", "mfi", "roc", "rsi", "stoch", "tsi", "wpr"], group='Oscillator', inline='osc')
useExternalSource = input.bool(false, title='External Source', group='Oscillator', inline="osce")
externalSource = input.source(close, title='', group='Oscillator', inline="osce")

var history = 1
var waitForClose = true
var atrMaType = "rma"
var atrMultiplier = 1

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

add_to_line_array(arr, val, maxItems)=>
    array.unshift(arr, val)
    if(array.size(arr) > maxItems)
        line.delete(array.pop(arr))

add_to_label_array(arr, val, maxItems)=>
    array.unshift(arr, val)
    if(array.size(arr) > maxItems)
        label.delete(array.pop(arr))
        
getSentimentDetails(sentiment) =>
    [sentimentSymbol, sentimentColor] = switch sentiment
        4 => ['⬆', color.green]
        -4 => ['⬇', color.red]
        3 => ['↗', color.lime]
        -3 => ['↘', color.orange]
        2 => ['⤴',color.rgb(202, 224, 13, 0)]
        -2 => ['⤵',color.rgb(250, 128, 114, 0)] 
        => ['▣', color.silver]
    [sentimentSymbol, sentimentColor]

plot_support_resistence(showZigzag, zigzagLength, srArray, lnArray, lblArray, maxItems, largest)=>
    if(showZigzag)
        length = zigzagLength * 3
        longLength = zigzagLength*5
        atrLength = zigzagLength*5
        [oscillator, overbought, oversold] = eta.oscillator(oscillatorType, length, length, longLength)
        [dir_zigzag, supertrend] = st.supertrend_zigzag(length=zigzagLength, history = history, useAlternativeSource = useAlternativeSource, alternativeSource=source,
                                         waitForClose=waitForClose, atrlength=atrLength, multiplier=atrMultiplier, atrMaType=atrMaType)
        [zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagpivotratios, zigzagoscillators,
                                             zigzagoscillatordirs, zigzagtrendbias, zigzagdivergence, 
                                             newPivot, doublePivot] = 
                                             zg.zigzag(zigzagLength, oscillatorSource=useExternalSource ? externalSource : oscillator, directionBias = dir_zigzag)
        if(array.size(zigzagpivots)>1)
            price = array.get(zigzagpivots, 1)
            divergence = array.get(zigzagdivergence, 1)
            
            if(math.abs(divergence) == 2 or math.abs(divergence) == 3)
                if(array.indexof(srArray, price) == -1)
                    add_to_array(srArray, price, maxItems)
                    [sentimentSymbol, sentimentColor] = getSentimentDetails(divergence)
                    lblSize = largest? size.normal : size.small
                    lnStyle = largest? line.style_solid : line.style_dashed
                    ln = line.new(time, price, time+1, price, extend=extend.right, xloc=xloc.bar_time, color=sentimentColor, style=lnStyle, width=largest? 1 : 0)
                    lbl = label.new(time, price, sentimentSymbol, color=sentimentColor, xloc=xloc.bar_time, textcolor=sentimentColor, style=label.style_none, yloc=yloc.price, size=lblSize)
                    add_to_line_array(lnArray, ln, maxItems)
                    add_to_label_array(lblArray, lbl, maxItems)

var srArray1 = array.new_float()
var srArray2 = array.new_float()

var lnArray1 = array.new_line()
var lblArray1 = array.new_label()

var lnArray2 = array.new_line()
var lblArray2 = array.new_label()

plot_support_resistence(showZigzag1, zigzag1Length, srArray1, lnArray1, lblArray1, maxItems, zigzag1Length>zigzag2Length)
plot_support_resistence(showZigzag2, zigzag2Length, srArray2, lnArray2, lblArray2, maxItems, zigzag2Length>zigzag1Length)

Overview

Users can select from various built-in oscillators, such as RSI, CCI, Stochastic, or MFI, or supply a custom external source. The indicator draws dynamic levels based on historical swing highs and lows. Note that Zigzag-based pivot calculations naturally repaint historical bars as new price extremes form.

Details

Published by

regalouisei on GitHub

Code credit

HeWhoMustNotBeNamed, 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
Max Depthint10maxItems
showZigzag1booltrueshowZigzag1
zigzag1Lengthint8zigzag1Length
showZigzag2booltrueshowZigzag2
zigzag2Lengthint13zigzag2Length
Oscillator Source       stringrsioscillatorType
External SourceboolfalseuseExternalSource
externalSourcesourcecloseexternalSource
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

23 stars

This indicator collection calculates and plots previous daily high and low price levels on the chart to identify potential supply and demand zones. It includes real-time intraday high and low calculations, RSI divergence detection across a customizable period

Indicator v4 Support & resistanceDivergence
Updated 3 years ago · None stated GitHub repository of Daily High and Low Strategy

This indicator implements a full Chanlun pipeline on the price chart. It merges contained bars, marks top and bottom fractals, and connects alternating fractals into strokes. Overlapping strokes form pivot boxes, and MACD area ratios flag trend or consolidatio

Indicator v6 Support & resistanceDivergence
Updated 2 months ago · GPL-3.0 GitHub repository of 缠论 - 分型与笔 [Chanlun Core]
5 stars

This indicator plots RSI(14) on the price chart along with regular and hidden bullish and bearish divergences. It detects divergences by comparing price highs and lows to RSI values using pivot points with configurable lookback bars. Signals appear as triangle

Indicator v4 Oscillators & momentumDivergence
Updated 2 years ago · None stated GitHub repository of CB RSI (Divergence and OB/OS)

This price-chart indicator finds swing highs and lows with a user-set pivot length and classifies them as higher high, lower high, higher low, or lower low. It tracks a trend state and the latest swing signal from those pivots. The same swing comparison is run

Indicator v5 Candlestick patternsSupport & resistance
Updated 5 months ago · None stated GitHub repository of Price Action Candles with Pivot Trendlines

SAB PRO Pivot Master

by IAMATOMIC001
3 stars

SAB PRO Pivot Master is a price-chart overlay that builds an RSX-style oscillator from a chosen source and length, then compares price extremes with that oscillator over a lookback window to mark pivots and bullish or bearish divergence. Overbought and oversol

Indicator v5 Oscillators & momentumVolatility & bands
Updated 12 months ago · None stated GitHub repository of SAB PRO Pivot Master
2 stars

This indicator detects regular and hidden divergences across multiple technical indicators including RSI, MACD, Stochastic, CCI, momentum, OBV, and others. It calculates pivot points on a chosen source to identify bullish and bearish divergences, then plots co

Indicator v4 DivergenceOscillators & momentum
Updated 4 years ago · MIT GitHub repository of Divergence for Many Indicators v4

More Divergence 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