Indicator · Pine Script v6 · On the chart

Fresh Supply & Demand Zones

by kvmafthab-pixel· code credited to Fresh Supply & Demand Zone Detector

Fresh Supply & Demand Zones is a Pine Script v6 indicator that detects and plots fresh supply and demand zones on the price chart.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code175
Settings16inputs
LicenceCustom licencefile: MPL-2.0
Last update5 months ago26 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

175 lines of Pine Script v6 from indicators/fresh-supply-demand-zones.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

fresh-supply-demand-zones.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
// © Fresh Supply & Demand Zone Detector

//@version=6
indicator("Fresh Supply & Demand Zones", shorttitle="Fresh S&D", overlay=true, max_boxes_count=500, max_labels_count=200)

string GRP_DET = "Zone Detection"
int    atrLen    = input.int(14,  "ATR Period",              minval=5,   maxval=50,  group=GRP_DET)
float  bodyMult  = input.float(1.5,"Body ÷ ATR Threshold",  minval=1.0, maxval=5.0, step=0.1, group=GRP_DET, tooltip="How many times larger than ATR the candle body must be to qualify as momentum")
int    baseLB    = input.int(5,   "Base Lookback Bars",      minval=1,   maxval=20,  group=GRP_DET, tooltip="Max bars to search back for the base (source) candle")
bool   useRange  = input.bool(false, "Range Mode (cluster base)", group=GRP_DET)
int    rangeLB   = input.int(3,   "Range Cluster Size",      minval=2,   maxval=10,  group=GRP_DET)

string GRP_MGT = "Zone Management"
string mitType   = input.string("Wick", "Mitigation Trigger",  options=["Wick", "Close"], group=GRP_MGT)
int    mitBuffer = input.int(3,   "Min Bars Before Mitigation", minval=1, maxval=20,  group=GRP_MGT)
int    maxPerSide = input.int(15, "Max Zones Per Side",        minval=1,  maxval=50,  group=GRP_MGT)
bool   showMit   = input.bool(false, "Show Mitigated Zones (gray)", group=GRP_MGT)
bool   showLabels = input.bool(true, "Show Zone Labels",       group=GRP_MGT)

string GRP_FLT = "Filters"
float  minZoneATR = input.float(0.3, "Min Zone Height (× ATR)", minval=0.1, maxval=2.0, step=0.1, group=GRP_FLT)
float  maxZoneATR = input.float(3.0, "Max Zone Height (× ATR)", minval=1.0, maxval=10.0, step=0.5, group=GRP_FLT)

