Model+ - Trendlines & S/R

indicator("Model+ - Trendlines & S/R", overlay=true)
// === Parameters ===
length = input.int(20, title="Pivot Length")
lookback = input.int(252, title="Lookback Period (trading days ~ 1 year)", minval=1, maxval=5000)
minTouches = input.int(2, title="Minimum Touches for Valid S/R")
maxLines = input.int(15, title="Max Lines")
tolerance = input.float(1.5, title="Price Tolerance for S/R Match")
// === Arrays to Store Pivot Points ===
var line[] supportLines = array.new_line()
var line[] resistanceLines = array.new_line()
var float[] supportLevels = array.new_float()
var float[] resistanceLevels = array.new_float()
// === Function to Check Pivot High ===
isPivotHigh(src, len, idx) =>
idxValid = idx - len >= 0 and idx + len < bar_index and idx < 5000
result = true
if idxValid
for j = 1 to len
result := result and src[idx] > src[idx - j] and src[idx] > src[idx + j]
else
result := false
result
// === Function to Check Pivot Low ===
isPivotLow(src, len, idx) =>
idxValid = idx - len >= 0 and idx + len < bar_index and idx < 5000
result = true
if idxValid
for j = 1 to len
result := result and src[idx] < src[idx - j] and src[idx] < src[idx + j]
else
result := false
result
// === Helper Function: Count Nearby Pivots ===
countTouches(src, level, lookbackBars) =>
count = 0
maxBack = math.min(lookbackBars, bar_index)
for j = 0 to maxBack - 1
if math.abs(src[j] - level) <= tolerance
count := count + 1
count
// === Loop Over Past Bars to Find S/R Levels ===
startIdx = math.max(length, bar_index - math.min(lookback, 4500))
endIdx = bar_index - length
if bar_index > startIdx + length
for i = startIdx to endIdx by 1
if isPivotHigh(high, length, i)
lvl = high
touches = countTouches(high, lvl, lookback)
if touches >= minTouches
l = line.new(x1=i, y1=lvl, x2=bar_index, y2=lvl, color=color.red, width=1)
array.push(resistanceLines, l)
array.push(resistanceLevels, lvl)
if isPivotLow(low, length, i)
lvl = low
touches = countTouches(low, lvl, lookback)
if touches >= minTouches
l = line.new(x1=i, y1=lvl, x2=bar_index, y2=lvl, color=color.green, width=1)
array.push(supportLines, l)
array.push(supportLevels, lvl)
// === Clean Up Old Lines ===
while array.size(resistanceLines) > maxLines
line.delete(array.shift(resistanceLines))
array.shift(resistanceLevels)
while array.size(supportLines) > maxLines
line.delete(array.shift(supportLines))
array.shift(supportLevels)
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.
Penafian
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.