本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("SMC 3 Setups: FVG + Liquidity", shorttitle="SMC-3", overlay=true, max_lines_count=300, max_labels_count=300)
// ════════════════════════════════════════════════════════════════════
// Built from the video "The Only 3 Setups I Would Trade"
// Setup 1: HTF trend + HTF fair value gap (discount/premium) + LTF liquidity sweep
// Setup 2: HTF buy/sell-side liquidity sweep + LTF fair value gap entry
// Setup 3: Equal highs/lows + fair value gap, single timeframe
// Setups 1 and 2: run the indicator on the LOWER (entry) timeframe, e.g. 5m with HTF = 1H.
// Setup 3: run it on the timeframe you trade, e.g. 1H.
// ════════════════════════════════════════════════════════════════════
// ───────── Inputs ─────────
htf = input.timeframe("60", "Analysis (higher) timeframe, setups 1 & 2", group = "General")
pivLen = input.int(3, "Swing length (chart timeframe)", minval = 1, group = "General")
htfPivLen = input.int(3, "Swing length (higher timeframe)", minval = 1, group = "General")
atrLen = input.int(14, "ATR length", minval = 1, group = "General")
slBuf = input.float(0.1, "Stop buffer (x ATR)", minval = 0.0, step = 0.05, group = "General")
minRR = input.float(1.0, "Minimum R:R to show a signal", minval = 0.0, step = 0.1, group = "General")
entryMode = input.string("Proximal edge", "Limit entry on the gap (setups 2 & 3)", options = ["Proximal edge", "Midpoint"], group = "General")
drawLines = input.bool(true, "Draw entry / stop / target lines", group = "General")
showTable = input.bool(true, "Show dashboard", group = "General")
useS1 = input.bool(true, "Setup 1: trend + FVG + LTF sweep", group = "Setups")
useS2 = input.bool(true, "Setup 2: HTF liquidity sweep + LTF FVG", group = "Setups")
useS3 = input.bool(true, "Setup 3: equal highs/lows + FVG", group = "Setups")
s2Valid = input.int(12, "Setup 2: sweep stays valid for (higher-TF bars)", minval = 1, group = "Setup 2")
s2Mss = input.bool(true, "Setup 2: require a break of the previous swing (momentum)", group = "Setup 2")
eqTol = input.float(0.1, "Setup 3: equal-level tolerance (x ATR)", minval = 0.0, step = 0.05, group = "Setup 3")
eqNear = input.float(1.5, "Setup 3: max distance of gap from equal level (x ATR)", minval = 0.1, step = 0.1, group = "Setup 3")
eqAge = input.int(60, "Setup 3: equal level stays valid for (bars)", minval = 5, group = "Setup 3")
rrTarget = input.float(2.0, "Setup 3 (and fallback) target, R multiple", minval = 0.5, step = 0.1, group = "Setup 3")
// ───────── Helpers ─────────
// Liquidity sweep = wick through the level and close back inside,
// or a single candle closing through the level and the next candle closing back inside.
f_bullSweep(float lvl, float l0, float c0, float c1, float c2) =>
(l0 < lvl and c0 > lvl and c1 >= lvl) or (c1 < lvl and c0 > lvl and c2 >= lvl)
f_bearSweep(float lvl, float h0, float c0, float c1, float c2) =>
(h0 > lvl and c0 < lvl and c1 <= lvl) or (c1 > lvl and c0 < lvl and c2 <= lvl)
// Market structure: bullish while price holds the low that caused the last break of structure.
// Closing below that protected low flips the bias (change of character).
f_structure(int pv) =>
var float lastSH = na
var float lastSL = na
var int lastSHIdx = na
var int lastSLIdx = na
var float brkSH = na
var float brkSL = na
var int bias = 0
var float protLow = na
var float protHigh = na
var int protLowIdx = na
var int protHighIdx = na
var float rngHigh = na
var float rngLow = na
ph = ta.pivothigh(high, pv, pv)
pvl = ta.pivotlow(low, pv, pv)
if not na(ph)
lastSH := ph
lastSHIdx := bar_index - pv
if not na(pvl)
lastSL := pvl
lastSLIdx := bar_index - pv
if bias == 1 and close < protLow
bias := 0
if bias == -1 and close > protHigh
bias := 0
bool bosUp = bias != -1 and not na(lastSH) and not na(lastSL) and close > lastSH and lastSH != nz(brkSH)
bool bosDn = bias != 1 and not na(lastSH) and not na(lastSL) and close < lastSL and lastSL != nz(brkSL)
if bosUp
bias := 1
brkSH := lastSH
protLow := lastSL
protLowIdx := lastSLIdx
float hh = high
int nb = math.min(bar_index - protLowIdx, 300)
for i = 0 to nb
hh := math.max(hh, high[i])
rngHigh := hh
else if bosDn
bias := -1
brkSL := lastSL
protHigh := lastSH
protHighIdx := lastSHIdx
float ll = low
int nb2 = math.min(bar_index - protHighIdx, 300)
for i = 0 to nb2
ll := math.min(ll, low[i])
rngLow := ll
else
if bias == 1
rngHigh := math.max(rngHigh, high)
if bias == -1
rngLow := math.min(rngLow, low)
[bias, protLow, protHigh, protLowIdx, protHighIdx, rngHigh, rngLow]
// Everything calculated on the higher timeframe. Returns last COMPLETED bar values (no repaint).
f_htf(int pv) =>
[bias, pLow, pHigh, pLowIdx, pHighIdx, rHigh, rLow] = f_structure(pv)
// Setup 1: unmitigated FVG inside the current range, in the discount (bull) or premium (bear) half
float zTop = na
float zBot = na
if bias == 1 and not na(pLowIdx) and not na(rHigh)
float eqU = (pLow + rHigh) / 2
int n = math.min(bar_index - pLowIdx, 250)
float minAfter = 1e10
if n >= 2
for i = 0 to n - 2
if low[i] > high[i + 2]
float b = high[i + 2]
float t = low[i]
if minAfter >= b and (b + t) / 2 <= eqU and b >= pLow
if na(zBot) or b < zBot
zBot := b
zTop := t
minAfter := math.min(minAfter, close[i])
else if bias == -1 and not na(pHighIdx) and not na(rLow)
float eqD = (pHigh + rLow) / 2
int n2 = math.min(bar_index - pHighIdx, 250)
float maxAfter = -1e10
if n2 >= 2
for i = 0 to n2 - 2
if high[i] < low[i + 2]
float b2 = high[i]
float t2 = low[i + 2]
if maxAfter <= t2 and (b2 + t2) / 2 >= eqD and t2 <= pHigh
if na(zTop) or t2 > zTop
zTop := t2
zBot := b2
maxAfter := math.max(maxAfter, close[i])
// Setup 2: buy-side / sell-side liquidity sweeps on the higher timeframe
var float sh1 = na
var float sh2 = na
var float sl1 = na
var float sl2 = na
var float doneH = na
var float doneL = na
var int s2Dir = 0
var float s2Ext = na
var float s2Tgt = na
var int s2Time = na
phv = ta.pivothigh(high, pv, pv)
plv = ta.pivotlow(low, pv, pv)
if not na(phv)
sh2 := sh1
sh1 := phv
if not na(plv)
sl2 := sl1
sl1 := plv
float lvlH = na
if f_bearSweep(sh1, high, close, close[1], close[2]) and sh1 != nz(doneH)
lvlH := sh1
else if f_bearSweep(sh2, high, close, close[1], close[2]) and sh2 != nz(doneH)
lvlH := sh2
float lvlL = na
if f_bullSweep(sl1, low, close, close[1], close[2]) and sl1 != nz(doneL)
lvlL := sl1
else if f_bullSweep(sl2, low, close, close[1], close[2]) and sl2 != nz(doneL)
lvlL := sl2
if not na(lvlH)
doneH := lvlH
s2Dir := -1
s2Ext := math.max(high, high[1])
s2Time := time
float tg = na
if not na(sl1) and sl1 < close
tg := sl1
else if not na(sl2) and sl2 < close
tg := sl2
s2Tgt := tg
else if not na(lvlL)
doneL := lvlL
s2Dir := 1
s2Ext := math.min(low, low[1])
s2Time := time
float tg2 = na
if not na(sh1) and sh1 > close
tg2 := sh1
else if not na(sh2) and sh2 > close
tg2 := sh2
s2Tgt := tg2
[bias[1], pLow[1], pHigh[1], rHigh[1], rLow[1], zTop[1], zBot[1], s2Dir[1], s2Ext[1], s2Tgt[1], s2Time[1]]
// Draws the signal label and the trade lines
f_signal(bool isLong, string txt, float entry, float sl, float tp, bool draw) =>
color c = isLong ? color.green : color.red
label.new(bar_index, isLong ? low : high, txt + "\n" + str.tostring(entry, format.mintick), style = isLong ? label.style_label_up : label.style_label_down, color = c, textcolor = color.white, size = size.small)
if draw
line.new(bar_index, entry, bar_index + 25, entry, color = color.gray, style = line.style_dashed)
line.new(bar_index, sl, bar_index + 25, sl, color = color.red)
line.new(bar_index, tp, bar_index + 25, tp, color = color.green)
0
// ───────── Data ─────────
atr = ta.atr(atrLen)
tfOK = timeframe.in_seconds(timeframe.period) * 2 <= timeframe.in_seconds(htf)
[hBias, hPLow, hPHigh, hRHigh, hRLow, hZTop, hZBot, hS2Dir, hS2Ext, hS2Tgt, hS2Time] = request.security(syminfo.tickerid, htf, f_htf(htfPivLen), lookahead = barmerge.lookahead_on)
[cBias, cPLow, cPHigh, cPLowIdx, cPHighIdx, cRHigh, cRLow] = f_structure(pivLen)
// chart-timeframe swings
pivH = ta.pivothigh(high, pivLen, pivLen)
pivL = ta.pivotlow(low, pivLen, pivLen)
var float lh1 = na
var float lh2 = na
var float ll1 = na
var float ll2 = na
if not na(pivH)
lh2 := lh1
lh1 := pivH
if not na(pivL)
ll2 := ll1
ll1 := pivL
bool bullFvg = low > high[2]
bool bearFvg = high < low[2]
bool confirmed = barstate.isconfirmed
// ───────── Setup 1: HTF trend + FVG + LTF liquidity sweep ─────────
var float doneLL = na
var float doneLH = na
float swLvlL = na
if f_bullSweep(ll1, low, close, close[1], close[2]) and ll1 != nz(doneLL)
swLvlL := ll1
else if f_bullSweep(ll2, low, close, close[1], close[2]) and ll2 != nz(doneLL)
swLvlL := ll2
float swLvlH = na
if f_bearSweep(lh1, high, close, close[1], close[2]) and lh1 != nz(doneLH)
swLvlH := lh1
else if f_bearSweep(lh2, high, close, close[1], close[2]) and lh2 != nz(doneLH)
swLvlH := lh2
bool s1On = useS1 and tfOK and confirmed
float sweepLow = math.min(low, low[1])
float sweepHigh = math.max(high, high[1])
float s1LEntry = close
float s1LSL = sweepLow - slBuf * atr
float s1LTP = hRHigh
bool s1LongOK = s1On and hBias == 1 and not na(swLvlL) and not na(hZTop) and sweepLow <= hZTop and high >= hZBot and close > hZBot and s1LTP > s1LEntry and s1LEntry > s1LSL and (s1LTP - s1LEntry) >= minRR * (s1LEntry - s1LSL)
float s1SEntry = close
float s1SSL = sweepHigh + slBuf * atr
float s1STP = hRLow
bool s1ShortOK = s1On and hBias == -1 and not na(swLvlH) and not na(hZTop) and sweepHigh >= hZBot and low <= hZTop and close < hZTop and s1STP < s1SEntry and s1SSL > s1SEntry and (s1SEntry - s1STP) >= minRR * (s1SSL - s1SEntry)
if confirmed and not na(swLvlL)
doneLL := swLvlL
if confirmed and not na(swLvlH)
doneLH := swLvlH
if s1LongOK
f_signal(true, "S1 BUY", s1LEntry, s1LSL, s1LTP, drawLines)
if s1ShortOK
f_signal(false, "S1 SELL", s1SEntry, s1SSL, s1STP, drawLines)
// ───────── Setup 2: HTF liquidity sweep + LTF FVG ─────────
int s2Bars = math.max(1, math.round(s2Valid * timeframe.in_seconds(htf) / timeframe.in_seconds(timeframe.period)))
var int s2Start = na
var bool s2Done = false
bool newSweep = hS2Dir != 0 and nz(hS2Time) != nz(hS2Time[1])
if newSweep
s2Start := bar_index
s2Done := false
if hS2Dir == 1 and close < hS2Ext
s2Done := true
if hS2Dir == -1 and close > hS2Ext
s2Done := true
bool s2Alive = useS2 and tfOK and confirmed and hS2Dir != 0 and not na(s2Start) and (bar_index - s2Start) <= s2Bars and not s2Done
bool mssUp = not s2Mss or (not na(lh1[1]) and close[1] > lh1[1])
bool mssDn = not s2Mss or (not na(ll1[1]) and close[1] < ll1[1])
float s2LEntry = entryMode == "Midpoint" ? (low + high[2]) / 2 : low
float s2LSL = hS2Ext - slBuf * atr
float s2LTP = (not na(hS2Tgt) and hS2Tgt > s2LEntry) ? hS2Tgt : s2LEntry + rrTarget * (s2LEntry - s2LSL)
bool s2LongOK = s2Alive and hS2Dir == 1 and bullFvg and mssUp and s2LEntry > s2LSL and (s2LTP - s2LEntry) >= minRR * (s2LEntry - s2LSL)
float s2SEntry = entryMode == "Midpoint" ? (high + low[2]) / 2 : high
float s2SSL = hS2Ext + slBuf * atr
float s2STP = (not na(hS2Tgt) and hS2Tgt < s2SEntry) ? hS2Tgt : s2SEntry - rrTarget * (s2SSL - s2SEntry)
bool s2ShortOK = s2Alive and hS2Dir == -1 and bearFvg and mssDn and s2SSL > s2SEntry and (s2SEntry - s2STP) >= minRR * (s2SSL - s2SEntry)
if s2LongOK
f_signal(true, "S2 BUY LIMIT", s2LEntry, s2LSL, s2LTP, drawLines)
s2Done := true
if s2ShortOK
f_signal(false, "S2 SELL LIMIT", s2SEntry, s2SSL, s2STP, drawLines)
s2Done := true
// ───────── Setup 3: equal highs/lows + FVG (single timeframe) ─────────
var float eqH = na
var int eqHIdx = na
var float eqL = na
var int eqLIdx = na
if not na(pivH) and not na(lh2) and math.abs(pivH - lh2) <= eqTol * atr
eqH := math.max(pivH, lh2)
eqHIdx := bar_index
if not na(pivL) and not na(ll2) and math.abs(pivL - ll2) <= eqTol * atr
eqL := math.min(pivL, ll2)
eqLIdx := bar_index
bool eqHValid = not na(eqH) and bar_index - eqHIdx <= eqAge
bool eqLValid = not na(eqL) and bar_index - eqLIdx <= eqAge
bool s3On = useS3 and confirmed
float s3SEntry = entryMode == "Midpoint" ? (high + low[2]) / 2 : high
float s3SSL = low[2] + slBuf * atr
float s3STP = s3SEntry - rrTarget * (s3SSL - s3SEntry)
bool s3ShortOK = s3On and cBias == -1 and eqHValid and bearFvg and high >= eqH and (high - eqH) <= eqNear * atr and s3SSL > s3SEntry and rrTarget >= minRR
float s3LEntry = entryMode == "Midpoint" ? (low + high[2]) / 2 : low
float s3LSL = high[2] - slBuf * atr
float s3LTP = s3LEntry + rrTarget * (s3LEntry - s3LSL)
bool s3LongOK = s3On and cBias == 1 and eqLValid and bullFvg and low <= eqL and (eqL - low) <= eqNear * atr and s3LEntry > s3LSL and rrTarget >= minRR
if s3LongOK
f_signal(true, "S3 BUY LIMIT", s3LEntry, s3LSL, s3LTP, drawLines)
if s3ShortOK
f_signal(false, "S3 SELL LIMIT", s3SEntry, s3SSL, s3STP, drawLines)
// ───────── Visuals ─────────
bool showZone = useS1 and tfOK
pZT = plot(showZone ? hZTop : na, "S1 zone top", color = color.new(color.gray, 100))
pZB = plot(showZone ? hZBot : na, "S1 zone bottom", color = color.new(color.gray, 100))
fill(pZT, pZB, color = hBias == 1 ? color.new(color.green, 85) : color.new(color.red, 85), title = "S1 FVG zone")
plot(showZone ? (hBias == 1 ? hPLow : hBias == -1 ? hPHigh : na) : na, "Protected level", color = color.orange, style = plot.style_linebr)
plot(showZone ? (hBias == 1 ? hRHigh : hBias == -1 ? hRLow : na) : na, "S1 target (recent extreme)", color = color.new(color.gray, 30), style = plot.style_linebr)
plot(useS3 and eqHValid ? eqH : na, "Equal highs", color = color.new(color.red, 40), style = plot.style_linebr)
plot(useS3 and eqLValid ? eqL : na, "Equal lows", color = color.new(color.green, 40), style = plot.style_linebr)
// ───────── Dashboard ─────────
f_biasTxt(int b) =>
b == 1 ? "Bullish" : b == -1 ? "Bearish" : "None"
var table dash = table.new(position.top_right, 2, 5, bgcolor = color.new(color.black, 25), border_width = 1)
if showTable and barstate.islast
table.cell(dash, 0, 0, "Analysis TF", text_color = color.white, text_size = size.small)
table.cell(dash, 1, 0, tfOK ? htf : htf + " (chart TF too high)", text_color = tfOK ? color.white : color.orange, text_size = size.small)
table.cell(dash, 0, 1, "HTF bias (S1)", text_color = color.white, text_size = size.small)
table.cell(dash, 1, 1, f_biasTxt(nz(hBias)), text_color = hBias == 1 ? color.lime : hBias == -1 ? color.red : color.gray, text_size = size.small)
table.cell(dash, 0, 2, "FVG zone ready", text_color = color.white, text_size = size.small)
table.cell(dash, 1, 2, not na(hZTop) ? "Yes" : "No", text_color = not na(hZTop) ? color.lime : color.gray, text_size = size.small)
table.cell(dash, 0, 3, "Chart bias (S3)", text_color = color.white, text_size = size.small)
table.cell(dash, 1, 3, f_biasTxt(cBias), text_color = cBias == 1 ? color.lime : cBias == -1 ? color.red : color.gray, text_size = size.small)
table.cell(dash, 0, 4, "HTF sweep (S2)", text_color = color.white, text_size = size.small)
table.cell(dash, 1, 4, hS2Dir == 1 ? "Sell-side swept" : hS2Dir == -1 ? "Buy-side swept" : "None", text_color = hS2Dir == 1 ? color.lime : hS2Dir == -1 ? color.red : color.gray, text_size = size.small)
// ───────── Alerts ─────────
alertcondition(s1LongOK, "Setup 1 BUY", "SMC-3 Setup 1: buy (liquidity sweep inside HTF FVG)")
alertcondition(s1ShortOK, "Setup 1 SELL", "SMC-3 Setup 1: sell (liquidity sweep inside HTF FVG)")
alertcondition(s2LongOK, "Setup 2 BUY LIMIT", "SMC-3 Setup 2: buy limit at the fair value gap")
alertcondition(s2ShortOK, "Setup 2 SELL LIMIT", "SMC-3 Setup 2: sell limit at the fair value gap")
alertcondition(s3LongOK, "Setup 3 BUY LIMIT", "SMC-3 Setup 3: buy limit at the fair value gap")
alertcondition(s3ShortOK, "Setup 3 SELL LIMIT", "SMC-3 Setup 3: sell limit at the fair value gap")