本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =====================================================================
// SMC Pro - FVG, Order Blocks, BOS / CHoCH, Major Support & Resistance,
// Trendlines + Bias / Trend panel + Latest Expected Reversal point
// Works on any symbol / timeframe (tuned for XAUUSD M5-M15).
// All structure logic runs on bar CLOSE (no repainting of signals).
// =====================================================================
indicator("SMC Pro: FVG, OB, BOS/CHoCH, S/R, Trendlines + Bias Panel", shorttitle="SMC Pro", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500, max_bars_back=500)
// ============================ INPUTS =================================
g0 = "General"
swingLen = input.int(5, "Structure swing length (BOS / CHoCH)", minval=2, maxval=30, group=g0)
atrLen = input.int(14, "ATR length", minval=1, group=g0)
htfTf = input.timeframe("60", "Bias timeframe", group=g0)
emaLen = input.int(50, "Bias EMA length", minval=2, group=g0)
rangeLen = input.int(60, "Premium / Discount range (bars)", minval=10, group=g0)
bullCol = input.color(#089981, "Bullish colour", group=g0)
bearCol = input.color(#F23645, "Bearish colour", group=g0)
g1 = "Structure"
showStruct = input.bool(true, "Show BOS / CHoCH", group=g1)
g2 = "Fair Value Gaps"
showFVG = input.bool(true, "Show FVG", group=g2)
fvgMin = input.float(0.15, "Min FVG size (x ATR)", minval=0.0, step=0.05, group=g2)
maxFVG = input.int(8, "Max active FVGs", minval=1, maxval=30, group=g2)
g3 = "Order Blocks"
showOB = input.bool(true, "Show Order Blocks", group=g3)
obLookback = input.int(100, "OB search lookback (bars)", minval=10, maxval=300, group=g3)
maxOB = input.int(6, "Max active OBs", minval=1, maxval=20, group=g3)
g4 = "Support / Resistance"
showSR = input.bool(true, "Show major Support / Resistance", group=g4)
srLen = input.int(15, "S/R pivot strength (bigger = more major)", minval=5, maxval=60, group=g4)
srTol = input.float(0.5, "Merge levels closer than (x ATR)", minval=0.1, step=0.1, group=g4)
maxSR = input.int(6, "Max S/R levels", minval=2, maxval=20, group=g4)
g5 = "Trendlines"
showTL = input.bool(true, "Show trendlines", group=g5)
tlLen = input.int(8, "Trendline pivot strength", minval=3, maxval=40, group=g5)
g6 = "Panel / Display"
showPanel = input.bool(true, "Show Bias & Trend panel (top right)", group=g6)
showRev = input.bool(true, "Mark latest expected reversal on chart", group=g6)
showMit = input.bool(false, "Keep mitigated zones (faded)", group=g6)
// ============================ STORAGE ================================
var array<box> fvgBox = array.new_box()
var array<int> fvgDir = array.new_int()
var array<float> fvgTop = array.new_float()
var array<float> fvgBot = array.new_float()
var array<box> obBox = array.new_box()
var array<int> obDir = array.new_int()
var array<float> obTop = array.new_float()
var array<float> obBot = array.new_float()
var array<line> srLn = array.new_line()
var array<label> srLb = array.new_label()
var array<float> srPx = array.new_float()
var array<int> srTy = array.new_int()
// ============================ HELPERS ================================
f_fmt(float x) =>
na(x) ? "-" : str.tostring(x, format.mintick)
// store a zone (dir: 1 bullish, -1 bearish) and trim the oldest
f_addZone(bx, dr, tp, bt, b, d, t, btm, maxN) =>
array.push(bx, b)
array.push(dr, d)
array.push(tp, t)
array.push(bt, btm)
if array.size(bx) > maxN
box.delete(array.shift(bx))
array.shift(dr)
array.shift(tp)
array.shift(bt)
true
// mitigation: bullish zone dies when price CLOSES below it, bearish when above
f_mitigate(bx, dr, tp, bt) =>
int n = array.size(bx)
if n > 0
for i = n - 1 to 0
int d = array.get(dr, i)
float t = array.get(tp, i)
float bm = array.get(bt, i)
bool hit = d == 1 ? close < bm : close > t
if hit
box bb = array.get(bx, i)
if showMit
box.set_extend(bb, extend.none)
box.set_right(bb, bar_index)
box.set_bgcolor(bb, color.new(color.gray, 90))
box.set_border_color(bb, color.new(color.gray, 70))
else
box.delete(bb)
array.remove(bx, i)
array.remove(dr, i)
array.remove(tp, i)
array.remove(bt, i)
true
// candle index (0 = current bar) of the extreme candle since 'fromBar'
f_obIdx(int fromBar, bool bull) =>
int n = math.min(bar_index - fromBar, obLookback)
int idx = 0
float best = bull ? 1.0e10 : -1.0e10
for i = 0 to n
if bull
if low[i] < best
best := low[i]
idx := i
else
if high[i] > best
best := high[i]
idx := i
idx
// add a Support / Resistance level (ty: 1 support, -1 resistance)
f_addSR(float px, int ty, float tol) =>
bool near = false
int n = array.size(srPx)
if n > 0
for i = 0 to n - 1
if math.abs(array.get(srPx, i) - px) <= tol
near := true
if not near
color col = ty == -1 ? color.new(bearCol, 10) : color.new(bullCol, 10)
line ln = line.new(bar_index - srLen, px, bar_index, px, extend=extend.right, color=col, width=2)
label lb = label.new(bar_index - srLen, px, ty == -1 ? "R" : "S", style=ty == -1 ? label.style_label_down : label.style_label_up, color=col, textcolor=color.white, size=size.tiny)
array.push(srLn, ln)
array.push(srLb, lb)
array.push(srPx, px)
array.push(srTy, ty)
if array.size(srLn) > maxSR
line.delete(array.shift(srLn))
label.delete(array.shift(srLb))
array.shift(srPx)
array.shift(srTy)
true
// nearest supply (above) / demand (below) among stored zones
f_scan(dr, tp, bt, string nm, float sL0, string sT0, float dL0, string dT0) =>
float sL = sL0
string sT = sT0
float dL = dL0
string dT = dT0
int n = array.size(dr)
if n > 0
for i = 0 to n - 1
int d = array.get(dr, i)
float t = array.get(tp, i)
float b = array.get(bt, i)
if d == -1 and b > close and (na(sL) or b < sL)
sL := b
sT := "Bear " + nm
if d == 1 and t < close and (na(dL) or t > dL)
dL := t
dT := "Bull " + nm
[sL, sT, dL, dT]
// ============================ CALCULATIONS ===========================
atr = ta.atr(atrLen)
[htfC, htfE] = request.security(syminfo.tickerid, htfTf, [close[1], ta.ema(close, emaLen)[1]], lookahead=barmerge.lookahead_on)
int htfB = htfC > htfE ? 1 : htfC < htfE ? -1 : 0
float rHi = ta.highest(high, rangeLen)
float rLo = ta.lowest(low, rangeLen)
float ph = ta.pivothigh(high, swingLen, swingLen)
float pl = ta.pivotlow(low, swingLen, swingLen)
float srPh = ta.pivothigh(high, srLen, srLen)
float srPl = ta.pivotlow(low, srLen, srLen)
float tlPh = ta.pivothigh(high, tlLen, tlLen)
float tlPl = ta.pivotlow(low, tlLen, tlLen)
// ---- zone mitigation (every confirmed bar, before new zones are added)
if barstate.isconfirmed
if showFVG
f_mitigate(fvgBox, fvgDir, fvgTop, fvgBot)
if showOB
f_mitigate(obBox, obDir, obTop, obBot)
// ============================ FAIR VALUE GAPS ========================
bool bullFVG = barstate.isconfirmed and low > high[2] and (low - high[2]) >= fvgMin * atr
bool bearFVG = barstate.isconfirmed and high < low[2] and (low[2] - high) >= fvgMin * atr
if showFVG
if bullFVG
box fb = box.new(bar_index - 2, low, bar_index, high[2], extend=extend.right, border_color=color.new(bullCol, 60), bgcolor=color.new(bullCol, 85), text="FVG", text_color=color.new(bullCol, 20), text_size=size.tiny)
f_addZone(fvgBox, fvgDir, fvgTop, fvgBot, fb, 1, low, high[2], maxFVG)
if bearFVG
box fb2 = box.new(bar_index - 2, low[2], bar_index, high, extend=extend.right, border_color=color.new(bearCol, 60), bgcolor=color.new(bearCol, 85), text="FVG", text_color=color.new(bearCol, 20), text_size=size.tiny)
f_addZone(fvgBox, fvgDir, fvgTop, fvgBot, fb2, -1, low[2], high, maxFVG)
// ============================ STRUCTURE: BOS / CHoCH =================
var float shP = na
var int shB = na
var bool shX = true
var float slP = na
var int slB = na
var bool slX = true
var int trend = 0
var string lastBrk = "-"
var float lastBrkPx = na
var int lastBrkDir = 0
if not na(ph)
shP := ph
shB := bar_index - swingLen
shX := false
if not na(pl)
slP := pl
slB := bar_index - swingLen
slX := false
bool bullBOS = false
bool bullCHoCH = false
bool bearBOS = false
bool bearCHoCH = false
bool bullBrk = barstate.isconfirmed and not na(shP) and not shX and close > shP
bool bearBrk = barstate.isconfirmed and not na(slP) and not slX and close < slP
if bullBrk
bool choch = trend == -1
if choch
bullCHoCH := true
else
bullBOS := true
if showStruct
line.new(shB, shP, bar_index, shP, color=bullCol, style=choch ? line.style_dotted : line.style_dashed, width=choch ? 2 : 1)
label.new(math.round(math.avg(shB, bar_index)), shP, choch ? "CHoCH" : "BOS", style=label.style_label_down, color=choch ? color.orange : color.new(bullCol, 20), textcolor=color.white, size=size.tiny)
if showOB
int oi = f_obIdx(shB, true)
box ob1 = box.new(bar_index - oi, high[oi], bar_index, low[oi], extend=extend.right, border_color=bullCol, bgcolor=color.new(bullCol, 75), text="OB", text_color=color.new(bullCol, 0), text_size=size.tiny)
f_addZone(obBox, obDir, obTop, obBot, ob1, 1, high[oi], low[oi], maxOB)
lastBrk := choch ? "CHoCH \u2191" : "BOS \u2191"
lastBrkPx := shP
lastBrkDir := 1
shX := true
trend := 1
if bearBrk
bool choch2 = trend == 1
if choch2
bearCHoCH := true
else
bearBOS := true
if showStruct
line.new(slB, slP, bar_index, slP, color=bearCol, style=choch2 ? line.style_dotted : line.style_dashed, width=choch2 ? 2 : 1)
label.new(math.round(math.avg(slB, bar_index)), slP, choch2 ? "CHoCH" : "BOS", style=label.style_label_up, color=choch2 ? color.orange : color.new(bearCol, 20), textcolor=color.white, size=size.tiny)
if showOB
int oi2 = f_obIdx(slB, false)
box ob2 = box.new(bar_index - oi2, high[oi2], bar_index, low[oi2], extend=extend.right, border_color=bearCol, bgcolor=color.new(bearCol, 75), text="OB", text_color=color.new(bearCol, 0), text_size=size.tiny)
f_addZone(obBox, obDir, obTop, obBot, ob2, -1, high[oi2], low[oi2], maxOB)
lastBrk := choch2 ? "CHoCH \u2193" : "BOS \u2193"
lastBrkPx := slP
lastBrkDir := -1
slX := true
trend := -1
// ============================ MAJOR SUPPORT / RESISTANCE =============
if showSR
if not na(srPh)
f_addSR(srPh, -1, srTol * nz(atr, 0.0))
if not na(srPl)
f_addSR(srPl, 1, srTol * nz(atr, 0.0))
// remove a level once price closes decisively through it
if barstate.isconfirmed
int ns = array.size(srPx)
if ns > 0
for i = ns - 1 to 0
float lvl = array.get(srPx, i)
int lty = array.get(srTy, i)
bool brk = lty == -1 ? close > lvl + srTol * atr : close < lvl - srTol * atr
if brk
line.delete(array.get(srLn, i))
label.delete(array.get(srLb, i))
array.remove(srLn, i)
array.remove(srLb, i)
array.remove(srPx, i)
array.remove(srTy, i)
// ============================ TRENDLINES =============================
var float pl1P = na
var int pl1B = na
var float ph1P = na
var int ph1B = na
var line upTL = na
var line dnTL = na
if showTL
// ascending trendline through higher lows
if not na(tlPl)
int nb = bar_index - tlLen
if not na(pl1P) and tlPl > pl1P
if not na(upTL)
line.delete(upTL)
upTL := line.new(pl1B, pl1P, nb, tlPl, extend=extend.right, color=bullCol, width=2)
else
if not na(upTL)
line.delete(upTL)
upTL := na
pl1P := tlPl
pl1B := nb
// descending trendline through lower highs
if not na(tlPh)
int nb2 = bar_index - tlLen
if not na(ph1P) and tlPh < ph1P
if not na(dnTL)
line.delete(dnTL)
dnTL := line.new(ph1B, ph1P, nb2, tlPh, extend=extend.right, color=bearCol, width=2)
else
if not na(dnTL)
line.delete(dnTL)
dnTL := na
ph1P := tlPh
ph1B := nb2
// trendline break (close beyond the line)
if barstate.isconfirmed
if not na(upTL)
if close < line.get_price(upTL, bar_index)
line.set_extend(upTL, extend.none)
line.set_x2(upTL, bar_index)
line.set_y2(upTL, line.get_price(upTL, bar_index))
line.set_style(upTL, line.style_dotted)
line.set_color(upTL, color.gray)
label.new(bar_index, high, "TL break \u2193", style=label.style_label_down, color=color.new(color.gray, 20), textcolor=color.white, size=size.tiny)
upTL := na
if not na(dnTL)
if close > line.get_price(dnTL, bar_index)
line.set_extend(dnTL, extend.none)
line.set_x2(dnTL, bar_index)
line.set_y2(dnTL, line.get_price(dnTL, bar_index))
line.set_style(dnTL, line.style_dotted)
line.set_color(dnTL, color.gray)
label.new(bar_index, low, "TL break \u2191", style=label.style_label_up, color=color.new(color.gray, 20), textcolor=color.white, size=size.tiny)
dnTL := na
// ============================ PANEL + EXPECTED REVERSAL ==============
var table panel = table.new(position.top_right, 2, 10, bgcolor=color.new(#131722, 10), border_width=1, border_color=color.new(color.gray, 60))
var line revLn = na
var label revLb = na
if barstate.islast
// ---- nearest supply above / demand below (FVG, OB, S/R)
[sA, sAt, dA, dAt] = f_scan(fvgDir, fvgTop, fvgBot, "FVG", float(na), "", float(na), "")
[sB, sBt, dB, dBt] = f_scan(obDir, obTop, obBot, "OB", sA, sAt, dA, dAt)
float supplyLvl = sB
string supplyTxt = sBt
float demandLvl = dB
string demandTxt = dBt
int nsr = array.size(srPx)
if nsr > 0
for i = 0 to nsr - 1
float p = array.get(srPx, i)
int ty = array.get(srTy, i)
if ty == -1 and p > close and (na(supplyLvl) or p < supplyLvl)
supplyLvl := p
supplyTxt := "Resistance"
if ty == 1 and p < close and (na(demandLvl) or p > demandLvl)
demandLvl := p
demandTxt := "Support"
// ---- bias
int score = trend + htfB
string biasTxt = score >= 2 ? "STRONG BULLISH" : score == 1 ? "BULLISH" : score == 0 ? "NEUTRAL / MIXED" : score == -1 ? "BEARISH" : "STRONG BEARISH"
color biasCol = score > 0 ? color.lime : score < 0 ? color.red : color.gray
string trendTxt = trend == 1 ? "Bullish \u25B2" : trend == -1 ? "Bearish \u25BC" : "Neutral"
color trendCol = trend == 1 ? color.lime : trend == -1 ? color.red : color.gray
string htfTxt = htfB == 1 ? "Bullish \u25B2" : htfB == -1 ? "Bearish \u25BC" : "Neutral"
color htfCol = htfB == 1 ? color.lime : htfB == -1 ? color.red : color.gray
float eqP = (rHi + rLo) / 2.0
float pct = rHi > rLo ? (close - rLo) / (rHi - rLo) * 100.0 : 50.0
string pdTxt = (close > eqP ? "Premium" : "Discount") + " (" + str.tostring(pct, "0") + "%)"
color pdCol = close > eqP ? color.new(color.red, 0) : color.new(color.lime, 0)
// ---- latest expected reversal: bullish -> top at supply above, bearish -> bottom at demand below
int dirB = trend != 0 ? trend : htfB
float revLvl = na
string revTxt = ""
if dirB >= 0
revLvl := na(supplyLvl) ? rHi : supplyLvl
revTxt := na(supplyLvl) ? "Range high" : supplyTxt
else
revLvl := na(demandLvl) ? rLo : demandLvl
revTxt := na(demandLvl) ? "Range low" : demandTxt
bool revIsTop = dirB >= 0
float revDist = math.abs(revLvl - close)
float revPct = revDist / close * 100.0
color revCol = revIsTop ? color.red : color.lime
if showPanel
table.cell(panel, 0, 0, "SMC Pro", text_color=color.white, text_size=size.small, bgcolor=color.new(color.blue, 40))
table.cell(panel, 1, 0, syminfo.ticker + " " + timeframe.period, text_color=color.white, text_size=size.small, bgcolor=color.new(color.blue, 40))
table.cell(panel, 0, 1, "Trend (structure)", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 1, trendTxt, text_color=trendCol, text_size=size.small)
table.cell(panel, 0, 2, "HTF bias (" + htfTf + ")", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 2, htfTxt, text_color=htfCol, text_size=size.small)
table.cell(panel, 0, 3, "Overall bias", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 3, biasTxt, text_color=biasCol, text_size=size.small)
table.cell(panel, 0, 4, "Last break", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 4, lastBrk + " @ " + f_fmt(lastBrkPx), text_color=lastBrkDir >= 0 ? color.lime : color.red, text_size=size.small)
table.cell(panel, 0, 5, "Price zone", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 5, pdTxt, text_color=pdCol, text_size=size.small)
table.cell(panel, 0, 6, "Demand / Support", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 6, na(demandLvl) ? "-" : f_fmt(demandLvl) + " (" + demandTxt + ")", text_color=color.lime, text_size=size.small)
table.cell(panel, 0, 7, "Supply / Resistance", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 7, na(supplyLvl) ? "-" : f_fmt(supplyLvl) + " (" + supplyTxt + ")", text_color=color.red, text_size=size.small)
table.cell(panel, 0, 8, "Exp. reversal", text_color=color.white, text_size=size.small, bgcolor=color.new(color.orange, 60))
table.cell(panel, 1, 8, (revIsTop ? "\u25BC " : "\u25B2 ") + f_fmt(revLvl) + " (" + revTxt + ")", text_color=revCol, text_size=size.small, bgcolor=color.new(color.orange, 60))
table.cell(panel, 0, 9, "Distance", text_color=color.white, text_size=size.small)
table.cell(panel, 1, 9, f_fmt(revDist) + " (" + str.tostring(revPct, "0.00") + "%)", text_color=color.white, text_size=size.small)
if showRev and not na(revLvl)
string rt = "Expected reversal " + (revIsTop ? "\u25BC " : "\u25B2 ") + f_fmt(revLvl) + " (" + revTxt + ")"
if na(revLn)
revLn := line.new(bar_index, revLvl, bar_index + 12, revLvl, color=revCol, style=line.style_dotted, width=2)
revLb := label.new(bar_index + 12, revLvl, rt, style=label.style_label_left, color=revCol, textcolor=color.white, size=size.small)
else
line.set_xy1(revLn, bar_index, revLvl)
line.set_xy2(revLn, bar_index + 12, revLvl)
line.set_color(revLn, revCol)
label.set_xy(revLb, bar_index + 12, revLvl)
label.set_text(revLb, rt)
label.set_color(revLb, revCol)
// ============================ ALERTS =================================
alertcondition(bullBOS, title="Bullish BOS", message="Bullish BOS")
alertcondition(bullCHoCH, title="Bullish CHoCH", message="Bullish CHoCH")
alertcondition(bearBOS, title="Bearish BOS", message="Bearish BOS")
alertcondition(bearCHoCH, title="Bearish CHoCH", message="Bearish CHoCH")
alertcondition(bullFVG, title="New Bullish FVG", message="New Bullish FVG")
alertcondition(bearFVG, title="New Bearish FVG", message="New Bearish FVG")