Indicator · Pine Script v4 · On the chart

Supply & Demand

by stonkpunk· code credited to frozon

Supply & Demand marks pivot highs and pivot lows on the price chart and draws translucent demand and supply boxes when a three-bar price imbalance appears between those pivots.

View on GitHub

GitHub stars26on the repository
Pine versionv4older, still runs
Lines of code88
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago11 Sep 2024

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

88 lines of Pine Script v4 from indicator-supply-demand-zones/orig-code.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

orig-code.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © frozon

// Updates:
//  - update screenshot
//  - update zone discovery

//@version=4
study("Supply & Demand", overlay=true)

srcH = input(high, title="Pivot High", inline="Pivot High")
leftLenH = input(title="", type=input.integer, defval=2, minval=1, inline="Pivot High")
rightLenH = input(title="/", type=input.integer, defval=1, minval=1, inline="Pivot High")

srcL = input(low, title="Pivot Low ", inline="Pivot Low")
leftLenL = input(title="", type=input.integer, defval=2, minval=1, inline="Pivot Low")
rightLenL = input(title="/", type=input.integer, defval=1, minval=1, inline="Pivot Low")

demandBoxColor = input(title="Demand box color", type=input.color, defval=color.rgb(120, 255, 120, 60))
supplyBoxColor = input(title="Supply box color", type=input.color, defval=color.rgb(255, 120, 120, 60))

f_boxExistsInArray(top, bottom, boxArr) =>
    if array.size(boxArr) > 0
        for i = 0 to array.size(boxArr) - 1 by 1
            b = array.get(boxArr, i)
            boxTop = box.get_top(b)
            boxBot = box.get_bottom(array.get(boxArr, i))
            if boxTop == top and boxBot == bottom
                true
            else
                false
    else
        false

var box[] demandBoxes = array.new_box()
var box[] supplyBoxes = array.new_box()
var line[] testLine = array.new_line()

ph = pivothigh(srcH, leftLenH, rightLenH)
pl = pivotlow(srcL, leftLenL, rightLenL)

pvtH = 0.0
pvtL = 0.0
pvtHIdx = 0
pvtLIdx = 0
pvtH := na(ph) ? pvtH[1] : ph
pvtL := na(pl) ? pvtL[1] : pl
pvtHIdx := na(ph) ? pvtHIdx[1] : bar_index
pvtLIdx := na(pl) ? pvtLIdx[1] : bar_index

// Check if we releasized an imbalanced
if high[3] < low[1]
    // imbalanced up
    if pvtHIdx[3] < pvtLIdx[2]
        exists = f_boxExistsInArray(pvtH[3], pvtL[2], demandBoxes)
        if not exists
            array.push(demandBoxes, box.new(left=bar_index + 5, top=pvtH[3], right=bar_index + 20, bottom=pvtL[2], bgcolor=demandBoxColor, border_color=demandBoxColor))

if low[3] > high[1]
    // imbalanced down
    if pvtLIdx[3] < pvtHIdx[2]
        exists = f_boxExistsInArray(pvtH[3], pvtL[2], supplyBoxes)
        if not exists
            array.push(supplyBoxes, box.new(left=bar_index + 5, top=pvtH[2], right=bar_index + 20, bottom=pvtL[3], bgcolor=supplyBoxColor, border_color=supplyBoxColor))

if array.size(demandBoxes) > 0
    for i = array.size(demandBoxes) - 1 to 0 by 1
        dsBox = array.get(demandBoxes, i)
        top = box.get_top(dsBox)
        left = box.get_left(dsBox)
        if low <= top and left < bar_index
            box.delete(dsBox)
            array.remove(demandBoxes, i)

        if low > top
            box.set_right(dsBox, bar_index + 20)

if array.size(supplyBoxes) > 0
    for i = array.size(supplyBoxes) - 1 to 0 by 1
        dsBox = array.get(supplyBoxes, i)
        bottom = box.get_bottom(dsBox)
        left = box.get_left(dsBox)
        if high >= bottom and left < bar_index
            box.delete(dsBox)
            array.remove(supplyBoxes, i)

        if high < bottom
            box.set_right(dsBox, bar_index + 20)

Overview

Demand zones form after an upward imbalance when the stored pivot high precedes the pivot low; supply zones form after a downward imbalance with the opposite pivot order. Users set the pivot source, left and right confirmation lengths, and box colors. A zone extends to the right until price trades through it, then the script deletes that box. It does not generate entries or exits.

Details

Published by

stonkpunk on GitHub

Code credit

frozon, as written in the script's header

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Support & resistance, SMC / ICT / price action

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
Pivot HighsourcehighsrcH
leftLenHint2leftLenH
/int1rightLenH
Pivot Low sourcelowsrcL
leftLenLint2leftLenL
/int1rightLenL
Demand box colorcolorcolor.rgb(120, 255, 120, 60)demandBoxColor
Supply box colorcolorcolor.rgb(255, 120, 120, 60)supplyBoxColor
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