Market Strategies MTF Structure & Bias Pro v2.0

samb817 · study · 300 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("Market Strategies MTF Structure & Bias Pro v2.0", shorttitle="MS MTF v2", overlay=true)
 
//============================================================
// INPUTS
//============================================================
 
groupDash = "DASHBOARD"
showDash = input.bool(true, "Show Dashboard", group=groupDash)
dashPosition = input.string("Top Right", "Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=groupDash)
dashSize = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal"], group=groupDash)
showLevels = input.bool(true, "Show Break Levels", group=groupDash)
showDistance = input.bool(true, "Show Distance From Break", group=groupDash)
 
groupStructure = "MARKET STRUCTURE"
pivotLength = input.int(5, "Swing Length", minval=2, maxval=30, group=groupStructure)
confirmClose = input.bool(true, "Require Close Beyond Swing", group=groupStructure)
live4HStatus = input.bool(false, "Live Price vs 4H Break", group=groupStructure)
 
groupVWAP = "DAILY VWAP"
vwapSource = input.source(ohlc4, "VWAP Source", group=groupVWAP)
useClosedVWAP = input.bool(false, "Use Confirmed 5M VWAP", group=groupVWAP)
showVWAPLine = input.bool(false, "Plot Daily VWAP", group=groupVWAP)
 
groupAlerts = "ALERTS"
enableAlerts = input.bool(true, "Enable Alerts", group=groupAlerts)
 
//============================================================
// WHITE DASHBOARD COLORS
//============================================================
 
bullColor = #008F43
bearColor = #D20F39
neutralColor = #52616B
warningColor = #B8860B
headerColor = color.rgb(235, 238, 242)
backgroundColor = color.rgb(255, 255, 255)
textColor = color.rgb(17, 24, 39)
borderColor = color.rgb(203, 213, 225)
 
//============================================================
// STRUCTURE ENGINE
//============================================================
 
structureEngine() =>
    float ph = ta.pivothigh(high, pivotLength, pivotLength)
    float pl = ta.pivotlow(low, pivotLength, pivotLength)
 
    var float lastHigh = na
    var float lastLow = na
    var bool highBroken = false
    var bool lowBroken = false
    var int direction = 0
    var int lastType = 0
    var float lastLevel = na
 
    if not na(ph)
        lastHigh := ph
        highBroken := false
 
    if not na(pl)
        lastLow := pl
        lowBroken := false
 
    bool bullBreak = not na(lastHigh) and not highBroken and (confirmClose ? close > lastHigh : high > lastHigh)
    bool bearBreak = not na(lastLow) and not lowBroken and (confirmClose ? close < lastLow : low < lastLow)
 
    if barstate.isconfirmed and bullBreak and not bearBreak
        lastType := direction == -1 ? 2 : 1
        direction := 1
        lastLevel := lastHigh
        highBroken := true
 
    if barstate.isconfirmed and bearBreak and not bullBreak
        lastType := direction == 1 ? 2 : 1
        direction := -1
        lastLevel := lastLow
        lowBroken := true
 
    [direction, lastType, lastLevel]
 
//============================================================
// 4H AND 5M STRUCTURE
//============================================================
 
confirmed4H() =>
    [d, t, l] = structureEngine()
    [d[1], t[1], l[1], close[1]]
 
[raw4HDir, raw4HType, level4H, close4H] = request.security(syminfo.tickerid, "240", confirmed4H(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
 
[raw5MDir, raw5MType, level5M] = request.security(syminfo.tickerid, "5", structureEngine(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
int dir4H = nz(raw4HDir, 0)
int type4H = nz(raw4HType, 0)
int dir5M = nz(raw5MDir, 0)
int type5M = nz(raw5MType, 0)
 
//============================================================
// DAILY VWAP
//============================================================
 
bool newDay = timeframe.change("D")
 
var float sumPV = 0.0
var float sumVolume = 0.0
 
if newDay
    sumPV := 0.0
    sumVolume := 0.0
 
sumPV += vwapSource * nz(volume, 0)
sumVolume += nz(volume, 0)
 
float dailyVWAP = sumVolume > 0 ? sumPV / sumVolume : na
 
plot(showVWAPLine ? dailyVWAP : na, title="Daily VWAP", color=color.blue, linewidth=2)
 
float priceForVWAP = useClosedVWAP ? close[1] : close
float valueForVWAP = useClosedVWAP ? dailyVWAP[1] : dailyVWAP
 
int vwapDirection = na(valueForVWAP) ? 0 : priceForVWAP > valueForVWAP ? 1 : priceForVWAP < valueForVWAP ? -1 : 0
 
//============================================================
// PRICE VS LAST BREAK
//============================================================
 
float compare4H = live4HStatus ? close : close4H
float compare5M = close
 
int priceVs4H = na(level4H) or na(compare4H) ? 0 : compare4H > level4H ? 1 : compare4H < level4H ? -1 : 0
int priceVs5M = na(level5M) ? 0 : compare5M > level5M ? 1 : compare5M < level5M ? -1 : 0
 
float distance4H = na(level4H) ? na : compare4H - level4H
float distance5M = na(level5M) ? na : compare5M - level5M
 
//============================================================
// STATUS FUNCTIONS
//============================================================
 
structureText(int d) =>
    d == 1 ? "BULLISH" : d == -1 ? "BEARISH" : "NEUTRAL"
 
breakText(int d, int t) =>
    d == 0 ? "NONE" : t == 2 ? (d == 1 ? "BULL CHOCH" : "BEAR CHOCH") : (d == 1 ? "BULL BOS" : "BEAR BOS")
 
positionText(int p) =>
    p == 1 ? "ABOVE" : p == -1 ? "BELOW" : "AT / N/A"
 
breakStatus(int d, int p) =>
    d == 0 or p == 0 ? "NO DATA" : d == 1 and p == 1 ? "BULL HELD" : d == 1 and p == -1 ? "BULL LOST" : d == -1 and p == -1 ? "BEAR HELD" : "BEAR RECLAIMED"
 
directionColor(int d) =>
    d == 1 ? bullColor : d == -1 ? bearColor : neutralColor
 
statusColor(int d, int p) =>
    d == 0 or p == 0 ? neutralColor : d == 1 and p == 1 ? bullColor : d == -1 and p == -1 ? bearColor : warningColor
 
distanceText(float distance) =>
    na(distance) ? "N/A" : (distance > 0 ? "+" : "") + str.tostring(distance, format.mintick)
 
//============================================================
// DISPLAY VALUES
//============================================================
 
string structure4H = structureText(dir4H)
string structure5M = structureText(dir5M)
 
string break4H = breakText(dir4H, type4H)
string break5M = breakText(dir5M, type5M)
 
string position4H = positionText(priceVs4H)
string position5M = positionText(priceVs5M)
 
string status4H = breakStatus(dir4H, priceVs4H)
string status5M = breakStatus(dir5M, priceVs5M)
 
color color4H = directionColor(dir4H)
color color5M = directionColor(dir5M)
color colorVWAP = directionColor(vwapDirection)
 
color statusColor4H = statusColor(dir4H, priceVs4H)
color statusColor5M = statusColor(dir5M, priceVs5M)
 
string vwapText = positionText(vwapDirection)
 
//============================================================
// ALIGNMENT
//============================================================
 
bool correctChart = timeframe.isminutes and timeframe.multiplier == 5
 
bool bullAligned = correctChart and dir4H == 1 and dir5M == 1 and vwapDirection == 1 and priceVs4H == 1 and priceVs5M == 1
bool bearAligned = correctChart and dir4H == -1 and dir5M == -1 and vwapDirection == -1 and priceVs4H == -1 and priceVs5M == -1
 
bool bullCountertrend = correctChart and dir4H == -1 and dir5M == 1
bool bearCountertrend = correctChart and dir4H == 1 and dir5M == -1
 
bool bullBreakLost = correctChart and dir4H == 1 and priceVs4H == -1
bool bearBreakReclaimed = correctChart and dir4H == -1 and priceVs4H == 1
 
string alignment = "MIXED / WAIT"
color alignmentColor = neutralColor
 
if not correctChart
    alignment := "USE 5M CHART"
    alignmentColor := warningColor
else if bullAligned
    alignment := "BULL ALIGNED"
    alignmentColor := bullColor
else if bearAligned
    alignment := "BEAR ALIGNED"
    alignmentColor := bearColor
else if bullBreakLost
    alignment := "4H BULL BREAK LOST"
    alignmentColor := bearColor
else if bearBreakReclaimed
    alignment := "4H BEAR RECLAIMED"
    alignmentColor := warningColor
else if bullCountertrend
    alignment := "BULL COUNTERTREND"
    alignmentColor := warningColor
else if bearCountertrend
    alignment := "BEAR COUNTERTREND"
    alignmentColor := warningColor
 
string decision = bullAligned ? "LONG BIAS" : bearAligned ? "SHORT BIAS" : "WAIT / REVIEW"
 
//============================================================
// TABLE SETTINGS
//============================================================
 
tablePosition = switch dashPosition
    "Top Left" => position.top_left
    "Bottom Right" => position.bottom_right
    "Bottom Left" => position.bottom_left
    => position.top_right
 
textSize = switch dashSize
    "Tiny" => size.tiny
    "Normal" => size.normal
    => size.small
 
//============================================================
// WHITE DASHBOARD
//============================================================
 
var table dashboard = table.new(tablePosition, 3, 8, bgcolor=color.white, frame_color=borderColor, frame_width=1, border_width=1, border_color=borderColor)
 
if barstate.islast
    if showDash
        table.cell(dashboard, 0, 0, "MARKET STRATEGIES", bgcolor=headerColor, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 0, "4H", bgcolor=headerColor, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 2, 0, "5M", bgcolor=headerColor, text_color=textColor, text_size=textSize)
 
        table.cell(dashboard, 0, 1, "STRUCTURE", bgcolor=color.white, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 1, structure4H, bgcolor=color.white, text_color=color4H, text_size=textSize)
        table.cell(dashboard, 2, 1, structure5M, bgcolor=color.white, text_color=color5M, text_size=textSize)
 
        table.cell(dashboard, 0, 2, "LAST BREAK", bgcolor=color.white, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 2, break4H, bgcolor=color.white, text_color=color4H, text_size=textSize)
        table.cell(dashboard, 2, 2, break5M, bgcolor=color.white, text_color=color5M, text_size=textSize)
 
        table.cell(dashboard, 0, 3, "BREAK LEVEL", bgcolor=color.white, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 3, showLevels and not na(level4H) ? str.tostring(level4H, format.mintick) : "—", bgcolor=color.white, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 2, 3, showLevels and not na(level5M) ? str.tostring(level5M, format.mintick) : "—", bgcolor=color.white, text_color=textColor, text_size=textSize)
 
        table.cell(dashboard, 0, 4, "PRICE VS BREAK", bgcolor=color.white, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 4, position4H, bgcolor=color.white, text_color=directionColor(priceVs4H), text_size=textSize)
        table.cell(dashboard, 2, 4, position5M, bgcolor=color.white, text_color=directionColor(priceVs5M), text_size=textSize)
 
        table.cell(dashboard, 0, 5, "BREAK STATUS", bgcolor=color.white, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 5, status4H, bgcolor=color.white, text_color=statusColor4H, text_size=textSize)
        table.cell(dashboard, 2, 5, status5M, bgcolor=color.white, text_color=statusColor5M, text_size=textSize)
 
        table.cell(dashboard, 0, 6, "DAILY VWAP", bgcolor=color.white, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 6, showDistance ? distanceText(distance4H) : "SESSION", bgcolor=color.white, text_color=neutralColor, text_size=textSize)
        table.cell(dashboard, 2, 6, showDistance ? vwapText + " / " + distanceText(distance5M) : vwapText, bgcolor=color.white, text_color=colorVWAP, text_size=textSize)
 
        table.cell(dashboard, 0, 7, "ALIGNMENT", bgcolor=headerColor, text_color=textColor, text_size=textSize)
        table.cell(dashboard, 1, 7, alignment, bgcolor=headerColor, text_color=alignmentColor, text_size=textSize)
        table.cell(dashboard, 2, 7, decision, bgcolor=headerColor, text_color=alignmentColor, text_size=textSize)
    else
        table.clear(dashboard, 0, 0, 2, 7)
 
//============================================================
// ALERTS
//============================================================
 
alertcondition(enableAlerts and bullAligned and not bullAligned[1], title="MTF BULLISH ALIGNMENT", message="MARKET STRATEGIES | Bullish structure aligned, break levels held, price above VWAP | {{ticker}}")
 
alertcondition(enableAlerts and bearAligned and not bearAligned[1], title="MTF BEARISH ALIGNMENT", message="MARKET STRATEGIES | Bearish structure aligned, break levels held, price below VWAP | {{ticker}}")
 
alertcondition(enableAlerts and bullBreakLost and not bullBreakLost[1], title="4H BULLISH BREAK LOST", message="MARKET STRATEGIES | Price below last 4H bullish structural break | {{ticker}}")
 
alertcondition(enableAlerts and bearBreakReclaimed and not bearBreakReclaimed[1], title="4H BEARISH BREAK RECLAIMED", message="MARKET STRATEGIES | Price above last 4H bearish structural break | {{ticker}}")
 
alertcondition(enableAlerts and bullCountertrend and not bullCountertrend[1], title="MTF BULLISH COUNTERTREND", message="MARKET STRATEGIES | 5M bullish against bearish 4H structure | {{ticker}}")
 
alertcondition(enableAlerts and bearCountertrend and not bearCountertrend[1], title="MTF BEARISH COUNTERTREND", message="MARKET STRATEGIES | 5M bearish against bullish 4H structure | {{ticker}}")

← 返回列表