本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("SMC Structure Suite", shorttitle="SMC Suite", overlay=true,
max_lines_count=500, max_labels_count=500, max_boxes_count=500, max_bars_back=500)
// ════════════════════════════ Types ════════════════════════════
type SessBox
box bx = na
float hi = na
float lo = na
bool on = false
type Liq
float price
int bar
bool isHigh
bool swept
label bub
line ln
type Trade
int dir
float entry
float sl
float tp1
float tp2
float tp3
int stage
bool open
bool slHit
array<line> lns
array<label> lbls
// ════════════════════════════ Inputs ════════════════════════════
grpStr = "1. Market Structure"
swingLen = input.int(5, "Swing Length (bars each side)", minval=2, group=grpStr)
showStruct = input.bool(true, "Show BOS / CHoCH", group=grpStr)
showSwings = input.bool(true, "Show HH / HL / LH / LL markers", group=grpStr)
grpEma = "2. EMAs"
showEma = input.bool(true, "Show EMAs", group=grpEma)
fastLen = input.int(12, "Fast EMA", minval=1, group=grpEma)
slowLen = input.int(26, "Slow EMA", minval=1, group=grpEma)
grpSess = "3. Sessions"
showSess = input.bool(true, "Show Sessions", group=grpSess)
sessTz = input.string("UTC", "Session Timezone", group=grpSess,
tooltip="Examples: UTC, GMT+3, America/New_York, Asia/Riyadh. Session times below are read in this timezone.")
asiaSess = input.session("0000-0900", "Asia", group=grpSess)
lonSess = input.session("0800-1700", "London", group=grpSess)
nySess = input.session("1300-2200", "New York", group=grpSess)
grpTrade = "4. Entry / SL / TP"
showTrade = input.bool(true, "Show trade plan on chart", group=grpTrade)
reqEma = input.bool(true, "Require EMA trend agreement", group=grpTrade,
tooltip="Buy only when Fast EMA > Slow EMA, sell only when Fast EMA < Slow EMA.")
atrLen = input.int(14, "ATR Length", minval=1, group=grpTrade)
slBuffer = input.float(0.2, "SL Buffer beyond swing (x ATR)", minval=0.0, step=0.05, group=grpTrade)
minRiskAtr = input.float(0.5, "Min Risk (x ATR)", minval=0.1, step=0.1, group=grpTrade)
maxRiskAtr = input.float(4.0, "Max Risk (x ATR)", minval=0.5, step=0.1, group=grpTrade)
tp1R = input.float(1.0, "TP1 (R multiple)", minval=0.1, step=0.1, group=grpTrade)
tp2R = input.float(2.0, "TP2 (R multiple)", minval=0.1, step=0.1, group=grpTrade)
tp3R = input.float(3.0, "TP3 (R multiple)", minval=0.1, step=0.1, group=grpTrade)
tradeBars = input.int(30, "Trade Lines Length (bars)", minval=5, group=grpTrade)
proxAtr = input.float(1.5, "Replace previous setup if new entry is within (x ATR)", minval=0.0, step=0.1, group=grpTrade,
tooltip="When a new CHoCH entry appears close to an existing setup, the older setup is removed from the chart.")
maxSetups = input.int(5, "Max Setups Kept On Chart", minval=1, maxval=30, group=grpTrade)
grpLiq = "5. Liquidity Bubbles"
showLiq = input.bool(true, "Show Liquidity Bubbles", group=grpLiq)
liqMax = input.int(12, "Max Bubbles Kept", minval=2, maxval=60, group=grpLiq)
volMaLen = input.int(20, "Volume MA Length (bubble size)", minval=2, group=grpLiq)
grpTbl = "6. Floating Panel"
showTable = input.bool(true, "Show Panel", group=grpTbl)
posStr = input.string("Top Right", "Panel Position",
options=["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right"], group=grpTbl)
grpMtf = "7. Multi-Timeframe Trend"
showMtf = input.bool(true, "Show Trend Status for other timeframes", group=grpMtf)
mtf1 = input.timeframe("5", "Timeframe 1", group=grpMtf)
mtf2 = input.timeframe("15", "Timeframe 2", group=grpMtf)
mtf3 = input.timeframe("60", "Timeframe 3", group=grpMtf)
mtf4 = input.timeframe("240", "Timeframe 4", group=grpMtf)
grpCol = "Colors"
bullCol = input.color(color.new(#00e676, 0), "Bullish", group=grpCol)
bearCol = input.color(color.new(color.red, 0), "Bearish", group=grpCol)
asiaCol = input.color(color.new(color.blue, 0), "Asia", group=grpCol)
lonCol = input.color(color.new(color.orange, 0), "London", group=grpCol)
nyCol = input.color(color.new(color.purple, 0), "New York", group=grpCol)
liqHiCol = input.color(color.new(color.yellow, 0), "Liquidity above (swing highs)", group=grpCol)
liqLoCol = input.color(color.new(color.yellow, 0), "Liquidity below (swing lows)", group=grpCol)
entryCol = input.color(color.new(#2962ff, 0), "Entry Label", group=grpCol)
slCol = input.color(color.new(#e53935, 0), "SL Label", group=grpCol)
tpCol = input.color(color.new(#00a152, 0), "TP Labels", group=grpCol)
lblTxtCol = input.color(color.new(color.white, 0), "Label Text", group=grpCol)
// ════════════════════════════ Helpers ════════════════════════════
f_level(Trade t, float price, string name, color bg, int w) =>
t.lns.push(line.new(bar_index, price, bar_index + tradeBars, price, color=bg, width=w))
t.lbls.push(label.new(bar_index + tradeBars, price, name + " " + str.tostring(price, format.mintick),
style=label.style_label_left, color=bg, textcolor=lblTxtCol, size=size.normal,
text_formatting=text.format_bold))
true
f_clear(Trade t) =>
for ln in t.lns
line.delete(ln)
for lb in t.lbls
label.delete(lb)
t.lns.clear()
t.lbls.clear()
true
f_bubble(float price, int bar, bool isHigh, float ratio, color c, string tip) =>
label lb = na
if ratio >= 2.0
lb := label.new(bar, price, "", style=label.style_circle, color=color.new(c, 35), size=size.large, tooltip=tip)
else if ratio >= 1.3
lb := label.new(bar, price, "", style=label.style_circle, color=color.new(c, 35), size=size.normal, tooltip=tip)
else
lb := label.new(bar, price, "", style=label.style_circle, color=color.new(c, 35), size=size.small, tooltip=tip)
line ln = line.new(bar, price, bar_index, price, color=color.new(c, 40), style=line.style_dotted, width=1)
Liq.new(price, bar, isHigh, false, lb, ln)
f_session(SessBox s, bool inS, string nm, color col, bool show) =>
if show and timeframe.isintraday
if inS
if not s.on
if barstate.isconfirmed
s.on := true
s.hi := high
s.lo := low
s.bx := box.new(bar_index, high, bar_index, low, border_color=col,
bgcolor=color.new(col, 88), text=nm, text_color=col, text_size=size.small,
text_halign=text.align_left, text_valign=text.align_top)
else
s.hi := math.max(s.hi, high)
s.lo := math.min(s.lo, low)
s.bx.set_top(s.hi)
s.bx.set_bottom(s.lo)
s.bx.set_right(bar_index)
else
s.on := false
true
// ════════════════════════════ EMAs ════════════════════════════
float fastEma = ta.ema(close, fastLen)
float slowEma = ta.ema(close, slowLen)
bool emaBull = fastEma > slowEma
color emaCol = emaBull ? bullCol : bearCol
pFast = plot(showEma ? fastEma : na, "Fast EMA", color=emaCol, linewidth=2)
pSlow = plot(showEma ? slowEma : na, "Slow EMA", color=emaCol, linewidth=1)
fill(pFast, pSlow, color=color.new(emaCol, 88), title="EMA Zone")
// ════════════════════════════ Swings: HH / HL / LH / LL ════════════════════════════
float atr = ta.atr(atrLen)
float ph = ta.pivothigh(high, swingLen, swingLen)
float pl = ta.pivotlow(low, swingLen, swingLen)
var float lastPH = na
var float lastPL = na
bool isHH = false
bool isLH = false
bool isHL = false
bool isLL = false
if not na(ph)
if not na(lastPH)
isHH := ph > lastPH
isLH := ph < lastPH
lastPH := ph
if not na(pl)
if not na(lastPL)
isHL := pl > lastPL
isLL := pl < lastPL
lastPL := pl
// HH & LH -> red triangle down | LL & HL -> green triangle up. Same size, plotted on the swing candle itself.
plotshape(showSwings and isHH, title="HH", style=shape.triangledown, location=location.abovebar,
color=bearCol, size=size.tiny, text="HH", textcolor=bearCol, offset=-swingLen)
plotshape(showSwings and isLH, title="LH", style=shape.triangledown, location=location.abovebar,
color=bearCol, size=size.tiny, text="LH", textcolor=bearCol, offset=-swingLen)
plotshape(showSwings and isLL, title="LL", style=shape.triangleup, location=location.belowbar,
color=bullCol, size=size.tiny, text="LL", textcolor=bullCol, offset=-swingLen)
plotshape(showSwings and isHL, title="HL", style=shape.triangleup, location=location.belowbar,
color=bullCol, size=size.tiny, text="HL", textcolor=bullCol, offset=-swingLen)
// ════════════════════════════ BOS / CHoCH ════════════════════════════
var int trend = 0 // 1 = bullish, -1 = bearish, 0 = not yet defined
var float sHigh = na
var int sHighBar = na
var bool sHighOn = false
var float sLow = na
var int sLowBar = na
var bool sLowOn = false
if not na(ph)
sHigh := ph
sHighBar := bar_index - swingLen
sHighOn := true
if not na(pl)
sLow := pl
sLowBar := bar_index - swingLen
sLowOn := true
bool bullBos = false
bool bullChoch = false
bool bearBos = false
bool bearChoch = false
if barstate.isconfirmed and sHighOn and close > sHigh
if trend == -1
bullChoch := true
else
bullBos := true
if showStruct
int mid = int(math.round((sHighBar + bar_index) / 2.0))
if bullChoch
line.new(sHighBar, sHigh, bar_index, sHigh, color=bullCol, style=line.style_solid, width=1)
else
line.new(sHighBar, sHigh, bar_index, sHigh, color=bullCol, style=line.style_dashed, width=1)
label.new(mid, sHigh, bullChoch ? "CHoCH" : "BOS", style=label.style_label_down,
color=color.new(color.black, 100), textcolor=bullCol, size=size.small)
trend := 1
sHighOn := false
if barstate.isconfirmed and sLowOn and close < sLow
if trend == 1
bearChoch := true
else
bearBos := true
if showStruct
int mid = int(math.round((sLowBar + bar_index) / 2.0))
if bearChoch
line.new(sLowBar, sLow, bar_index, sLow, color=bearCol, style=line.style_solid, width=1)
else
line.new(sLowBar, sLow, bar_index, sLow, color=bearCol, style=line.style_dashed, width=1)
label.new(mid, sLow, bearChoch ? "CHoCH" : "BOS", style=label.style_label_up,
color=color.new(color.black, 100), textcolor=bearCol, size=size.small)
trend := -1
sLowOn := false
// ════════════════════════════ Sessions ════════════════════════════
var SessBox asia = SessBox.new()
var SessBox lon = SessBox.new()
var SessBox ny = SessBox.new()
bool inAsia = not na(time(timeframe.period, asiaSess + ":1234567", sessTz))
bool inLon = not na(time(timeframe.period, lonSess + ":1234567", sessTz))
bool inNy = not na(time(timeframe.period, nySess + ":1234567", sessTz))
f_session(asia, inAsia, "Asia", asiaCol, showSess)
f_session(lon, inLon, "London", lonCol, showSess)
f_session(ny, inNy, "New York", nyCol, showSess)
// ════════════════════════════ Entry / SL / TP (entries on CHoCH only) ════════════════════════════
bool sigLong = bullChoch and (not reqEma or emaBull) and not na(atr)
bool sigShort = bearChoch and (not reqEma or not emaBull) and not na(atr)
var array<Trade> trades = array.new<Trade>()
var Trade lastT = na
// Opens a setup. Older setups whose entry is close to the new one are removed first.
f_open(array<Trade> arr, int dir, float e, float sl, float t1, float t2, float t3, float atrV) =>
if arr.size() > 0
for i = arr.size() - 1 to 0
Trade p = arr.get(i)
if math.abs(p.entry - e) <= proxAtr * atrV
f_clear(p)
arr.remove(i)
Trade n = Trade.new(dir, e, sl, t1, t2, t3, 0, true, false, array.new<line>(), array.new<label>())
if showTrade
f_level(n, e, "ENTRY " + (dir == 1 ? "BUY" : "SELL"), entryCol, 2)
f_level(n, sl, "SL", slCol, 1)
f_level(n, t1, "TP1 " + str.tostring(tp1R, "0.##") + "R", tpCol, 1)
f_level(n, t2, "TP2 " + str.tostring(tp2R, "0.##") + "R", tpCol, 1)
f_level(n, t3, "TP3 " + str.tostring(tp3R, "0.##") + "R", tpCol, 1)
arr.push(n)
while arr.size() > maxSetups
Trade old = arr.shift()
f_clear(old)
n
// Track every setup on the chart. SL hit = the setup is removed. (SL is checked before TP on the same candle.)
if barstate.isconfirmed and trades.size() > 0
for i = trades.size() - 1 to 0
Trade t = trades.get(i)
if t.open
bool slTouched = t.dir == 1 ? low <= t.sl : high >= t.sl
if slTouched
t.open := false
t.slHit := true
f_clear(t)
trades.remove(i)
else
bool tp1Hit = t.dir == 1 ? high >= t.tp1 : low <= t.tp1
bool tp2Hit = t.dir == 1 ? high >= t.tp2 : low <= t.tp2
bool tp3Hit = t.dir == 1 ? high >= t.tp3 : low <= t.tp3
if tp3Hit
t.stage := 3
t.open := false
else if tp2Hit
t.stage := math.max(t.stage, 2)
else if tp1Hit
t.stage := math.max(t.stage, 1)
if sigLong
float slRaw = (na(lastPL) ? close - 1.5 * atr : lastPL) - slBuffer * atr
float risk = math.min(math.max(close - slRaw, minRiskAtr * atr), maxRiskAtr * atr)
lastT := f_open(trades, 1, close, close - risk, close + risk * tp1R, close + risk * tp2R, close + risk * tp3R, atr)
if sigShort
float slRaw = (na(lastPH) ? close + 1.5 * atr : lastPH) + slBuffer * atr
float risk = math.min(math.max(slRaw - close, minRiskAtr * atr), maxRiskAtr * atr)
lastT := f_open(trades, -1, close, close + risk, close - risk * tp1R, close - risk * tp2R, close - risk * tp3R, atr)
plotshape(sigLong, title="Buy Entry (CHoCH)", style=shape.labelup, location=location.belowbar,
color=bullCol, textcolor=color.black, text="BUY", size=size.small)
plotshape(sigShort, title="Sell Entry (CHoCH)", style=shape.labeldown, location=location.abovebar,
color=bearCol, textcolor=color.white, text="SELL", size=size.small)
// ════════════════════════════ Liquidity bubbles ════════════════════════════
var array<Liq> liqs = array.new<Liq>()
float volMa = ta.sma(nz(volume), volMaLen)
float pvRatio = nz(volMa[swingLen]) > 0 ? nz(volume[swingLen]) / volMa[swingLen] : 1.0
if showLiq and barstate.isconfirmed
if not na(ph)
liqs.push(f_bubble(ph, bar_index - swingLen, true, pvRatio, liqHiCol,
"Liquidity above | volume " + str.tostring(pvRatio, "0.0") + "x average"))
if not na(pl)
liqs.push(f_bubble(pl, bar_index - swingLen, false, pvRatio, liqLoCol,
"Liquidity below | volume " + str.tostring(pvRatio, "0.0") + "x average"))
while liqs.size() > liqMax
Liq old = liqs.shift()
label.delete(old.bub)
line.delete(old.ln)
// Extend unswept levels; grey them out once price trades through
if liqs.size() > 0
for i = 0 to liqs.size() - 1
Liq lq = liqs.get(i)
if not lq.swept
line.set_x2(lq.ln, bar_index)
if (lq.isHigh and high > lq.price) or (not lq.isHigh and low < lq.price)
lq.swept := true
label.set_color(lq.bub, color.new(color.gray, 70))
line.set_color(lq.ln, color.new(color.gray, 70))
// ════════════════════════════ Floating panel ════════════════════════════
// Structure trend (BOS/CHoCH logic) + EMA direction, evaluated on another timeframe
f_mtf(int len, int fastP, int slowP) =>
float mPh = ta.pivothigh(high, len, len)
float mPl = ta.pivotlow(low, len, len)
var float mH = na
var float mL = na
var bool mHOn = false
var bool mLOn = false
var int mTr = 0
if not na(mPh)
mH := mPh
mHOn := true
if not na(mPl)
mL := mPl
mLOn := true
if mHOn and close > mH
mTr := 1
mHOn := false
if mLOn and close < mL
mTr := -1
mLOn := false
[mTr, ta.ema(close, fastP) > ta.ema(close, slowP)]
f_tfLabel(string tf) =>
int sec = timeframe.in_seconds(tf)
sec >= 86400 ? str.tostring(int(sec / 86400)) + "D" : sec >= 3600 ? str.tostring(int(sec / 3600)) + "H" : str.tostring(int(sec / 60)) + "M"
f_trTxt(int tr, bool emaUp) =>
(tr == 1 ? "BULLISH" : tr == -1 ? "BEARISH" : "NEUTRAL") + " | EMA " + (emaUp ? "▲" : "▼")
[tr1, em1] = request.security(syminfo.tickerid, mtf1, f_mtf(swingLen, fastLen, slowLen))
[tr2, em2] = request.security(syminfo.tickerid, mtf2, f_mtf(swingLen, fastLen, slowLen))
[tr3, em3] = request.security(syminfo.tickerid, mtf3, f_mtf(swingLen, fastLen, slowLen))
[tr4, em4] = request.security(syminfo.tickerid, mtf4, f_mtf(swingLen, fastLen, slowLen))
tblPos = switch posStr
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
"Middle Right" => position.middle_right
=> position.top_right
var table tbl = table.new(tblPos, 2, 8 + (showMtf ? 4 : 0), bgcolor=color.new(#131722, 10), frame_color=color.gray, frame_width=1,
border_color=color.new(color.gray, 60), border_width=1)
f_px(float v) =>
na(v) ? "-" : str.tostring(v, format.mintick)
if barstate.islast and showTable
bool hasT = not na(lastT)
bool gone = false
int pDir = 0
int pStg = 0
float pE = na
float pSL = na
float pT1 = na
float pT2 = na
float pT3 = na
if hasT
gone := lastT.slHit
pDir := lastT.dir
pStg := lastT.stage
if not gone
pE := lastT.entry
pSL := lastT.sl
pT1 := lastT.tp1
pT2 := lastT.tp2
pT3 := lastT.tp3
string trendTxt = (trend == 1 ? "BULLISH" : trend == -1 ? "BEARISH" : "NEUTRAL") + " | EMA " + (emaBull ? "▲" : "▼")
color trendCol = trend == 1 ? bullCol : trend == -1 ? bearCol : color.gray
string stTxt = not hasT ? "-" : gone ? "SL hit - setup removed" : pStg == 3 ? "TP3 hit" : pStg > 0 ? "TP" + str.tostring(pStg) + " hit (running)" : "Active"
color stCol = not hasT ? color.gray : gone ? bearCol : pStg > 0 ? bullCol : color.white
string dirTxt = gone ? "" : pDir == 1 ? "BUY" : pDir == -1 ? "SELL" : ""
int o = showMtf ? 4 : 0
table.cell(tbl, 0, 0, "SMC TRADE PANEL", text_color=color.white, text_size=size.normal, bgcolor=color.new(color.blue, 40))
table.merge_cells(tbl, 0, 0, 1, 0)
table.cell(tbl, 0, 1, "Trend Status (" + f_tfLabel(timeframe.period) + ")", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 1, trendTxt, text_color=trendCol, text_size=size.small, text_halign=text.align_right)
if showMtf
table.cell(tbl, 0, 2, "Trend Status (" + f_tfLabel(mtf1) + ")", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 2, f_trTxt(tr1, em1), text_color=tr1 == 1 ? bullCol : tr1 == -1 ? bearCol : color.gray, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 3, "Trend Status (" + f_tfLabel(mtf2) + ")", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 3, f_trTxt(tr2, em2), text_color=tr2 == 1 ? bullCol : tr2 == -1 ? bearCol : color.gray, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 4, "Trend Status (" + f_tfLabel(mtf3) + ")", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 4, f_trTxt(tr3, em3), text_color=tr3 == 1 ? bullCol : tr3 == -1 ? bearCol : color.gray, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 5, "Trend Status (" + f_tfLabel(mtf4) + ")", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 5, f_trTxt(tr4, em4), text_color=tr4 == 1 ? bullCol : tr4 == -1 ? bearCol : color.gray, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 2 + o, "Entry " + dirTxt, text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 2 + o, f_px(pE), text_color=color.white, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 3 + o, "SL", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 3 + o, f_px(pSL), text_color=bearCol, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 4 + o, "TP1", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 4 + o, f_px(pT1), text_color=bullCol, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 5 + o, "TP2", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 5 + o, f_px(pT2), text_color=bullCol, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 6 + o, "TP3", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 6 + o, f_px(pT3), text_color=bullCol, text_size=size.small, text_halign=text.align_right)
table.cell(tbl, 0, 7 + o, "Trade Status", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(tbl, 1, 7 + o, stTxt, text_color=stCol, text_size=size.small, text_halign=text.align_right)
// ════════════════════════════ Alerts ════════════════════════════
alertcondition(bullBos, "Bullish BOS", "Bullish BOS: close above swing high")
alertcondition(bullChoch, "Bullish CHoCH", "Bullish CHoCH: structure shifted to bullish")
alertcondition(bearBos, "Bearish BOS", "Bearish BOS: close below swing low")
alertcondition(bearChoch, "Bearish CHoCH", "Bearish CHoCH: structure shifted to bearish")
alertcondition(sigLong, "Buy Entry", "SMC Suite: BUY entry signal")
alertcondition(sigShort, "Sell Entry", "SMC Suite: SELL entry signal")