Snd//@version=5
indicator("Supply and Demand Zones", overlay=true)
// Input parameters for length of high and low
len = input(20, title="Length for high/low")
// Detecting local highs and lows
highs = ta.highest(high, len)
lows = ta.lowest(low, len)
// Plotting supply zones (resistance) when a new local high is detected
var float supply_level = na
if (high == highs)
supply_level := high
line.new(x1=bar_index, y1=supply_level, x2=bar_index + 1, y2=supply_level, color=color.red, width=2, extend=extend.right)
// Plotting demand zones (support) when a new local low is detected
var float demand_level = na
if (low == lows)
demand_level := low
line.new(x1=bar_index, y1=demand_level, x2=bar_index + 1, y2=demand_level, color=color.green, width=2, extend=extend.right)
// Ensuring the supply and demand zones are displayed on the chart
if (supply_level)
plot(supply_level, title="Supply Level", style=plot.style_linebr, color=color.red, linewidth=2)
if (demand_level)
plot(demand_level, title="Demand Level", style=plot.style_linebr, color=color.green, linewidth=2)