Structure Midline [@Mishu]

Mishundare · study · 83 行 · 点赞 27 · TradingView 原页

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

Pine Script

//@version=6
// © Mishundare | IntelligenceFlow
// Successor of "Entry Helper [@Mishu]" (2024), rewritten and extended.
indicator("Structure Midline [@Mishu]", shorttitle = "SML", overlay = true)
 
// ───── Inputs ─────
grpS = "Structure"
brkLen = input.int(1, "Breakout lookback (bars)", minval = 1, group = grpS, tooltip = "1 = a close above the previous bar's high starts an up-leg (classic Entry Helper logic). Higher values produce fewer, larger legs.")
brkAtr = input.float(0.0, "Min. breakout distance (× ATR)", minval = 0.0, step = 0.05, group = grpS, tooltip = "Requires the close to clear the reference high/low by this fraction of the ATR. 0 = off.")
atrLen = input.int(14, "ATR length", minval = 1, group = grpS)
 
grpV = "Display"
showTB    = input.bool(true, "Top / bottom", group = grpV)
showMid   = input.bool(true, "Midline", group = grpV)
showZone  = input.bool(true, "Shade upper / lower half", group = grpV)
showFlips = input.bool(true, "Mark structure flips", group = grpV)
colorBars = input.bool(false, "Color bars by bias", group = grpV)
colTop    = input.color(#f11405, "Top", group = grpV)
colBot    = input.color(#4caf50, "Bottom", group = grpV)
colMid    = input.color(#0a58e9, "Midline", group = grpV)
 
// ───── Calculation ─────
atr      = nz(ta.atr(atrLen))
refHigh  = ta.highest(high, brkLen)[1]
refLow   = ta.lowest(low, brkLen)[1]
upBreak  = ta.crossover(close, refHigh + brkAtr * atr)
dnBreak  = ta.crossunder(close, refLow - brkAtr * atr)
 
var int   dir     = 0      // 1 = up-leg, -1 = down-leg
var float legHigh = na
var float legLow  = na
var float top     = na
var float bottom  = na
 
if upBreak and not dnBreak
    if dir != 1
        legHigh := high
    dir := 1
else if dnBreak and not upBreak
    if dir != -1
        legLow := low
    dir := -1
 
if dir == 1
    legHigh := math.max(nz(legHigh, high), high)
    top := legHigh
else if dir == -1
    legLow := math.min(nz(legLow, low), low)
    bottom := legLow
 
mid  = (top + bottom) / 2
bull = close > mid
bear = close < mid
 
flipUp = dir == 1 and dir[1] == -1 and barstate.isconfirmed
flipDn = dir == -1 and dir[1] == 1 and barstate.isconfirmed
midUp  = ta.crossover(close, mid) and barstate.isconfirmed
midDn  = ta.crossunder(close, mid) and barstate.isconfirmed
 
// ───── Plots ─────
plot(showTB ? top : na, "Top", ta.change(top) != 0 ? na : colTop, style = plot.style_linebr)
plot(showTB ? bottom : na, "Bottom", ta.change(bottom) != 0 ? na : colBot, style = plot.style_linebr)
plot(showMid ? mid : na, "Midline", colMid, 2)
 
pTopF = plot(top, "Top (fill)", color = na, editable = false, display = display.none)
pMidF = plot(mid, "Mid (fill)", color = na, editable = false, display = display.none)
pBotF = plot(bottom, "Bottom (fill)", color = na, editable = false, display = display.none)
fill(pTopF, pMidF, color = showZone ? color.new(colTop, 92) : na, title = "Upper half")
fill(pMidF, pBotF, color = showZone ? color.new(colBot, 92) : na, title = "Lower half")
 
plotshape(showFlips and flipUp, "Flip up",   shape.labelup,   location.belowbar, colBot, size = size.tiny)
plotshape(showFlips and flipDn, "Flip down", shape.labeldown, location.abovebar, colTop, size = size.tiny)
 
barcolor(colorBars ? (bull ? colBot : bear ? colTop : na) : na)
 
// ───── Alerts ─────
alertcondition(flipUp, "Structure flip up",   "SML: structure flipped up on {{ticker}} ({{interval}})")
alertcondition(flipDn, "Structure flip down", "SML: structure flipped down on {{ticker}} ({{interval}})")
alertcondition(midUp,  "Close above midline", "SML: close above midline on {{ticker}} ({{interval}})")
alertcondition(midDn,  "Close below midline", "SML: close below midline on {{ticker}} ({{interval}})")
 
if flipUp or flipDn
    alert("SML structure flip " + (flipUp ? "up" : "down") + " | " + syminfo.ticker + " | " + timeframe.period, alert.freq_once_per_bar_close)

← 返回列表