M1 / M5 Support-Resistance (Seconds chart)

logicalboga · study · 208 行 · 点赞 18 · TradingView 原页

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

Pine Script

//@version=6
indicator("M1 / M5 Support-Resistance (Seconds chart)", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ───────── Settings ─────────
grp1 = "Timeframe 1"
show1  = input.bool(true, "Show", group = grp1)
tf1    = input.timeframe("1", "Timeframe", group = grp1)
left1  = input.int(5, "Pivot left bars", minval = 1, group = grp1)
right1 = input.int(3, "Pivot right bars (confirmation)", minval = 1, group = grp1)
max1   = input.int(4, "Max R and S count (each)", minval = 1, maxval = 50, group = grp1)
rCol1  = input.color(color.new(color.red, 30), "Resistance color", group = grp1)
sCol1  = input.color(color.new(color.green, 30), "Support color", group = grp1)
w1     = input.int(1, "Line width", minval = 1, maxval = 5, group = grp1)
 
grp2 = "Timeframe 2"
show2  = input.bool(true, "Show", group = grp2)
tf2    = input.timeframe("5", "Timeframe", group = grp2)
left2  = input.int(5, "Pivot left bars", minval = 1, group = grp2)
right2 = input.int(3, "Pivot right bars (confirmation)", minval = 1, group = grp2)
max2   = input.int(3, "Max R and S count (each)", minval = 1, maxval = 50, group = grp2)
rCol2  = input.color(color.new(color.orange, 0), "Resistance color", group = grp2)
sCol2  = input.color(color.new(color.teal, 0), "Support color", group = grp2)
w2     = input.int(2, "Line width", minval = 1, maxval = 5, group = grp2)
 
grpG = "General"
brokenMode = input.string("Delete", "Broken level", options = ["Delete", "Dashed", "Keep"], group = grpG)
breakSrc   = input.string("Close", "Break confirmation", options = ["Close", "Wick"], group = grpG)
showLabels = input.bool(true, "Show labels", group = grpG)
 
// ───────── Data ─────────
type Level
    line  ln
    label lb
    bool  isRes
    bool  broken
 
// Pivots are computed on the higher timeframe and only confirmed AFTER the bar closes (no repaint)
f_piv(int l, int r) =>
    ph = ta.pivothigh(high, l, r)
    pl = ta.pivotlow(low, l, r)
    [ph[1], pl[1], time[r + 1]]
 
[ph1, pl1, t1] = request.security(syminfo.tickerid, tf1, f_piv(left1, right1), gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)
[ph2, pl2, t2] = request.security(syminfo.tickerid, tf2, f_piv(left2, right2), gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)
 
var array<Level> r1 = array.new<Level>()
var array<Level> s1 = array.new<Level>()
var array<Level> r2 = array.new<Level>()
var array<Level> s2 = array.new<Level>()
 
addLevel(array<Level> arr, float price, int t, color col, int w, bool isRes, string txt, int maxN) =>
    ln = line.new(t, price, time, price, xloc = xloc.bar_time, extend = extend.right, color = col, width = w)
    lb = showLabels ? label.new(time, price, txt + "  " + str.tostring(price, format.mintick), xloc = xloc.bar_time, style = label.style_label_left, color = color.new(color.black, 100), textcolor = col, size = size.small) : na
    arr.push(Level.new(ln, lb, isRes, false))
    if arr.size() > maxN
        old = arr.shift()
        line.delete(old.ln)
        label.delete(old.lb)
 
hiSrc = breakSrc == "Close" ? close : high
loSrc = breakSrc == "Close" ? close : low
 
manage(array<Level> arr) =>
    if arr.size() > 0 and brokenMode != "Keep"
        for i = arr.size() - 1 to 0
            lv = arr.get(i)
            if not lv.broken
                y = lv.ln.get_y1()
                isBroken = lv.isRes ? hiSrc > y : loSrc < y
                if isBroken
                    if brokenMode == "Delete"
                        line.delete(lv.ln)
                        label.delete(lv.lb)
                        arr.remove(i)
                    else
                        lv.ln.set_style(line.style_dashed)
                        lv.ln.set_extend(extend.none)
                        lv.ln.set_x2(time)
                        label.delete(lv.lb)
                        lv.broken := true
    // move labels next to the last bar
    if barstate.islast and arr.size() > 0
        for lv in arr
            if not na(lv.lb) and not lv.broken
                lv.lb.set_x(time)
 
// ───────── Drawing ─────────
if show1
    if not na(ph1)
        addLevel(r1, ph1, t1, rCol1, w1, true,  tf1 + " R", max1)
    if not na(pl1)
        addLevel(s1, pl1, t1, sCol1, w1, false, tf1 + " S", max1)
if show2
    if not na(ph2)
        addLevel(r2, ph2, t2, rCol2, w2, true,  tf2 + " R", max2)
    if not na(pl2)
        addLevel(s2, pl2, t2, sCol2, w2, false, tf2 + " S", max2)
 
manage(r1)
manage(s1)
manage(r2)
manage(s2)
 
// ───────── Anchored VWAP ─────────
grpV = "Anchored VWAP"
showV    = input.bool(true, "Show VWAP", group = grpV)
vMode    = input.string("Period", "Anchor type", options = ["Period", "Date/Time"], group = grpV)
vPeriod  = input.string("NY Session", "Period", options = ["NY Session", "Session (Day)", "Hour", "4 Hours", "Week", "Month"], group = grpV)
nySess   = input.session("0930-1600", "NY session hours (New York time)", group = grpV, tooltip = "Hours are based on the America/New_York time zone; daylight saving is adjusted automatically and is independent of broker time. For the Forex NY open you can use 0800-1700.")
onlySess = input.bool(false, "Show VWAP only during the session", group = grpV)
vTime    = input.time(timestamp("2026-09-30T00:00:00"), "Anchor date/time", group = grpV)
vSrc     = input.source(hlc3, "Source", group = grpV)
vCol     = input.color(color.fuchsia, "VWAP color", group = grpV)
showB    = input.bool(true, "Deviation bands", group = grpV)
bMult1   = input.float(1.0, "Band 1 multiplier", step = 0.5, group = grpV)
bMult2   = input.float(2.0, "Band 2 multiplier", step = 0.5, group = grpV)
 
chD  = timeframe.change("D")
ch60 = timeframe.change("60")
ch4H = timeframe.change("240")
chW  = timeframe.change("W")
chM  = timeframe.change("M")
 
inNY    = not na(time(timeframe.period, nySess, "America/New_York"))
nyStart = inNY and not inNY[1]
 
periodNew = switch vPeriod
    "NY Session"    => nyStart
    "Session (Day)" => chD
    "Hour"          => ch60
    "4 Hours"       => ch4H
    "Week"          => chW
    => chM
 
timeNew = time >= vTime and (na(time[1]) or time[1] < vTime)
isNew   = vMode == "Period" ? periodNew : timeNew
active  = vMode == "Period" or time >= vTime
 
var float sumPV  = na
var float sumV   = na
var float sumPV2 = na
 
nyMode = vMode == "Period" and vPeriod == "NY Session"
 
if isNew or (active and na(sumV) and not nyMode)
    sumPV  := 0.0
    sumV   := 0.0
    sumPV2 := 0.0
 
if active and not na(sumV)
    v = nz(volume)
    sumPV  += vSrc * v
    sumV   += v
    sumPV2 += vSrc * vSrc * v
 
visible = not (nyMode and onlySess and not inNY)
vwapVal = active and visible and sumV > 0 ? sumPV / sumV : na
dev     = active and sumV > 0 ? math.sqrt(math.max(sumPV2 / sumV - vwapVal * vwapVal, 0)) : na
 
pV  = plot(showV ? vwapVal : na, "VWAP", color = vCol, linewidth = 2)
pU1 = plot(showV and showB ? vwapVal + dev * bMult1 : na, "Upper band 1", color = color.new(vCol, 50))
pL1 = plot(showV and showB ? vwapVal - dev * bMult1 : na, "Lower band 1", color = color.new(vCol, 50))
pU2 = plot(showV and showB ? vwapVal + dev * bMult2 : na, "Upper band 2", color = color.new(vCol, 70))
pL2 = plot(showV and showB ? vwapVal - dev * bMult2 : na, "Lower band 2", color = color.new(vCol, 70))
fill(pU1, pL1, color = color.new(vCol, 92))
 
// ───────── Adaptive Moving Average (Kaufman / MT5 AMA) ─────────
// MT5 formula:
//   ER   = |price - price[N]| / Σ|price - price[1]|  (N bars)
//   SSC  = ER * (2/(fast+1) - 2/(slow+1)) + 2/(slow+1)
//   AMA  = AMA[1] + SSC² * (price - AMA[1])
f_ama(float src, int len, int fastLen, int slowLen) =>
    change = math.abs(src - src[len])
    noise  = math.sum(math.abs(src - src[1]), len)
    er     = noise != 0 ? change / noise : 0.0
    fsc    = 2.0 / (fastLen + 1)
    ssc    = 2.0 / (slowLen + 1)
    sc     = math.pow(nz(er) * (fsc - ssc) + ssc, 2)
    float ama = na
    ama := na(ama[1]) ? src : ama[1] + sc * (src - ama[1])
    ama
 
grpA1 = "AMA 1"
showA1  = input.bool(true, "Show", group = grpA1)
srcA1   = input.source(close, "Apply to", group = grpA1)
lenA1   = input.int(9, "Period", minval = 1, group = grpA1)
fastA1  = input.int(2, "Fast EMA", minval = 1, group = grpA1)
slowA1  = input.int(30, "Slow EMA", minval = 1, group = grpA1)
shiftA1 = input.int(0, "Shift", group = grpA1)
colA1   = input.color(color.yellow, "Color", group = grpA1)
wA1     = input.int(1, "Width", minval = 1, maxval = 5, group = grpA1)
 
grpA2 = "AMA 2"
showA2  = input.bool(true, "Show", group = grpA2)
srcA2   = input.source(close, "Apply to", group = grpA2)
lenA2   = input.int(180, "Period", minval = 1, group = grpA2)
fastA2  = input.int(2, "Fast EMA", minval = 1, group = grpA2)
slowA2  = input.int(30, "Slow EMA", minval = 1, group = grpA2)
shiftA2 = input.int(0, "Shift", group = grpA2)
colA2   = input.color(color.aqua, "Color", group = grpA2)
wA2     = input.int(2, "Width", minval = 1, maxval = 5, group = grpA2)
 
// Always calculated on the chart's current timeframe (like MT5)
ama1 = f_ama(srcA1, lenA1, fastA1, slowA1)
ama2 = f_ama(srcA2, lenA2, fastA2, slowA2)
 
plot(showA1 ? ama1 : na, "AMA 1", color = colA1, linewidth = wA1, offset = shiftA1)
plot(showA2 ? ama2 : na, "AMA 2", color = colA2, linewidth = wA2, offset = shiftA2)

← 返回列表