Here's a Pine Script implementation that dynamically identifies support/resistance levels with volatility-based filtering and reliability tracking:
//@version=5
indicator("Dynamic Support/Resistance with Volatility Filtering", overlay=true)
// Inputs
length = input.int(14, "Pivot Lookback Length")
volatilityPeriod = input.int(14, "ATR Period")
thresholdMult = input.float(1.0, "Threshold Multiplier")
maxLevels = input.int(20, "Max Levels to Track")
// Calculate ATR for volatility measurement
atr = ta.atr(volatilityPeriod)
// Arrays to store levels and test counts
var float[] supportLevels = array.new_float()
var float[] resistanceLevels = array.new_float()
var int[] supportTests = array.new_int()
var int[] resistanceTests = array.new_int()
// Function to check proximity to existing levels
isNear(level, array<float> levels, float threshold) =>
var result = false
for i = 0 to array.size(levels) - 1
if math.abs(level - array.get(levels, i)) <= threshold
result := true
break
result
// Detect new swing highs/lows and add to arrays if not near existing levels
newHigh = ta.pivothigh(high, length, length)
newLow = ta.pivotlow(low, length, length)
// Add new resistance level
if not na(newHigh) and array.size(resistanceLevels) < maxLevels
if not isNear(newHigh, resistanceLevels, atr * thresholdMult)
array.unshift(resistanceLevels, newHigh)
array.unshift(resistanceTests, 0)
// Add new support level
if not na(newLow) and array.size(supportLevels) < maxLevels
if not isNear(newLow, supportLevels, atr * thresholdMult)
array.unshift(supportLevels, newLow)
array.unshift(supportTests, 0)
// Update test counts and remove broken levels
updateLevels(levels, tests, isResistance) =>
size = array.size(levels)
for i = 0 to size - 1
level = array.get(levels, i)
// Check for breakouts
if isResistance
if close < level - atr * thresholdMult
array.remove(levels, i)
array.remove(tests, i)
size -= 1
else
if close > level + atr * thresholdMult
array.remove(levels, i)
array.remove(tests, i)
size -= 1
// Check for tests (price interaction)
if low <= level + atr * thresholdMult and high >= level - atr * thresholdMult
array.set(tests, i, array.get(tests, i) + 1)
null
// Update both support and resistance levels
updateLevels(supportLevels, supportTests, false)
updateLevels(resistanceLevels, resistanceTests, true)
// Plot support/resistance levels with varying intensity based on test count
plotLevels(levels, tests, colorBase) =>
size = array.size(levels)
for i = 0 to size - 1
level = array.get(levels, i)
testCount = array.get(tests, i)
// Calculate line width and opacity based on test count
width = math.min(3, testCount)
opacity = math.min(100, testCount * 20)
line.new(x1=bar_index - 1, y1=level, x2=bar_index, y2=level,
color=color.from_opacity(colorBase, opacity),
width=width, extend=extend.right)
// Plot support and resistance lines
plotLevels(supportLevels, supportTests, color.green)
plotLevels(resistanceLevels, resistanceTests, color.red)
// Visual debugging
plotchar(array.size(supportLevels), title="Support Count", location=location.top)
plotchar(array.size(resistanceLevels), title="Resistance Count", location=location.top)
Key features of this implementation:
- Volatility-Based Filtering:
- Uses ATR (Average True Range) to dynamically adjust proximity thresholds
- New levels are only added if they're sufficiently far from existing levels
- Reliability Tracking:
- Counts how many times each level is tested (price interaction without breakout)
- Line thickness and opacity increase with the number of tests
- Dynamic Level Management:
- Removes levels when they're decisively broken (price closes beyond threshold)
- Limits maximum levels to prevent clutter
- Prioritizes newer levels through unshift() (adds to front of array)
- Visual Feedback:
- Support levels in green, resistance in red
- More tested levels appear thicker and more opaque
- Shows current level counts for monitoring
- Robust Detection Logic:
- Uses built-in pivot high/low detection
- Avoids duplicate levels through proximity checks
- Handles array operations carefully to prevent errors
The script automatically adapts to changing market conditions by:
- Using ATR for volatility scaling
- Removing invalidated levels
- Adjusting visual prominence based on reliability
- Limiting levels to prevent overcrowding
You can customize the input parameters to suit different trading styles:
- Shorter periods for more responsive levels
- Higher threshold multipliers for stricter filtering
- More levels for comprehensive analysis (at risk of clutter)