Support & Resistance Zones

SahilTradeSchool · study · 93 行 · 点赞 368 · TradingView 原页

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

Pine Script

//@version=6
indicator("Support & Resistance Zones", overlay = true, max_lines_count = 500, max_boxes_count = 500)
 
// ───────── Inputs ─────────
lenL       = input.int(10, "Pivot Left Bars", minval = 1, group = "Pivot Settings")
lenR       = input.int(10, "Pivot Right Bars", minval = 1, group = "Pivot Settings")
maxLvls    = input.int(5, "Max Levels Per Side", minval = 1, maxval = 15, group = "Pivot Settings")
 
atrLen     = input.int(14, "ATR Length", minval = 1, group = "Zone Settings")
zoneMult   = input.float(0.25, "Zone Half-Thickness (x ATR)", minval = 0.0, step = 0.05, group = "Zone Settings")
mergeMult  = input.float(0.5, "Merge Distance (x ATR)", minval = 0.0, step = 0.05, group = "Zone Settings")
showZones  = input.bool(true, "Show Zone Boxes", group = "Zone Settings")
zoneTransp = input.int(85, "Zone Transparency", minval = 0, maxval = 100, group = "Zone Settings")
 
breakType  = input.string("Close", "Breakout Confirmation", options = ["Close", "Wick"], group = "Breakouts")
showBreaks = input.bool(true, "Show Breakout Markers", group = "Breakouts")
 
resCol     = input.color(color.red, "Resistance Color", group = "Style")
supCol     = input.color(color.green, "Support Color", group = "Style")
lineWidth  = input.int(2, "Line Width", minval = 1, maxval = 5, group = "Style")
 
// ───────── Types & Storage ─────────
type Level
    float price
    line  ln
    box   bx
 
var array<Level> resLvls = array.new<Level>()
var array<Level> supLvls = array.new<Level>()
 
atr = ta.atr(atrLen)
th  = atr * zoneMult
tol = atr * mergeMult
 
// ───────── Helpers ─────────
f_exists(array<Level> arr, float p, float tolerance) =>
    bool found = false
    for l in arr
        if math.abs(l.price - p) <= tolerance
            found := true
    found
 
f_add(array<Level> arr, float p, float thick, color c) =>
    int x1 = bar_index - lenR
    line ln = line.new(x1, p, bar_index, p, color = c, width = lineWidth)
    box  bx = box.new(x1, p + thick, bar_index, p - thick, border_color = na, bgcolor = showZones ? color.new(c, zoneTransp) : na)
    arr.push(Level.new(p, ln, bx))
    if arr.size() > maxLvls
        Level old = arr.shift()
        line.delete(old.ln)
        box.delete(old.bx)
 
f_update(array<Level> arr, bool isRes, float thick) =>
    bool broke = false
    int i = arr.size() - 1
    while i >= 0
        Level l = arr.get(i)
        line.set_x2(l.ln, bar_index)
        box.set_right(l.bx, bar_index)
        bool hit = false
        if barstate.isconfirmed
            if isRes
                hit := (breakType == "Close" ? close : high) > l.price + thick
            else
                hit := (breakType == "Close" ? close : low) < l.price - thick
        if hit
            line.delete(l.ln)
            box.delete(l.bx)
            arr.remove(i)
            broke := true
        i -= 1
    broke
 
// ───────── Pivot Detection ─────────
ph = ta.pivothigh(high, lenL, lenR)
pl = ta.pivotlow(low, lenL, lenR)
 
if not na(ph) and not na(atr) and not f_exists(resLvls, ph, tol)
    f_add(resLvls, ph, th, resCol)
 
if not na(pl) and not na(atr) and not f_exists(supLvls, pl, tol)
    f_add(supLvls, pl, th, supCol)
 
// ───────── Update & Breakouts ─────────
resBreak = f_update(resLvls, true, th)
supBreak = f_update(supLvls, false, th)
 
plotshape(showBreaks and resBreak, "Resistance Break", shape.triangleup, location.belowbar, color.new(supCol, 0), size = size.small, text = "BO")
plotshape(showBreaks and supBreak, "Support Break", shape.triangledown, location.abovebar, color.new(resCol, 0), size = size.small, text = "BD")
 
// ───────── Alerts ─────────
alertcondition(resBreak, "Resistance Breakout", "Price broke above resistance on {{ticker}} ({{interval}})")
alertcondition(supBreak, "Support Breakdown", "Price broke below support on {{ticker}} ({{interval}})")

← 返回列表