本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Auto Support & Resistance", overlay=true, max_lines_count=500, max_bars_back=5000)
// ───────────── INPUTS ─────────────
colorSup = input.color(#09ff00, "Support Color (Green)")
colorRes = input.color(#fd0202, "Resistance Color (Red)")
strengthSR = input.int(2, "S/R Strength (min pivots touching level)", 1)
clusterPerc = input.float(10, "Cluster Width % of 300-bar range", 0.5, 30, step=0.5)
maxLevels = input.int(10, "Max S/R levels shown", 1, 20)
lineStyle = input.string("Dotted", "Line Style", ["Solid", "Dotted", "Dashed"])
lineWidth = input.int(2, "Line Width", 1)
expandSR = input.bool(true, "Extend lines both sides")
// ───────────── CORE ─────────────
rb = 10 // pivot left/right bars
prd = 284 // lookback for scanning pivots
lnStyle = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ? line.style_dotted : line.style_dashed
extd = expandSR ? extend.both : extend.right
float ph = ta.pivothigh(high, rb, rb)
float pl = ta.pivotlow(low, rb, rb)
prdhighest = ta.highest(300)
prdlowest = ta.lowest(300)
cwidth = (prdhighest - prdlowest) * clusterPerc / 100
var sr_levs = array.new_float(21, na)
// Recalculate levels only when a new pivot is confirmed
if not na(ph) or not na(pl)
sr_levels = array.new_float(21, na)
aas = array.new_bool(41, true)
countpp = 0
for x = 0 to prd
if na(close[x])
break
if not na(ph[x]) or not na(pl[x])
countpp += 1
if countpp > 40
break
if array.get(aas, countpp)
upl = (not na(ph[x]) ? high[x + rb] : low[x + rb]) + cwidth
dnl = (not na(ph[x]) ? high[x + rb] : low[x + rb]) - cwidth
tmp = array.new_bool(41, true)
cnt = 0
tpoint = 0
for xx = 0 to prd
if na(close[xx])
break
if not na(ph[xx]) or not na(pl[xx])
chg = false
cnt += 1
if cnt > 40
break
if array.get(aas, cnt)
if not na(ph[xx])
if high[xx + rb] <= upl and high[xx + rb] >= dnl
tpoint += 1
chg := true
if not na(pl[xx])
if low[xx + rb] <= upl and low[xx + rb] >= dnl
tpoint += 1
chg := true
if chg and cnt < 41
array.set(tmp, cnt, false)
if tpoint >= strengthSR
for g = 0 to 40
if not array.get(tmp, g)
array.set(aas, g, false)
if not na(ph[x]) and countpp < 21
array.set(sr_levels, countpp, high[x + rb])
if not na(pl[x]) and countpp < 21
array.set(sr_levels, countpp, low[x + rb])
for x = 0 to 20
array.set(sr_levs, x, array.get(sr_levels, x))
// ───────────── DRAWING (last bar only) ─────────────
var sr_lines = array.new<line>(21, na)
if barstate.islast
for x = 0 to 20
line.delete(array.get(sr_lines, x))
x1 = math.max(bar_index - 355, 0)
drawn = 0
for x = 0 to 20
lvl = array.get(sr_levs, x)
if not na(lvl) and drawn < maxLevels
drawn += 1
line_col = close >= lvl ? colorSup : colorRes
array.set(sr_lines, x, line.new(x1, lvl, bar_index, lvl, xloc.bar_index, extd, line_col, lnStyle, lineWidth))