Auto Support & Resistance

SahilTradeSchool · study · 162 行 · 点赞 2,067 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Auto Support & Resistance", overlay=true, max_lines_count=500, max_labels_count=500, max_bars_back=5000)
 
// ───────────── INPUTS ─────────────
enableSR    = input.bool(true, "Enable Support & Resistance", group="SR")
colorSup    = input.color(#09ff00, "Support Color", group="SR")
colorRes    = input.color(#fd0202, "Resistance Color", group="SR")
strengthSR  = input.int(2, "S/R Strength (min pivots touching level)", 1, group="SR")
clusterPerc = input.float(10, "Cluster Width % of 300-bar range", 0.5, 30, step=0.5, group="SR")
maxLevels   = input.int(10, "Max S/R levels shown", 1, 20, group="SR")
lineStyle   = input.string("Dotted", "Line Style", ["Solid", "Dotted", "Dashed"], group="SR")
lineWidth   = input.int(2, "S/R Line Width", 1, group="SR")
showLabels  = input.bool(true, "Show price labels", group="SR")
showHL      = input.bool(true, "Show High/Low Level lines", group="SR")
useZones    = input.bool(false, "Zones On/Off", group="SR")
useHLZones  = input.bool(false, "High Low Zones On/Off", group="SR")
zoneWidth   = input.int(2, "Zone Width %", 0, tooltip="Calculated as % of the distance between highest/lowest of the last 300 bars", group="SR")
expandSR    = input.bool(true, "Extend lines both sides", group="SR")
 
// ───────────── CORE ─────────────
rb       = 10   // pivot left/right bars
prd      = 284  // lookback for scanning pivots
labelLoc = 55   // label offset (bars to the right)
 
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
zonePerc   = (prdhighest - prdlowest) * zoneWidth / 100
 
var sr_levs         = array.new_float(21, na)
var float highestph = na
var float lowestpl  = 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)
    highestph := prdlowest
    lowestpl  := prdhighest
    countpp   = 0
    for x = 0 to prd
        if na(close[x])
            break
        if not na(ph[x]) or not na(pl[x])
            highestph := math.max(highestph, nz(ph[x], prdlowest), nz(pl[x], prdlowest))
            lowestpl  := math.min(lowestpl, nz(ph[x], prdhighest), nz(pl[x], prdhighest))
            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 line     highest_  = na
var line     lowest_   = na
var line     hi_f1     = na
var line     hi_f2     = na
var line     lo_f1     = na
var line     lo_f2     = na
var linefill hi_fill   = na
var linefill lo_fill   = na
var label    hlabel    = na
var label    llabel    = na
var sr_lines  = array.new<line>(21, na)
var sr_linesH = array.new<line>(21, na)
var sr_linesL = array.new<line>(21, na)
var sr_linesF = array.new<linefill>(21, na)
var sr_labels = array.new<label>(21, na)
 
if barstate.islast
    // clear everything from the previous tick
    line.delete(highest_)
    line.delete(lowest_)
    line.delete(hi_f1)
    line.delete(hi_f2)
    line.delete(lo_f1)
    line.delete(lo_f2)
    linefill.delete(hi_fill)
    linefill.delete(lo_fill)
    label.delete(hlabel)
    label.delete(llabel)
    for x = 0 to 20
        line.delete(array.get(sr_lines, x))
        line.delete(array.get(sr_linesH, x))
        line.delete(array.get(sr_linesL, x))
        linefill.delete(array.get(sr_linesF, x))
        label.delete(array.get(sr_labels, x))
 
    x1 = math.max(bar_index - 355, 0)
 
    if enableSR and not na(highestph) and not na(lowestpl)
        hi_col = close >= highestph ? colorSup : colorRes
        lo_col = close >= lowestpl ? colorSup : colorRes
 
        // Highest / lowest pivot level
        if showHL
            highest_ := line.new(x1, highestph, bar_index, highestph, xloc.bar_index, extd, hi_col, lnStyle, lineWidth)
            lowest_  := line.new(x1, lowestpl, bar_index, lowestpl, xloc.bar_index, extd, lo_col, lnStyle, lineWidth)
            if useHLZones
                hi_f1 := line.new(x1, highestph + zonePerc, bar_index, highestph + zonePerc, xloc.bar_index, extd, na)
                hi_f2 := line.new(x1, highestph - zonePerc, bar_index, highestph - zonePerc, xloc.bar_index, extd, na)
                lo_f1 := line.new(x1, lowestpl + zonePerc, bar_index, lowestpl + zonePerc, xloc.bar_index, extd, na)
                lo_f2 := line.new(x1, lowestpl - zonePerc, bar_index, lowestpl - zonePerc, xloc.bar_index, extd, na)
                hi_fill := linefill.new(hi_f1, hi_f2, color.new(hi_col, 80))
                lo_fill := linefill.new(lo_f1, lo_f2, color.new(lo_col, 80))
            hlabel := label.new(bar_index + labelLoc + 20, highestph, "High Level : " + str.tostring(highestph), color=hi_col, textcolor=#000000, style=label.style_label_down)
            llabel := label.new(bar_index + labelLoc + 20, lowestpl, "Low Level : " + str.tostring(lowestpl), color=lo_col, textcolor=#000000, style=label.style_label_up)
 
        // Auto S/R levels (draws up to "Max S/R levels shown")
        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))
                if useZones
                    array.set(sr_linesH, x, line.new(x1, lvl + zonePerc, bar_index, lvl + zonePerc, xloc.bar_index, extd, na))
                    array.set(sr_linesL, x, line.new(x1, lvl - zonePerc, bar_index, lvl - zonePerc, xloc.bar_index, extd, na))
                    array.set(sr_linesF, x, linefill.new(array.get(sr_linesH, x), array.get(sr_linesL, x), color.new(line_col, 80)))
                if showLabels
                    lab_style = close >= lvl ? label.style_label_up : label.style_label_down
                    array.set(sr_labels, x, label.new(bar_index + labelLoc, lvl, str.tostring(math.round_to_mintick(lvl)), color=line_col, textcolor=#000000, style=lab_style))

← 返回列表