Indicator · Pine Script v6 · On the chart

Support and Resistance

by rubbermetal· code credited to claygrace74

Support and Resistance finds pivot highs and lows using configurable left and right bar counts and keeps the most recent levels, merging nearby pivots into one zone and counting touches.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code224
Settings11inputs
LicenceMPL-2.0
Last update4 months ago10 Jun 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

224 lines of Pine Script v6 from Support and Resistance.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

Support and Resistance.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © claygrace74

//@version=6
indicator("Support and Resistance", shorttitle="S/R", overlay=true, max_lines_count=500, max_labels_count=500)

// ==================== Pivot Settings ====================
var piv_group = "Pivot Detection"
leftBars   = input.int(10, "Left Bars", minval=2, tooltip="Bars to the left of pivot point", group=piv_group)
rightBars  = input.int(10, "Right Bars", minval=2, tooltip="Bars to the right to confirm pivot", group=piv_group)
maxLevels  = input.int(8, "Max Levels Shown", minval=2, maxval=20, group=piv_group)
zonePct    = input.float(0.3, "Zone Width (%)", minval=0.05, step=0.05, tooltip="S/R zone thickness as % of price", group=piv_group)

// ==================== Display Settings ====================
var disp_group = "Display Settings"
i_showZones    = input.bool(true, "Show S/R Zones", group=disp_group)
i_showSignals  = input.bool(true, "Show Break/Test Signals", group=disp_group)
i_showLabels   = input.bool(true, "Show Level Labels", group=disp_group)
i_showEMA      = input.bool(true, "Show Zero-Lag EMA", group=disp_group)
emaLen         = input.int(21, "EMA Length", minval=1, group=disp_group)
resColor       = input.color(color.red, "Resistance Color", group=disp_group)
supColor       = input.color(color.green, "Support Color", group=disp_group)

// ==================================================================================
// Pivot Detection
// ==================================================================================
pivHigh = ta.pivothigh(high, leftBars, rightBars)
pivLow  = ta.pivotlow(low, leftBars, rightBars)

// ==================================================================================
// Level Tracking (arrays of price levels + metadata)
// ==================================================================================
var float[] resLevels  = array.new_float(0)
var int[]   resTouches = array.new_int(0)
var int[]   resBars    = array.new_int(0)
var line[]  resLines   = array.new_line(0)
var box[]   resBoxes   = array.new_box(0)
var label[] resLabels  = array.new_label(0)

var float[] supLevels  = array.new_float(0)
var int[]   supTouches = array.new_int(0)
var int[]   supBars    = array.new_int(0)
var line[]  supLines   = array.new_line(0)
var box[]   supBoxes   = array.new_box(0)
var label[] supLabels  = array.new_label(0)

// Zone width in price
zoneWidth = close * zonePct / 100

// Check if a new level is near an existing one (merge)
f_nearExisting(levels, price, threshold) =>
    bool found = false
    int idx = -1
    if array.size(levels) > 0
        for i = 0 to array.size(levels) - 1
            if math.abs(array.get(levels, i) - price) <= threshold
                found := true
                idx := i
                break
    [found, idx]

// Add or merge a resistance level
if not na(pivHigh)
    [exists, idx] = f_nearExisting(resLevels, pivHigh, zoneWidth)
    if exists and idx >= 0
        array.set(resTouches, idx, array.get(resTouches, idx) + 1)
        array.set(resBars, idx, bar_index - rightBars)
    else
        array.push(resLevels, pivHigh)
        array.push(resTouches, 1)
        array.push(resBars, bar_index - rightBars)

// Add or merge a support level
if not na(pivLow)
    [exists, idx] = f_nearExisting(supLevels, pivLow, zoneWidth)
    if exists and idx >= 0
        array.set(supTouches, idx, array.get(supTouches, idx) + 1)
        array.set(supBars, idx, bar_index - rightBars)
    else
        array.push(supLevels, pivLow)
        array.push(supTouches, 1)
        array.push(supBars, bar_index - rightBars)

// ==================================================================================
// Prune broken levels (price closed decisively through)
// ==================================================================================
// Remove resistance levels that price closed above
if array.size(resLevels) > 0
    for i = array.size(resLevels) - 1 to 0
        lvl = array.get(resLevels, i)
        if close > lvl + zoneWidth * 2 and close[1] > lvl + zoneWidth * 2
            array.remove(resLevels, i)
            array.remove(resTouches, i)
            array.remove(resBars, i)

// Remove support levels that price closed below
if array.size(supLevels) > 0
    for i = array.size(supLevels) - 1 to 0
        lvl = array.get(supLevels, i)
        if close < lvl - zoneWidth * 2 and close[1] < lvl - zoneWidth * 2
            array.remove(supLevels, i)
            array.remove(supTouches, i)
            array.remove(supBars, i)

// Cap array sizes to maxLevels (keep most-touched levels)
f_capLevels(levels, touches, bars, maxSize) =>
    while array.size(levels) > maxSize
        // Find level with fewest touches
        int minIdx = 0
        int minVal = array.get(touches, 0)
        for i = 1 to array.size(touches) - 1
            if array.get(touches, i) < minVal
                minVal := array.get(touches, i)
                minIdx := i
        array.remove(levels, minIdx)
        array.remove(touches, minIdx)
        array.remove(bars, minIdx)

f_capLevels(resLevels, resTouches, resBars, maxLevels)
f_capLevels(supLevels, supTouches, supBars, maxLevels)

// ==================================================================================
// Draw S/R Zones
// ==================================================================================

