Supply Demand Indicator in Pinescript

Post Reply
SamiFX
Expert Trader
Posts: 7
Joined: Thu Nov 30, 2023 9:44 am

Supply Demand Indicator in Pinescript

Post by SamiFX »

Supply and demand zones serve as an excellent indicator for tracking institutional activities.
There are 4 types of supply and demand zones.
  • Rally Base Rally
  • Rally Base Rally
  • Rally Base Drop
  • Drop Base Drop
In this tutorial, I have shown in detail how you can program your own supply and demand trading indicator in the pine script.

If you have any questions please let me know below

Below is a code for the indicator

Code: Select all

//@version=5
indicator("Supply and Demand Zones (RBR, RBD, DBR, DBD)", overlay=true)

// Function to calculate body-to-wick ratio
body_to_wick_ratio(candle_open, candle_close, candle_high, candle_low) =>
    body = math.abs(candle_close - candle_open)
    wick = math.abs(candle_high - candle_low) - body
    ratio = wick > 0 ? body / wick : 0
    ratio

// Define body-to-wick ratio thresholds
rally_drop_ratio_threshold = 0.7
base_ratio_threshold = 0.3
candle_range(candle_high, candle_low) =>
    crange = candle_high - candle_low
    crange

// Function for Rally Base Rally (RBR) Bullish
is_rbr_bullish() =>
    // Rally Candle 1 (Bullish)
    body_to_wick_ratio(open[2], close[2], high[2], low[2]) > rally_drop_ratio_threshold and
     close[2] > open[2] and
    // Base Candle (Bearish)
     body_to_wick_ratio(open[1], close[1], high[1], low[1]) < base_ratio_threshold and
     close[1] < open[1] and
     candle_range(high[1], low[1]) < candle_range(high[2], low[2]) and candle_range(high[1], low[1]) < candle_range(high, low) and
    // Rally Candle 2 (Bullish)
     body_to_wick_ratio(open, close, high, low) > rally_drop_ratio_threshold and
     close > open

// Function for Drop Base Rally (DBR) Bearish
is_dbr_bearish() =>
    // Drop Candle 1 (Bearish)
    body_to_wick_ratio(open[2], close[2], high[2], low[2]) > rally_drop_ratio_threshold and
     close[2] < open[2] and
    // Base Candle (Bearish)
     body_to_wick_ratio(open[1], close[1], high[1], low[1]) < base_ratio_threshold and
     close[1] < open[1] and
     candle_range(high[1], low[1]) < candle_range(high[2], low[2]) and candle_range(high[1], low[1]) < candle_range(high, low) and
    // Rally Candle 2 (Bullish)
     body_to_wick_ratio(open, close, high, low) > rally_drop_ratio_threshold and
     close > open

// Function for Drop Base Drop (DBD) Bearish
is_dbd_bearish() =>
    // Drop Candle 1 (Bearish)
    body_to_wick_ratio(open[2], close[2], high[2], low[2]) > rally_drop_ratio_threshold and
     close[2] < open[2] and
    // Base Candle (Bearish)
     body_to_wick_ratio(open[1], close[1], high[1], low[1]) < base_ratio_threshold and
     close[1] < open[1] and
     candle_range(high[1], low[1]) < candle_range(high[2], low[2]) and candle_range(high[1], low[1]) < candle_range(high, low) and
    // Drop Candle 2 (Bearish)
     body_to_wick_ratio(open, close, high, low) > rally_drop_ratio_threshold and
     close < open

// Function for Rally Base Drop (RBD) Bullish
is_rbd_bullish() =>
    // Rally Candle 1 (Bullish)
    body_to_wick_ratio(open[2], close[2], high[2], low[2]) > rally_drop_ratio_threshold and
     close[2] > open[2] and
    // Base Candle (Bearish)
     body_to_wick_ratio(open[1], close[1], high[1], low[1]) < base_ratio_threshold and
     close[1] < open[1] and
     candle_range(high[1], low[1]) < candle_range(high[2], low[2]) and candle_range(high[1], low[1]) < candle_range(high, low) and
    // Drop Candle 2 (Bearish)
     body_to_wick_ratio(open, close, high, low) > rally_drop_ratio_threshold and
     close < open

// Plot labels and lines for RBR, RBD, DBR, DBD patterns only on the base candle
if is_rbr_bullish()
    label.new(bar_index[1], low[1], "RBR", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small)

    // Draw the supply/demand zone as a rectangle at the base candle high and low
    box.new(bar_index[1], high[1], bar_index, low[1], border_color=color.green, bgcolor=color.new(color.green, 90))

if is_dbr_bearish()
    label.new(bar_index[1], low[1], "DBR", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small)

    // Draw the supply/demand zone as a rectangle at the base candle high and low
    box.new(bar_index[1], high[1], bar_index, low[1], border_color=color.green, bgcolor=color.new(color.green, 90))

if is_dbd_bearish()
    label.new(bar_index[1], high[1], "DBD", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)

    // Draw the supply/demand zone as a rectangle at the base candle high and low
    box.new(bar_index[1], high[1], bar_index, low[1], border_color=color.red, bgcolor=color.new(color.red, 90))

if is_rbd_bullish()
    label.new(bar_index[1], high[1], "RBD", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)

    // Draw the supply/demand zone as a rectangle at the base candle high and low
    box.new(bar_index[1], high[1], bar_index, low[1], border_color=color.red, bgcolor=color.new(color.red, 90))
kumarhemanshu
Posts: 1
Joined: Sat Feb 01, 2025 4:06 pm

Re: Supply Demand Indicator in Pinescript

Post by kumarhemanshu »

This Indicator is Awesome but please can you help me with Horizontal line as well with this..
Post Reply