本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Market structure: BMS (break of market structure, trend continuation) and MSS (market structure shift, trend change).
// Works on any timeframe. A swing high / low is a candle with no higher high / lower low within `Swing strength` candles on
// each side (confirmed `Swing strength` candles later). When price closes (or wicks, if chosen) beyond the latest unbroken
// swing:
// - in the direction of the current trend -> BMS
// - against the current trend -> MSS (the trend flips)
// The line runs from the swing to the breaking candle. Solid = MSS, dashed = BMS. Green = bullish, red = bearish.
// The table shows the same logic on several timeframes at once (values from lower timeframes than the chart are approximate).
indicator("MSS / BMS Market Structure (MTF)", shorttitle="MSS/BMS", overlay=true, max_lines_count=500, max_labels_count=500)
// ───────────────────────── Inputs ─────────────────────────
grpS = "Structure"
len = input.int(3, "Swing strength (candles each side)", minval=1, group=grpS)
breakBy = input.string("Close", "Break confirmed by", options=["Close", "Wick"], group=grpS)
showBms = input.bool(true, "Show BMS", group=grpS)
showMss = input.bool(true, "Show MSS", group=grpS)
showLive = input.bool(true, "Show live (unbroken) swing high / low", group=grpS)
bullCol = input.color(#089981, "Bullish colour", group=grpS)
bearCol = input.color(#f23645, "Bearish colour", group=grpS)
grpT = "Multi-timeframe table"
showTbl = input.bool(true, "Show table", group=grpT)
tblPos = input.string("Top right", "Position", options=["Top right", "Bottom right", "Top left", "Bottom left"], group=grpT)
tf1 = input.timeframe("5", "Timeframe 1", group=grpT)
tf2 = input.timeframe("15", "Timeframe 2", group=grpT)
tf3 = input.timeframe("60", "Timeframe 3", group=grpT)
tf4 = input.timeframe("240", "Timeframe 4", group=grpT)
tf5 = input.timeframe("D", "Timeframe 5", group=grpT)
tf6 = input.timeframe("W", "Timeframe 6", group=grpT)
useWick = breakBy == "Wick"
// ───────────────────────── Structure engine ─────────────────────────
// Returns [trend, evt, lvl, lvlT, lastEvt, lastLvl, liveHigh, liveLow]
// trend: 1 bullish, -1 bearish, 0 unknown. evt (this candle) / lastEvt: 1 bull BMS, 2 bull MSS, -1 bear BMS, -2 bear MSS, 0 none.
f_struct(int n, bool wick) =>
var int trend = 0
var float sh = na
var int shT = na
var float sl = na
var int slT = na
var int lastEvt = 0
var float lastLvl = na
int evt = 0
float lvl = na
int lvlT = na
float ph = ta.pivothigh(high, n, n)
float pl = ta.pivotlow(low, n, n)
if not na(ph)
sh := ph
shT := time[n]
if not na(pl)
sl := pl
slT := time[n]
float up = wick ? high : close
float dn = wick ? low : close
if not na(sh) and up > sh
evt := trend == -1 ? 2 : 1
lvl := sh
lvlT := shT
trend := 1
sh := na
if not na(sl) and dn < sl
evt := trend == 1 ? -2 : -1
lvl := sl
lvlT := slT
trend := -1
sl := na
if evt != 0
lastEvt := evt
lastLvl := lvl
[trend, evt, lvl, lvlT, lastEvt, lastLvl, sh, sl]
// ───────────────────────── Chart timeframe ─────────────────────────
[trend, evt, lvl, lvlT, lastEvt, lastLvl, liveH, liveL] = f_struct(len, useWick)
isMss = math.abs(evt) == 2
if evt != 0 and (isMss ? showMss : showBms)
color c = evt > 0 ? bullCol : bearCol
line.new(lvlT, lvl, time, lvl, xloc.bar_time, color=c, width=2, style=isMss ? line.style_solid : line.style_dashed)
label.new(int((lvlT + time) / 2), lvl, isMss ? "MSS" : "BMS", xloc.bar_time, color=color.new(color.black, 100), textcolor=c, style=evt > 0 ? label.style_label_down : label.style_label_up, size=size.small)
plot(showLive ? liveH : na, "Live swing high", color.new(bearCol, 40), 1, plot.style_linebr)
plot(showLive ? liveL : na, "Live swing low", color.new(bullCol, 40), 1, plot.style_linebr)
alertcondition(evt == 2, "Bullish MSS", "Bullish MSS on {{ticker}} {{interval}}")
alertcondition(evt == -2, "Bearish MSS", "Bearish MSS on {{ticker}} {{interval}}")
alertcondition(evt == 1, "Bullish BMS", "Bullish BMS on {{ticker}} {{interval}}")
alertcondition(evt == -1, "Bearish BMS", "Bearish BMS on {{ticker}} {{interval}}")
alertcondition(isMss, "Any MSS", "MSS on {{ticker}} {{interval}}")
if evt != 0
alert((evt > 0 ? "Bullish " : "Bearish ") + (isMss ? "MSS" : "BMS") + " on " + syminfo.ticker + " " + timeframe.period + " at " + str.tostring(lvl, format.mintick), alert.freq_once_per_bar_close)
// ───────────────────────── Multi-timeframe table ─────────────────────────
[t1, _e1, _l1, _lt1, le1, ll1, h1, lo1] = request.security(syminfo.tickerid, tf1, f_struct(len, useWick))
[t2, _e2, _l2, _lt2, le2, ll2, h2, lo2] = request.security(syminfo.tickerid, tf2, f_struct(len, useWick))
[t3, _e3, _l3, _lt3, le3, ll3, h3, lo3] = request.security(syminfo.tickerid, tf3, f_struct(len, useWick))
[t4, _e4, _l4, _lt4, le4, ll4, h4, lo4] = request.security(syminfo.tickerid, tf4, f_struct(len, useWick))
[t5, _e5, _l5, _lt5, le5, ll5, h5, lo5] = request.security(syminfo.tickerid, tf5, f_struct(len, useWick))
[t6, _e6, _l6, _lt6, le6, ll6, h6, lo6] = request.security(syminfo.tickerid, tf6, f_struct(len, useWick))
f_evtText(int e) =>
switch e
2 => "Bull MSS"
1 => "Bull BMS"
-1 => "Bear BMS"
-2 => "Bear MSS"
=> "-"
f_px(float v) =>
na(v) ? "-" : str.tostring(v, format.mintick)
f_tfName(string tf) =>
timeframe.in_seconds(tf) < 3600 ? tf + "m" : timeframe.in_seconds(tf) < 86400 ? str.tostring(timeframe.in_seconds(tf) / 3600) + "H" : tf
var table tbl = table.new(tblPos == "Top right" ? position.top_right : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Top left" ? position.top_left : position.bottom_left, 5, 7, bgcolor=#131722, border_width=1, border_color=#363a45)
f_row(int r, string tf, int t, int le, float ll, float h, float lo) =>
color tc = t == 1 ? bullCol : t == -1 ? bearCol : color.gray
table.cell(tbl, 0, r, f_tfName(tf), text_color=color.white, text_size=size.small)
table.cell(tbl, 1, r, t == 1 ? "Bullish" : t == -1 ? "Bearish" : "-", text_color=tc, text_size=size.small)
table.cell(tbl, 2, r, f_evtText(le) + (na(ll) ? "" : " @ " + f_px(ll)), text_color=le > 0 ? bullCol : le < 0 ? bearCol : color.gray, text_size=size.small)
table.cell(tbl, 3, r, f_px(h), text_color=color.white, text_size=size.small)
table.cell(tbl, 4, r, f_px(lo), text_color=color.white, text_size=size.small)
if showTbl and barstate.islast
table.cell(tbl, 0, 0, "TF", text_color=color.silver, text_size=size.small)
table.cell(tbl, 1, 0, "Trend", text_color=color.silver, text_size=size.small)
table.cell(tbl, 2, 0, "Last break", text_color=color.silver, text_size=size.small)
table.cell(tbl, 3, 0, "Bull trigger (swing high)", text_color=color.silver, text_size=size.small)
table.cell(tbl, 4, 0, "Bear trigger (swing low)", text_color=color.silver, text_size=size.small)
f_row(1, tf1, t1, le1, ll1, h1, lo1)
f_row(2, tf2, t2, le2, ll2, h2, lo2)
f_row(3, tf3, t3, le3, ll3, h3, lo3)
f_row(4, tf4, t4, le4, ll4, h4, lo4)
f_row(5, tf5, t5, le5, ll5, h5, lo5)
f_row(6, tf6, t6, le6, ll6, h6, lo6)