// Clear previous drawings
if array.size(resLines) > 0
    for ln in resLines
        line.delete(ln)
    array.clear(resLines)
if array.size(resBoxes) > 0
    for bx in resBoxes
        box.delete(bx)
    array.clear(resBoxes)
if array.size(resLabels) > 0
    for lb in resLabels
        label.delete(lb)
    array.clear(resLabels)
if array.size(supLines) > 0
    for ln in supLines
        line.delete(ln)
    array.clear(supLines)
if array.size(supBoxes) > 0
    for bx in supBoxes
        box.delete(bx)
    array.clear(supBoxes)
if array.size(supLabels) > 0
    for lb in supLabels
        label.delete(lb)
    array.clear(supLabels)

// Draw resistance zones
if i_showZones and array.size(resLevels) > 0
    for i = 0 to array.size(resLevels) - 1
        lvl     = array.get(resLevels, i)
        touches = array.get(resTouches, i)
        startBar = array.get(resBars, i)
        // Opacity based on touches: more touches = more opaque
        alpha = math.max(95 - touches * 10, 60)
        ln = line.new(startBar, lvl, bar_index + 20, lvl, color=color.new(resColor, alpha - 30), width=1, style=line.style_solid)
        array.push(resLines, ln)
        bx = box.new(startBar, lvl + zoneWidth, bar_index + 20, lvl - zoneWidth, bgcolor=color.new(resColor, alpha), border_color=color(na))
        array.push(resBoxes, bx)
        if i_showLabels
            lb = label.new(bar_index + 22, lvl, "R" + str.tostring(touches), style=label.style_none, textcolor=color.new(resColor, alpha - 40), size=size.small)
            array.push(resLabels, lb)

// Draw support zones
if i_showZones and array.size(supLevels) > 0
    for i = 0 to array.size(supLevels) - 1
        lvl     = array.get(supLevels, i)
        touches = array.get(supTouches, i)
        startBar = array.get(supBars, i)
        alpha = math.max(95 - touches * 10, 60)
        ln = line.new(startBar, lvl, bar_index + 20, lvl, color=color.new(supColor, alpha - 30), width=1, style=line.style_solid)
        array.push(supLines, ln)
        bx = box.new(startBar, lvl + zoneWidth, bar_index + 20, lvl - zoneWidth, bgcolor=color.new(supColor, alpha), border_color=color(na))
        array.push(supBoxes, bx)
        if i_showLabels
            lb = label.new(bar_index + 22, lvl, "S" + str.tostring(touches), style=label.style_none, textcolor=color.new(supColor, alpha - 40), size=size.small)
            array.push(supLabels, lb)

// ==================================================================================
// Signal Detection — proximity, breakout, retest
// ==================================================================================
bool nearRes = false
bool nearSup = false
bool resBreak = false
bool supBreak = false

if array.size(resLevels) > 0
    for i = 0 to array.size(resLevels) - 1
        lvl = array.get(resLevels, i)
        if high >= lvl - zoneWidth and high <= lvl + zoneWidth
            nearRes := true
        if close > lvl + zoneWidth and close[1] <= lvl + zoneWidth
            resBreak := true

if array.size(supLevels) > 0
    for i = 0 to array.size(supLevels) - 1
        lvl = array.get(supLevels, i)
        if low <= lvl + zoneWidth and low >= lvl - zoneWidth
            nearSup := true
        if close < lvl - zoneWidth and close[1] >= lvl - zoneWidth
            supBreak := true

plotshape(i_showSignals and resBreak, style=shape.triangleup,   location=location.abovebar, color=color.lime,   text="Break", textcolor=color.lime,   size=size.tiny, title="Resistance Break")
plotshape(i_showSignals and supBreak, style=shape.triangledown, location=location.belowbar, color=color.orange, text="Break", textcolor=color.orange, size=size.tiny, title="Support Break")

// ==================================================================================
// Zero-Lag EMA
// ==================================================================================
emaStd     = ta.ema(close, emaLen)
emaZeroLag = 2 * emaStd - ta.ema(emaStd, emaLen)
emaColor   = emaZeroLag > emaZeroLag[1] ? color.green : color.red
plot(i_showEMA ? emaZeroLag : na, "Zero-Lag EMA", color=emaColor, linewidth=2)

// ==================================================================================
// Alert Conditions
// ==================================================================================
alertcondition(resBreak,  "Resistance Breakout", "Price broke above a resistance zone")
alertcondition(supBreak,  "Support Breakdown",   "Price broke below a support zone")
alertcondition(nearRes,   "Near Resistance",     "Price testing a resistance zone")
alertcondition(nearSup,   "Near Support",        "Price testing a support zone")

Overview

Zones are drawn as boxes and lines with optional labels, plus break and test markers. A zero-lag EMA can be shown. Because each pivot needs right-side bars to confirm, levels appear after a delay equal to the right bar count. It draws on the price chart.

Details

Published by

rubbermetal on GitHub

Code credit

claygrace74, as written in the script's header

Type

Indicator, Pine Script v6

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
Left Barsint10leftBars
Right Barsint10rightBars
Max Levels Shownint8maxLevels
Zone Width (%)float0.3zonePct
Show S/R Zonesbooltruei_showZones
Show Break/Test Signalsbooltruei_showSignals
Show Level Labelsbooltruei_showLabels
Show Zero-Lag EMAbooltruei_showEMA
EMA Lengthint21emaLen
Resistance Colorcolorcolor.redresColor
Support Colorcolorcolor.greensupColor
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 11 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 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