Model+ - Dynamic Trendlines

indicator("Model+ - Dynamic Trendlines", overlay=true)
// === Helper: Detect Swing Highs and Lows ===
isSwingLow(idx) => low[idx] < low[idx - 1] and low[idx] < low[idx + 1]
isSwingHigh(idx) => high[idx] > high[idx - 1] and high[idx] > high[idx + 1]
// === Function to find trendlines ===
findTrendline(_isLow, length) =>
var float point1 = na
var float point2 = na
var int idx1 = na
var int idx2 = na
for i = length to 1 by -1
if _isLow ? isSwingLow(i) : isSwingHigh(i)
if na(point1)
point1 := _isLow ? low : high
idx1 := bar_index - i
else if na(point2)
point2 := _isLow ? low : high
idx2 := bar_index - i
break
slope = (point2 - point1) / (idx2 - idx1)
offset = bar_index - idx2
start = point2 + slope * (-offset)
end = point2 + slope * (1 - offset)
[start, end]
// === Dynamic detection per timeframe ===
timeframeName = timeframe.period
len = switch timeframeName
"D" => 50
"W" => 30
"M" => 20
=> 50
// === Detect trendlines ===
[startLow, endLow] = findTrendline(true, len)
[startHigh, endHigh] = findTrendline(false, len)
// === Draw lines ===
var line trendSupport = na
var line trendResistance = na
if not na(startLow) and not na(endLow)
line.delete(trendSupport)
trendSupport := line.new(x1=bar_index - 1, y1=startLow, x2=bar_index, y2=endLow, color=color.green, width=2, style=line.style_solid)
if not na(startHigh) and not na(endHigh)
line.delete(trendResistance)
trendResistance := line.new(x1=bar_index - 1, y1=startHigh, x2=bar_index, y2=endHigh, color=color.red, width=2, style=line.style_solid)
// === Support/Resistance Horizontal (based on swing points) ===
getHorizontalLevels(n) =>
var float support = na
var float resistance = na
for i = n to 1 by -1
if isSwingLow(i) and na(support)
support := low
if isSwingHigh(i) and na(resistance)
resistance := high
if not na(support) and not na(resistance)
break
[support, resistance]
[supportLine, resistanceLine] = getHorizontalLevels(len)
var line srSupportLine = na
var line srResistanceLine = na
if not na(supportLine)
line.delete(srSupportLine)
srSupportLine := line.new(x1=bar_index - len, y1=supportLine, x2=bar_index, y2=supportLine, color=color.green, style=line.style_dashed)
if not na(resistanceLine)
line.delete(srResistanceLine)
srResistanceLine := line.new(x1=bar_index - len, y1=resistanceLine, x2=bar_index, y2=resistanceLine, color=color.red, style=line.style_dashed)
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.