NS Market Structure

NSakib07 · study · 225 行 · 点赞 19 · TradingView 原页

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

Pine Script

//@version=6
// NS Market Structure
// BOS  = trend continues: price breaks the last swing high (uptrend) or swing low (downtrend).
// MSS  = trend changes: price breaks the PROTECTED level - the swing that created the last BOS.
//        Breaking a smaller internal pullback (inside the leg) is NOT an MSS.
indicator("NS Market Structure", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// =====================================================================
// INPUTS
// =====================================================================
len     = input.int(5, "Swing length (candles on each side)", minval = 1, tooltip = "How many candles on each side are needed to confirm a swing high / low. Bigger = only bigger swings count.")
brk     = input.string("Close", "Break confirmed by", options = ["Close", "Wick"], tooltip = "Close = a candle must CLOSE beyond the level. Wick = touching beyond the level is enough.")
showBos = input.bool(true, "Show BOS")
showMss = input.bool(true, "Show MSS")
showLvl = input.bool(true, "Show live levels (next BOS / MSS level)")
showTbl = input.bool(true, "Show structure box")
cUp     = input.color(color.green, "Bullish color")
cDn     = input.color(color.red,   "Bearish color")
 
// =====================================================================
// SWINGS
// =====================================================================
ph     = ta.pivothigh(high, len, len)
pl     = ta.pivotlow(low, len, len)
lowN   = ta.lowest(low, len)
lowNb  = bar_index + ta.lowestbars(low, len)
highN  = ta.highest(high, len)
highNb = bar_index + ta.highestbars(high, len)
float upPx = brk == "Close" ? close : high
float dnPx = brk == "Close" ? close : low
 
// =====================================================================
// STATE
// trend: 1 = bullish, -1 = bearish, 0 = not known yet
// sH / sL   : level that must break for the next BOS
// prot      : protected level (low in uptrend, high in downtrend) - break = MSS
// loS / hiS : lowest low since sH / highest high since sL (becomes the new protected level on BOS)
// hiP / loP : highest high / lowest low since prot was set (becomes the new protected level on MSS)
// =====================================================================
var int   trend = 0
var float sH    = na
var int   sHb   = na
var float sL    = na
var int   sLb   = na
var float loS   = na
var int   loSb  = na
var float hiS   = na
var int   hiSb  = na
var float prot  = na
var int   protB = na
var float hiP   = na
var int   hiPb  = na
var float loP   = na
var int   loPb  = na
var string lastEvt = "-"
 
// New swing high: in an uptrend only a HIGHER swing high replaces the BOS level (internal lower highs are ignored)
if not na(ph) and trend != -1 and (trend == 0 or na(sH) or ph > sH)
    sH   := ph
    sHb  := bar_index - len
    loS  := lowN
    loSb := lowNb
 
// New swing low: in a downtrend only a LOWER swing low replaces the BOS level
if not na(pl) and trend != 1 and (trend == 0 or na(sL) or pl < sL)
    sL   := pl
    sLb  := bar_index - len
    hiS  := highN
    hiSb := highNb
 
// Track extremes
if not na(sH) and (na(loS) or low < loS)
    loS  := low
    loSb := bar_index
if not na(sL) and (na(hiS) or high > hiS)
    hiS  := high
    hiSb := bar_index
if not na(prot)
    if na(hiP) or high > hiP
        hiP  := high
        hiPb := bar_index
    if na(loP) or low < loP
        loP  := low
        loPb := bar_index
 
// =====================================================================
// EVENTS (MSS is checked before BOS)
// =====================================================================
bool bosUp = false
bool bosDn = false
bool mssUp = false
bool mssDn = false
 
if trend == 1
    if not na(prot) and dnPx < prot
        mssDn := true
    else if not na(sH) and upPx > sH
        bosUp := true
else if trend == -1
    if not na(prot) and upPx > prot
        mssUp := true
    else if not na(sL) and dnPx < sL
        bosDn := true
else
    if not na(sH) and upPx > sH
        bosUp := true
    else if not na(sL) and dnPx < sL
        bosDn := true
 
drawBreak(int x1, float y, string txt, color c, bool solid) =>
    line.new(x1, y, bar_index, y, color = c, width = solid ? 2 : 1, style = solid ? line.style_solid : line.style_dashed)
    label.new(int((x1 + bar_index) / 2), y, txt, style = label.style_none, textcolor = c, size = size.small)
 
// --- BOS up: the low of the pullback that broke the high becomes the protected low
if bosUp
    if showBos
        drawBreak(sHb, sH, "BOS", cUp, false)
    prot    := loS
    protB   := loSb
    hiP     := math.max(high, sH)
    hiPb    := high >= sH ? bar_index : sHb
    loP     := loS
    loPb    := loSb
    trend   := 1
    sH      := na
    loS     := na
    sL      := na
    hiS     := na
    lastEvt := "BOS UP"
 
// --- BOS down: the high of the pullback that broke the low becomes the protected high
if bosDn
    if showBos
        drawBreak(sLb, sL, "BOS", cDn, false)
    prot    := hiS
    protB   := hiSb
    loP     := math.min(low, sL)
    loPb    := low <= sL ? bar_index : sLb
    hiP     := hiS
    hiPb    := hiSb
    trend   := -1
    sL      := na
    hiS     := na
    sH      := na
    loS     := na
    lastEvt := "BOS DOWN"
 
// --- MSS down: protected low broken -> downtrend, the top of the last up leg is the new protected high
if mssDn
    if showMss
        drawBreak(protB, prot, "MSS", cDn, true)
    prot    := hiP
    protB   := hiPb
    loP     := low
    loPb    := bar_index
    trend   := -1
    sL      := na
    hiS     := na
    sH      := na
    loS     := na
    lastEvt := "MSS DOWN"
 
// --- MSS up: protected high broken -> uptrend, the bottom of the last down leg is the new protected low
if mssUp
    if showMss
        drawBreak(protB, prot, "MSS", cUp, true)
    prot    := loP
    protB   := loPb
    hiP     := high
    hiPb    := bar_index
    trend   := 1
    sH      := na
    loS     := na
    sL      := na
    hiS     := na
    lastEvt := "MSS UP"
 
// =====================================================================
// LIVE LEVELS (redrawn on the last bar)
// =====================================================================
var line  bosLine = na
var label bosLbl  = na
var line  mssLine = na
var label mssLbl  = na
 
if barstate.islast and showLvl
    line.delete(bosLine)
    label.delete(bosLbl)
    line.delete(mssLine)
    label.delete(mssLbl)
    float tgt  = trend == 1 ? sH : trend == -1 ? sL : na
    int   tgtB = trend == 1 ? sHb : trend == -1 ? sLb : na
    if not na(tgt)
        bosLine := line.new(tgtB, tgt, bar_index + 10, tgt, color = color.gray, style = line.style_dotted, width = 1)
        bosLbl  := label.new(bar_index + 10, tgt, "Next BOS level", style = label.style_label_left, color = color.new(color.gray, 20), textcolor = color.white, size = size.tiny)
    if not na(prot)
        color mc = trend == 1 ? cDn : cUp
        mssLine := line.new(protB, prot, bar_index + 10, prot, color = mc, style = line.style_dotted, width = 2)
        mssLbl  := label.new(bar_index + 10, prot, (trend == 1 ? "Protected LOW - break = MSS down" : "Protected HIGH - break = MSS up"), style = label.style_label_left, color = color.new(mc, 20), textcolor = color.white, size = size.tiny)
 
// =====================================================================
// STRUCTURE BOX
// =====================================================================
var table tbl = table.new(position.middle_right, 2, 5, border_width = 1, frame_color = color.gray, frame_width = 1)
if showTbl and barstate.islast
    bg = color.new(color.gray, 20)
    float tgt = trend == 1 ? sH : trend == -1 ? sL : na
    tbl.cell(0, 0, "Market Structure", text_color = color.white, bgcolor = color.new(color.blue, 10))
    tbl.merge_cells(0, 0, 1, 0)
    tbl.cell(0, 1, "Trend", text_color = color.white, bgcolor = bg)
    tbl.cell(1, 1, trend == 1 ? "BULLISH" : trend == -1 ? "BEARISH" : "WAITING", text_color = color.white, bgcolor = trend == 1 ? color.new(cUp, 30) : trend == -1 ? color.new(cDn, 30) : bg, text_formatting = text.format_bold)
    tbl.cell(0, 2, "Protected level", text_color = color.white, bgcolor = bg)
    tbl.cell(1, 2, na(prot) ? "-" : str.tostring(prot, format.mintick), text_color = color.white, bgcolor = bg)
    tbl.cell(0, 3, "Next BOS level", text_color = color.white, bgcolor = bg)
    tbl.cell(1, 3, na(tgt) ? "forming..." : str.tostring(tgt, format.mintick), text_color = color.white, bgcolor = bg)
    tbl.cell(0, 4, "Last event", text_color = color.white, bgcolor = bg)
    tbl.cell(1, 4, lastEvt, text_color = color.white, bgcolor = bg, text_formatting = text.format_bold)
 
// =====================================================================
// ALERTS
// =====================================================================
alertcondition(bosUp, "BOS UP",   "Bullish break of structure")
alertcondition(bosDn, "BOS DOWN", "Bearish break of structure")
alertcondition(mssUp, "MSS UP",   "Market structure shift to bullish")
alertcondition(mssDn, "MSS DOWN", "Market structure shift to bearish")

← 返回列表