//@version=5
indicator("Enhanced Support & Resistance Power Zones", "Enhanced S&R Power", overlay=true, max_labels_count=500, max_boxes_count=100)
// ============================================================================================================
// USER INPUTS
// ============================================================================================================
// Period Settings
grp1 = "═══ Period Settings ═══"
length = input.int(130, "Lookback Period", minval=20, maxval=500, group=grp1)
extend = input.int(30, "Extension Length", minval=20, maxval=100, group=grp1)
atr_mult = input.float(0.5, "ATR Multiplier for Zone Width", minval=0.1, maxval=2.0, step=0.1, group=grp1)
atr_length = input.int(200, "ATR Period", minval=10, maxval=500, group=grp1)
// Visual Settings
grp2 = "═══ Visual Settings ═══"
show_zones = input.bool(true, "Show S/R Zones", group=grp2)
show_signals = input.bool(true, "Show Entry Signals", group=grp2)
show_power = input.bool(true, "Show Power Analysis", group=grp2)
show_midline = input.bool(true, "Show Midline", group=grp2)
show_labels = input.bool(true, "Show Price Labels", group=grp2)
// Colors
grp3 = "═══ Colors ═══"
res_col = input.color(color.new(#ff1744, 0), "Resistance", group=grp3)
sup_col = input.color(color.new(#00e676, 0), "Support", group=grp3)
res_zone_col = input.color(color.new(#ff1744, 85), "Resistance Zone", group=grp3)
sup_zone_col = input.color(color.new(#00e676, 85), "Support Zone", group=grp3)
mid_col = input.color(color.gray, "Midline", group=grp3)
// Advanced Settings
grp4 = "═══ Advanced Settings ═══"
use_volume_weight = input.bool(true, "Use Volume Weighting", group=grp4)
min_touches = input.int(2, "Min Touches for Validation", minval=1, maxval=5, group=grp4)
zone_merge_threshold = input.float(0.3, "Zone Merge Threshold %", minval=0.1, maxval=1.0, step=0.1, group=grp4)
// ============================================================================================================
// TYPE DEFINITIONS
// ============================================================================================================
type ZoneData
line res_line
line sup_line
box res_box
box sup_box
label res_label
label sup_label
label mid_label
line mid_line
int buy_power = 0
int sell_power = 0
float res_level = na
float sup_level = na
int res_touches = 0
int sup_touches = 0
// ============================================================================================================
// FUNCTIONS
// ============================================================================================================
// Calculate zone levels with improved accuracy
calculate_levels(length) =>
highs = array.new<float>()
lows = array.new<float>()
volumes = array.new<float>()
for i = 0 to length - 1
array.push(highs, high[i])
array.push(lows, low[i])
array.push(volumes, volume[i])
// Find max/min with volume weighting if enabled
max_val = array.max(highs)
min_val = array.min(lows)
if use_volume_weight
// Find volume-weighted levels
max_idx = array.indexof(highs, max_val)
min_idx = array.indexof(lows, min_val)
// Look for other significant highs/lows with high volume
for i = 0 to array.size(highs) - 1
if highs.get(i) > max_val * (1 - zone_merge_threshold/100) and volumes.get(i) > array.avg(volumes)
if volumes.get(i) > volumes.get(max_idx)
max_val := highs.get(i)
max_idx := i
for i = 0 to array.size(lows) - 1
if lows.get(i) < min_val * (1 + zone_merge_threshold/100) and volumes.get(i) > array.avg(volumes)
if volumes.get(i) > volumes.get(min_idx)
min_val := lows.get(i)
min_idx := i
[max_val, min_val, array.indexof(highs, max_val), array.indexof(lows, min_val)]
// Count zone touches for validation
count_touches(level, zone_width, is_resistance) =>
touches = 0
for i = 1 to length - 1
if is_resistance
if high[i] >= level - zone_width and high[i] <= level + zone_width and high[i-1] < level - zone_width
touches += 1
else
if low[i] <= level + zone_width and low[i] >= level - zone_width and low[i-1] > level + zone_width
touches += 1
touches
// Calculate buy/sell power with volume consideration
calculate_power() =>
buy_power = 0
sell_power = 0
buy_volume = 0.0
sell_volume = 0.0
for i = 0 to length - 1
if close[i] > open[i]
buy_power += 1
buy_volume += volume[i]
else if close[i] < open[i]
sell_power += 1
sell_volume += volume[i]
total_volume = buy_volume + sell_volume
buy_strength = total_volume > 0 ? (buy_volume / total_volume) * 100 : 50
sell_strength = total_volume > 0 ? (sell_volume / total_volume) * 100 : 50
[buy_power, sell_power, buy_strength, sell_strength]
// Generate entry signals with improved logic
generate_signals(data, zone_width) =>
// Clear old signals
for lbl in label.all
if label.get_text(lbl) == "▲" or label.get_text(lbl) == "▼"
label.delete(lbl)
if not show_signals
return
// Look for breakout/retest signals
for i = 1 to math.min(length - 1, 100)
// Support bounce signal
if low[i] <= data.sup_level + zone_width and low[i] >= data.sup_level - zone_width
if close[i] > open[i] and low[i+1] > data.sup_level + zone_width
if data.sup_touches >= min_touches
label.new(bar_index - i, low[i], "▲", color=color.new(sup_col, 0),
style=label.style_label_up, size=size.small,
tooltip="Support Bounce\nTouches: " + str.tostring(data.sup_touches))
// Resistance rejection signal
if high[i] >= data.res_level - zone_width and high[i] <= data.res_level + zone_width
if close[i] < open[i] and high[i+1] < data.res_level - zone_width
if data.res_touches >= min_touches
label.new(bar_index - i, high[i], "▼", color=color.new(res_col, 0),
style=label.style_label_down, size=size.small,
tooltip="Resistance Rejection\nTouches: " + str.tostring(data.res_touches))
// Clean up old drawings
cleanup(data) =>
line.delete(data.res_line)
line.delete(data.sup_line)
box.delete(data.res_box)
box.delete(data.sup_box)
label.delete(data.res_label)
label.delete(data.sup_label)
label.delete(data.mid_label)
line.delete(data.mid_line)
// ============================================================================================================
// MAIN LOGIC
// ============================================================================================================
var ZoneData zone_data = ZoneData.new()
if barstate.islast
// Calculate ATR for zone width
atr = ta.atr(atr_length) * atr_mult
// Get levels
[max_level, min_level, max_idx, min_idx] = calculate_levels(length)
// Store levels
zone_data.res_level := max_level
zone_data.sup_level := min_level
// Count touches
zone_data.res_touches := count_touches(max_level, atr, true)
zone_data.sup_touches := count_touches(min_level, atr, false)
// Calculate power
[bp, sp, bs, ss] = calculate_power()
zone_data.buy_power := bp
zone_data.sell_power := sp
// Clean up old drawings
cleanup(zone_data)
// Draw zones
if show_zones
// Resistance zone
zone_data.res_line := line.new(bar_index - length, max_level, bar_index + extend, max_level,
color=res_col, width=2)
zone_data.res_box := box.new(bar_index - length, max_level + atr, bar_index + extend, max_level - atr,
border_color=color.new(res_col, 80), bgcolor=res_zone_col)
// Support zone
zone_data.sup_line := line.new(bar_index - length, min_level, bar_index + extend, min_level,
color=sup_col, width=2)
zone_data.sup_box := box.new(bar_index - length, min_level + atr, bar_index + extend, min_level - atr,
border_color=color.new(sup_col, 80), bgcolor=sup_zone_col)
// Add power text
if show_power
res_text = "Sell Power: " + str.tostring(sp) + " (" + str.tostring(ss, "#.#") + "%)"
sup_text = "Buy Power: " + str.tostring(bp) + " (" + str.tostring(bs, "#.#") + "%)"
box.set_text(zone_data.res_box, res_text)
box.set_text(zone_data.sup_box, sup_text)
box.set_text_halign(zone_data.res_box, text.align_left)
box.set_text_halign(zone_data.sup_box, text.align_left)
box.set_text_color(zone_data.res_box, chart.fg_color)
box.set_text_color(zone_data.sup_box, chart.fg_color)
// Draw midline
if show_midline
mid_level = (max_level + min_level) / 2
zone_data.mid_line := line.new(bar_index - length, mid_level, bar_index + extend, mid_level,
color=mid_col, style=line.style_dotted)
if show_labels
zone_data.mid_label := label.new(bar_index + extend, mid_level,
str.tostring(mid_level, "#.##") + " (Mid)",
color=color(na), textcolor=mid_col,
style=label.style_label_left)
// Draw price labels
if show_labels
zone_data.res_label := label.new(bar_index + extend + 10, max_level,
"↓ " + str.tostring(max_level, "#.##") +
"\nTouches: " + str.tostring(zone_data.res_touches),
color=color(na), textcolor=res_col,
style=label.style_label_center)
zone_data.sup_label := label.new(bar_index + extend + 10, min_level,
"↑ " + str.tostring(min_level, "#.##") +
"\nTouches: " + str.tostring(zone_data.sup_touches),
color=color(na), textcolor=sup_col,
style=label.style_label_center)
// Mark the actual high/low points
label.new(bar_index - max_idx, max_level, "●", color=color(na),
textcolor=res_col, size=size.normal, style=label.style_label_center,
tooltip="Highest High")
label.new(bar_index - min_idx, min_level, "●", color=color(na),
textcolor=sup_col, size=size.normal, style=label.style_label_center,
tooltip="Lowest Low")
// Generate signals
generate_signals(zone_data, atr)
// ============================================================================================================
// ALERTS
// ============================================================================================================
// Alert conditions
res_touch = high >= zone_data.res_level - ta.atr(atr_length) * atr_mult and
high <= zone_data.res_level + ta.atr(atr_length) * atr_mult
sup_touch = low <= zone_data.sup_level + ta.atr(atr_length) * atr_mult and
low >= zone_data.sup_level - ta.atr(atr_length) * atr_mult
alertcondition(res_touch, "Resistance Touch", "Price touching resistance zone")
alertcondition(sup_touch, "Support Touch", "Price touching support zone")
alertcondition(close > zone_data.res_level, "Resistance Break", "Price broke above resistance")
alertcondition(close < zone_data.sup_level, "Support Break", "Price broke below support")