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)
Script open-source
In pieno spirito TradingView, il creatore di questo script lo ha reso open-source, in modo che i trader possano esaminarlo e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricorda che la ripubblicazione del codice è soggetta al nostro Regolamento.
Per un accesso rapido a un grafico, aggiungi questo script ai tuoi preferiti: per saperne di più clicca qui.
Declinazione di responsabilità
Script open-source
In pieno spirito TradingView, il creatore di questo script lo ha reso open-source, in modo che i trader possano esaminarlo e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricorda che la ripubblicazione del codice è soggetta al nostro Regolamento.
Per un accesso rapido a un grafico, aggiungi questo script ai tuoi preferiti: per saperne di più clicca qui.