Pine Script Supply and Demand Indicator Tradingview

Pine Script Supply and Demand Indicator Tradingview

A Supply and Demand indicator in Pine Script generally highlights key areas on a chart where price reversals or consolidations occur due to supply (resistance) and demand (support) zones. Here's a basic version of a Supply and Demand indicator using Pine Script.

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

// Input settings for lookback periods
lookback_high = input.int(10, title="Lookback Period for Highs")
lookback_low = input.int(10, title="Lookback Period for Lows")
zone_width = input.int(3, title="Zone Width")

// Detecting supply (resistance) zones
var float supply_zone_high = na
var float supply_zone_low = na
if ta.highestbars(high, lookback_high) == 0
    supply_zone_high := high
    supply_zone_low := high - zone_width * syminfo.mintick

// Detecting demand (support) zones
var float demand_zone_low = na
var float demand_zone_high = na
if ta.lowestbars(low, lookback_low) == 0
    demand_zone_low := low
    demand_zone_high := low + zone_width * syminfo.mintick

// Plotting the supply zone
hline(supply_zone_high, "Supply Zone High", color=color.red, linewidth=2)
hline(supply_zone_low, "Supply Zone Low", color=color.red, linewidth=1, linestyle=hline.style_dotted)
box.new(left=bar_index[lookback_high], top=supply_zone_high, right=bar_index, bottom=supply_zone_low, bgcolor=color.new(color.red, 90), border_color=color.red)

// Plotting the demand zone
hline(demand_zone_low, "Demand Zone Low", color=color.green, linewidth=2)
hline(demand_zone_high, "Demand Zone High", color=color.green, linewidth=1, linestyle=hline.style_dotted)
box.new(left=bar_index[lookback_low], top=demand_zone_high, right=bar_index, bottom=demand_zone_low, bgcolor=color.new(color.green, 90), border_color=color.green)

        

Explanation:

  1. Lookback Period: lookback_high and lookback_low are used to set how far back the script should look to identify key highs and lows, representing supply (resistance) and demand (support) areas.
  2. Supply Zone: It looks for recent highs (ta.highestbars) within the given lookback_high period and defines a supply zone (resistance). This zone has a thickness based on zone_width.
  3. Demand Zone: It looks for recent lows (ta.lowestbars) within the given lookback_low period and defines a demand zone (support). Like the supply zone, it has a thickness based on zone_width.
  4. Plotting:

  • hline is used to mark the upper and lower bounds of the supply and demand zones.
  • box.new is used to draw a shaded area between the upper and lower bounds to visually highlight the zones.

You can adjust the lookback period and zone width to fit your trading strategy. Would you like to add additional features or modify this script?

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics