Structure Bias MTF [@Mishu]

Mishundare · study · 108 行 · 点赞 5 · TradingView 原页

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

Pine Script

//@version=6
// © Mishundare | IntelligenceFlow
// Successor of "Entry Helper v2 [@Mishu]" (2024, invite-only), rewritten as a public script.
indicator("Structure Bias MTF [@Mishu]", shorttitle = "SBM", overlay = true)
 
// ───── Inputs ─────
grpT = "Timeframes"
tf1 = input.timeframe("5",  "Timeframe 1 (lower)",  group = grpT, tooltip = "Should be equal to or higher than the chart timeframe.")
tf2 = input.timeframe("60", "Timeframe 2 (higher)", group = grpT)
 
grpS = "Structure"
brkLen = input.int(1, "Breakout lookback (bars)", minval = 1, group = grpS)
brkAtr = input.float(0.0, "Min. breakout distance (× ATR)", minval = 0.0, step = 0.05, group = grpS)
atrLen = input.int(14, "ATR length", minval = 1, group = grpS)
 
grpV = "Display"
showMid1  = input.bool(true, "Midline TF 1", group = grpV)
showMid2  = input.bool(true, "Midline TF 2", group = grpV)
showTB    = input.bool(false, "Top / bottom lines", group = grpV)
colMid1   = input.color(#2962ff, "Midline TF 1", group = grpV)
colMid2   = input.color(#ff1744, "Midline TF 2", group = grpV)
colBull   = input.color(#26a69a, "Bullish", group = grpV)
colBear   = input.color(#ef5350, "Bearish", group = grpV)
showTable = input.bool(true, "Bias table", group = grpV)
tblPos    = input.string("Bottom Right", "Table position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpV)
tblSize   = input.string("Normal", "Table text size", options = ["Small", "Normal", "Large"], group = grpV)
 
// ───── Structure function ─────
// Returns the values of the last CLOSED bar of the requested timeframe.
structure() =>
    a       = nz(ta.atr(atrLen))
    refHigh = ta.highest(high, brkLen)[1]
    refLow  = ta.lowest(low, brkLen)[1]
    upBreak = ta.crossover(close, refHigh + brkAtr * a)
    dnBreak = ta.crossunder(close, refLow - brkAtr * a)
    var int   dir     = 0
    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
    [top[1], bottom[1], mid[1], close[1]]
 
[top1, bot1, mid1, cls1] = request.security(syminfo.tickerid, tf1, structure(), lookahead = barmerge.lookahead_on)
[top2, bot2, mid2, cls2] = request.security(syminfo.tickerid, tf2, structure(), lookahead = barmerge.lookahead_on)
 
bias1 = cls1 > mid1 ? 1 : cls1 < mid1 ? -1 : 0
bias2 = cls2 > mid2 ? 1 : cls2 < mid2 ? -1 : 0
combined = bias1 == 1 and bias2 == 1 ? 1 : bias1 == -1 and bias2 == -1 ? -1 : 0
 
alignedUp = combined == 1 and combined[1] != 1 and barstate.isconfirmed
alignedDn = combined == -1 and combined[1] != -1 and barstate.isconfirmed
 
// ───── Plots ─────
plot(showMid1 ? mid1 : na, "Midline TF 1", colMid1, 2)
plot(showMid2 ? mid2 : na, "Midline TF 2", colMid2, 2)
plot(showTB ? top1 : na, "Top TF 1",    color.new(colMid1, 60), style = plot.style_stepline)
plot(showTB ? bot1 : na, "Bottom TF 1", color.new(colMid1, 60), style = plot.style_stepline)
plot(showTB ? top2 : na, "Top TF 2",    color.new(colMid2, 60), style = plot.style_stepline)
plot(showTB ? bot2 : na, "Bottom TF 2", color.new(colMid2, 60), style = plot.style_stepline)
 
plotshape(alignedUp, "Aligned bullish", shape.triangleup,   location.belowbar, colBull, size = size.tiny)
plotshape(alignedDn, "Aligned bearish", shape.triangledown, location.abovebar, colBear, size = size.tiny)
 
// ───── Bias table ─────
posEnum = switch tblPos
    "Top Right"    => position.top_right
    "Top Left"     => position.top_left
    "Bottom Left"  => position.bottom_left
    => position.bottom_right
sizeEnum = switch tblSize
    "Small" => size.small
    "Large" => size.large
    => size.normal
 
biasTxt(int b) => b == 1 ? "Bullish" : b == -1 ? "Bearish" : "Neutral"
biasCol(int b) => b == 1 ? colBull : b == -1 ? colBear : color.gray
 
var table tbl = table.new(posEnum, 2, 3, border_width = 1)
if showTable and barstate.islast
    hdr = color.new(color.black, 30)
    table.cell(tbl, 0, 0, tf1 == "" ? "Chart" : tf1, text_color = color.white, bgcolor = hdr, text_size = sizeEnum)
    table.cell(tbl, 1, 0, biasTxt(bias1), text_color = color.white, bgcolor = biasCol(bias1), text_size = sizeEnum)
    table.cell(tbl, 0, 1, tf2 == "" ? "Chart" : tf2, text_color = color.white, bgcolor = hdr, text_size = sizeEnum)
    table.cell(tbl, 1, 1, biasTxt(bias2), text_color = color.white, bgcolor = biasCol(bias2), text_size = sizeEnum)
    table.cell(tbl, 0, 2, "Combined", text_color = color.white, bgcolor = hdr, text_size = sizeEnum)
    table.cell(tbl, 1, 2, combined == 0 ? "Mixed" : biasTxt(combined), text_color = color.white, bgcolor = biasCol(combined), text_size = sizeEnum)
 
// ───── Alerts ─────
alertcondition(alignedUp, "Aligned bullish", "SBM: both timeframes bullish on {{ticker}}")
alertcondition(alignedDn, "Aligned bearish", "SBM: both timeframes bearish on {{ticker}}")
 
if alignedUp or alignedDn
    alert("SBM both timeframes " + (alignedUp ? "bullish" : "bearish") + " | " + syminfo.ticker, alert.freq_once_per_bar_close)

← 返回列表