string GRP_STY = "Style"
color  supFill   = input.color(color.new(#ef5350, 80), "Supply Fill",   group=GRP_STY)
color  supBord   = input.color(color.new(#ef5350, 40), "Supply Border", group=GRP_STY)
color  demFill   = input.color(color.new(#26a69a, 80), "Demand Fill",   group=GRP_STY)
color  demBord   = input.color(color.new(#26a69a, 40), "Demand Border", group=GRP_STY)

float atrVal   = ta.atr(atrLen)
float bodySize = math.abs(close - open)

bool bullMom = bodySize > atrVal * bodyMult and close > open
bool bearMom = bodySize > atrVal * bodyMult and close < open

var array<box>   supBoxes = array.new_box()
var array<float> supTops  = array.new_float()
var array<float> supBots  = array.new_float()
var array<int>   supBars  = array.new_int()
var array<label> supLbls  = array.new_label()

var array<box>   demBoxes = array.new_box()
var array<float> demTops  = array.new_float()
var array<float> demBots  = array.new_float()
var array<int>   demBars  = array.new_int()
var array<label> demLbls  = array.new_label()

findBaseIndex(bool seekBullish, int lb) =>
    int idx = 1
    for i = 1 to lb
        bool match = seekBullish ? (close[i] > open[i]) : (close[i] < open[i])
        if match
            idx := i
            break
    idx

getZoneBounds(int baseIdx, bool rangeMode, int clusterSize) =>
    float zTop = high[baseIdx]
    float zBot = low[baseIdx]
    int   leftIdx = baseIdx
    if rangeMode
        for i = baseIdx to math.min(baseIdx + clusterSize - 1, 499)
            zTop := math.max(zTop, high[i])
            zBot := math.min(zBot, low[i])
            leftIdx := i
    [zTop, zBot, leftIdx]

isDuplicate(array<float> tops, array<float> bots, float newTop, float newBot) =>
    bool exists = false
    if array.size(tops) > 0
        float tol = atrVal * 0.08
        for i = 0 to array.size(tops) - 1
            if math.abs(array.get(tops, i) - newTop) < tol and math.abs(array.get(bots, i) - newBot) < tol
                exists := true
                break
    exists

trimOldest(array<box> boxes, array<float> tops, array<float> bots, array<int> bars, array<label> lbls, int maxCount) =>
    if array.size(boxes) > maxCount
        box.delete(array.shift(boxes))
        label.delete(array.shift(lbls))
        array.shift(tops)
        array.shift(bots)
        array.shift(bars)

color mitFill = color.new(color.gray, 93)
color mitBord = color.new(color.gray, 80)
color mitText = color.new(color.gray, 60)

if array.size(supBoxes) > 0
    for i = array.size(supBoxes) - 1 to 0
        if bar_index >= array.get(supBars, i) + mitBuffer
            float zBot = array.get(supBots, i)
            bool  hit  = mitType == "Wick" ? high >= zBot : close >= zBot
            if hit
                box   b = array.get(supBoxes, i)
                label l = array.get(supLbls, i)
                if showMit
                    box.set_extend(b, extend.none)
                    box.set_right(b, bar_index)
                    box.set_bgcolor(b, mitFill)
                    box.set_border_color(b, mitBord)
                    label.set_textcolor(l, mitText)
                else
                    box.delete(b)
                    label.delete(l)
                array.remove(supBoxes, i)
                array.remove(supTops, i)
                array.remove(supBots, i)
                array.remove(supBars, i)
                array.remove(supLbls, i)

if array.size(demBoxes) > 0
    for i = array.size(demBoxes) - 1 to 0
        if bar_index >= array.get(demBars, i) + mitBuffer
            float zTop = array.get(demTops, i)
            bool  hit  = mitType == "Wick" ? low <= zTop : close <= zTop
            if hit
                box   b = array.get(demBoxes, i)
                label l = array.get(demLbls, i)
                if showMit
                    box.set_extend(b, extend.none)
                    box.set_right(b, bar_index)
                    box.set_bgcolor(b, mitFill)
                    box.set_border_color(b, mitBord)
                    label.set_textcolor(l, mitText)
                else
                    box.delete(b)
                    label.delete(l)
                array.remove(demBoxes, i)
                array.remove(demTops, i)
                array.remove(demBots, i)
                array.remove(demBars, i)
                array.remove(demLbls, i)

if bearMom
    int baseIdx = findBaseIndex(true, baseLB)
    [zTop, zBot, leftIdx] = getZoneBounds(baseIdx, useRange, rangeLB)
    float zHeight = zTop - zBot
    bool validHeight = zHeight >= atrVal * minZoneATR and zHeight <= atrVal * maxZoneATR
    if validHeight and not isDuplicate(supTops, supBots, zTop, zBot)
        box   b = box.new(bar_index[leftIdx], zTop, bar_index, zBot, bgcolor=supFill, border_color=supBord, border_width=1, extend=extend.right)
        label l = label.new(bar_index[leftIdx], zTop, showLabels ? "S" : "", style=label.style_label_down, color=color.new(color.white, 100), textcolor=supBord, size=size.small)
        array.push(supBoxes, b)
        array.push(supTops, zTop)
        array.push(supBots, zBot)
        array.push(supBars, bar_index)
        array.push(supLbls, l)
        trimOldest(supBoxes, supTops, supBots, supBars, supLbls, maxPerSide)

if bullMom
    int baseIdx = findBaseIndex(false, baseLB)
    [zTop, zBot, leftIdx] = getZoneBounds(baseIdx, useRange, rangeLB)
    float zHeight = zTop - zBot
    bool validHeight = zHeight >= atrVal * minZoneATR and zHeight <= atrVal * maxZoneATR
    if validHeight and not isDuplicate(demTops, demBots, zTop, zBot)
        box   b = box.new(bar_index[leftIdx], zTop, bar_index, zBot, bgcolor=demFill, border_color=demBord, border_width=1, extend=extend.right)
        label l = label.new(bar_index[leftIdx], zBot, showLabels ? "D" : "", style=label.style_label_up, color=color.new(color.white, 100), textcolor=demBord, size=size.small)
        array.push(demBoxes, b)
        array.push(demTops, zTop)
        array.push(demBots, zBot)
        array.push(demBars, bar_index)
        array.push(demLbls, l)
        trimOldest(demBoxes, demTops, demBots, demBars, demLbls, maxPerSide)

var table infoTbl = table.new(position.top_right, 2, 2, bgcolor=color.new(color.black, 85), border_width=1, border_color=color.new(color.gray, 70))
if barstate.islast
    table.cell(infoTbl, 0, 0, "Supply", text_color=supBord, text_size=size.small)
    table.cell(infoTbl, 1, 0, str.tostring(array.size(supBoxes)), text_color=supBord, text_size=size.small)
    table.cell(infoTbl, 0, 1, "Demand", text_color=demBord, text_size=size.small)
    table.cell(infoTbl, 1, 1, str.tostring(array.size(demBoxes)), text_color=demBord, text_size=size.small)

alertcondition(bearMom, "New Supply Zone", "Fresh supply zone formed")
alertcondition(bullMom, "New Demand Zone", "Fresh demand zone formed")

Overview

Fresh Supply & Demand Zones identifies momentum candles based on ATR period and body size threshold, then scans back for base candles to form zones. Zones can use range mode for clustering or default single-candle approach. Mitigation triggers on wicks or closes after configurable bars. Filters limit zone height in ATR multiples and prevent duplicates. It draws colored boxes for supply (red) and demand (green) zones with optional labels and gray ghost zones for mitigated ones. Main settings include ATR length, body multiplier, lookback bars, range mode, and max zones per side. It works on any timeframe and instrument but is optimized for SMC trading on forex and gold.

Details

Published by

kvmafthab-pixel on GitHub

Code credit

Fresh Supply & Demand Zone Detector, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

SMC / ICT / price action, Support & resistance, Volatility & bands, Oscillators & momentum

Markets

Forex, Gold

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.

Topics

pinescript-tradingview-trading-xauusdtechnical-analysis-smart-money

Settings

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

SettingTypeDefaultVariable
ATR Periodint14atrLen
Body ÷ ATR Thresholdfloat1.5bodyMult
Base Lookback Barsint5baseLB
Range Mode (cluster base)boolfalseuseRange
Range Cluster Sizeint3rangeLB
Mitigation TriggerstringWickmitType
Min Bars Before Mitigationint3mitBuffer
Max Zones Per Sideint15maxPerSide
Show Mitigated Zones (gray)boolfalseshowMit
Show Zone LabelsbooltrueshowLabels
Min Zone Height (× ATR)float0.3minZoneATR
Max Zone Height (× ATR)float3.0maxZoneATR
Show all 16 settings
SettingTypeDefaultVariable
Supply Fillcolorcolor.new(#ef5350, 80)supFill
Supply Bordercolorcolor.new(#ef5350, 40)supBord
Demand Fillcolorcolor.new(#26a69a, 80)demFill
Demand Bordercolorcolor.new(#26a69a, 40)demBord
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

This overlay indicator scores liquidity-sweep reversals on the price chart. It looks for a swing high or low violation followed by a recovery close, then grades optional conditions such as RSI and structure into a 0–2 quality score with separate long and short

Indicator v6 SMC / ICT / price actionOscillators & momentum
Updated yesterday · MIT GitHub repository of Reversal Engine Score v1 [WavesUnchained]

ICT Liquidity Void Fill

by ArunKBhaskar
575 stars

This overlay indicator marks ICT liquidity void fills on the price chart and can show long and short signals. It places a Fibonacci level inside a displacement candle, defaulting to the midpoint, and draws lines in a chosen style. Optional filters limit the fi

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Liquidity Void Fill
218 stars

This indicator maps market structure and supply and demand zones on the price chart. It marks validated and optional unvalidated swing highs and lows using a pivot length and extra confirmation bars. Consolidation and impulse moves are measured against ATR to

Indicator v6 SMC / ICT / price actionSupport & resistance Repaints
Updated 3 months ago · None stated GitHub repository of Market Structure + Supply/Demand
55 stars

This Pine Script indicator overlays institutional liquidity levels on the price chart. It identifies order block levels using pivot highs and lows, then draws mean reversion lines from the 20 and 50 period simple moving averages, a volatility range midpoint pr

Indicator v6 Support & resistanceSMC / ICT / price action Repaints
Updated 6 months ago · Apache-2.0 GitHub repository of AAPL Institutional Liquidity Grid

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