本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0
// ICT Confluence Signals — Liquidity Sweep → Displacement MSS → FVG retrace
//
// Model (the "ICT 2022 / Silver Bullet" style entry most often cited as the
// highest-probability ICT setup):
// 1. Liquidity sweep : price wicks through a swing low/high or previous-day
// low/high and closes back inside (stop run).
// 2. Market structure : a candle CLOSES through the last internal swing in the
// shift (MSS) opposite direction within N bars of the sweep.
// 3. Displacement : a large-bodied candle (≥ ATR × mult) drives the MSS.
// 4. Fair value gap : the displacement leaves an FVG; entry when price
// retraces into it.
// Score (0-5) adds: displacement, FVG, killzone, HTF bias, discount/premium.
//
// Signals use only confirmed pivots and closed-bar data (no lookahead), so
// historical signals do not repaint. Set alerts "Once Per Bar Close".
// Nothing here guarantees profit — check the stats table on YOUR market and
// timeframe, and forward-test before risking money.
//@version=6
indicator("ICT Confluence Signals", "ICT Signals", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// ───────────────────────────── Inputs ─────────────────────────────
const string G1 = "Liquidity & Structure"
extLen = input.int(10, "Liquidity swing length", minval = 2, group = G1, tooltip = "Bars left/right that define a swing high/low whose stops are the liquidity to be swept.")
intLen = input.int(3, "Internal swing length (MSS)", minval = 1, group = G1, tooltip = "Smaller swings used to confirm the market structure shift.")
usePD = input.bool(true, "Previous-day high/low as liquidity", group = G1)
maxToMss = input.int(30, "Max bars: sweep → MSS", minval = 1, group = G1)
maxToEntry = input.int(20, "Max bars: MSS → entry", minval = 1, group = G1)
const string G2 = "Displacement & FVG"
dispMult = input.float(1.0, "Displacement body ≥ ATR ×", minval = 0, step = 0.1, group = G2)
minFvg = input.float(0.1, "Min FVG size (ATR ×)", minval = 0, step = 0.05, group = G2)
needFvg = input.bool(true, "Require FVG entry", group = G2, tooltip = "Off: if no FVG forms, enter on a retest of the MSS level instead.")
const string G3 = "Confluence filters"
useKZ = input.bool(true, "Score killzones (New York time)", group = G3)
kz1 = input.session("0200-0500", "London", group = G3)
kz2 = input.session("0700-1100", "New York AM (incl. Silver Bullet 10-11)", group = G3)
kz3 = input.session("1330-1600", "New York PM", group = G3)
showKZ = input.bool(true, "Shade killzones", group = G3)
biasMode = input.string("HTF open + prior candle", "HTF bias", options = ["Off", "HTF open", "HTF open + prior candle"], group = G3)
biasTf = input.timeframe("D", "Bias timeframe", group = G3)
minScore = input.int(4, "Minimum score to signal (0-5)", minval = 0, maxval = 5, group = G3, tooltip = "5 = A+, 4 = A, 3 = B. Higher = fewer but cleaner signals.")
const string G4 = "Risk"
tpMode = input.string("Fixed R", "Target", options = ["Fixed R", "Opposite liquidity"], group = G4, tooltip = "Opposite liquidity: target the nearest untaken swing high/low if it is at least the R multiple away, else Fixed R.")
rrIn = input.float(2.0, "R multiple", minval = 0.5, step = 0.25, group = G4)
slBuf = input.float(0.1, "Stop buffer beyond sweep (ATR ×)", minval = 0, step = 0.05, group = G4)
const string G5 = "Display"
showSweep = input.bool(true, "Sweeps", group = G5)
showMss = input.bool(true, "MSS lines", group = G5)
showFvg = input.bool(true, "Entry FVGs", group = G5)
showLevels = input.bool(true, "Entry / SL / TP lines", group = G5)
showTable = input.bool(true, "Stats table", group = G5)
bullCol = input.color(#089981, "Bull", group = G5)
bearCol = input.color(#F23645, "Bear", group = G5)
// ───────────────────────────── Core series ─────────────────────────────
atr = ta.atr(14)
bodyUpMax = ta.highest(close - open, 3)
bodyDnMax = ta.highest(open - close, 3)
fbHi = ta.highest(high, intLen * 2 + 1)[1]
fbLo = ta.lowest(low, intLen * 2 + 1)[1]
// Killzones
intraday = timeframe.isintraday and timeframe.in_seconds() <= 3600
inKZ = not na(time(timeframe.period, kz1, "America/New_York")) or not na(time(timeframe.period, kz2, "America/New_York")) or not na(time(timeframe.period, kz3, "America/New_York"))
kzOk = not useKZ or not intraday or inKZ
bgcolor(showKZ and useKZ and intraday and inKZ ? color.new(color.gray, 93) : na, title = "Killzone")
// HTF bias (no lookahead leak: current HTF open is known, prior candle is closed)
[hO, hPC, hPO] = request.security(syminfo.tickerid, biasTf, [open, close[1], open[1]], lookahead = barmerge.lookahead_on)
biasBull = biasMode == "Off" or (close > hO and (biasMode == "HTF open" or hPC > hPO))
biasBear = biasMode == "Off" or (close < hO and (biasMode == "HTF open" or hPC < hPO))
// Previous-day liquidity
[pdh, pdl] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead = barmerge.lookahead_on)
usePDok = usePD and timeframe.isintraday
var bool pdhTaken = false
var bool pdlTaken = false
if timeframe.change("D")
pdhTaken := false
pdlTaken := false
// Swings
ph = ta.pivothigh(high, extLen, extLen)
pl = ta.pivotlow(low, extLen, extLen)
iph = ta.pivothigh(high, intLen, intLen)
ipl = ta.pivotlow(low, intLen, intLen)
var float extHi = na
var float extLo = na
var bool extHiTaken = true
var bool extLoTaken = true
var float intHi = na
var float intLo = na
var int intHiBar = na
var int intLoBar = na
if not na(ph)
extHi := ph
extHiTaken := false
if not na(pl)
extLo := pl
extLoTaken := false
if not na(iph)
intHi := iph
intHiBar := bar_index - intLen
if not na(ipl)
intLo := ipl
intLoBar := bar_index - intLen
// Sweeps: wick through liquidity, close back inside
bullSweep = (not extLoTaken and low < extLo and close > extLo) or (usePDok and not pdlTaken and low < pdl and close > pdl)
bearSweep = (not extHiTaken and high > extHi and close < extHi) or (usePDok and not pdhTaken and high > pdh and close < pdh)
if low < extLo
extLoTaken := true
if high > extHi
extHiTaken := true
if usePDok and low < pdl
pdlTaken := true
if usePDok and high > pdh
pdhTaken := true
plotshape(showSweep and bullSweep, "Sell-side sweep", shape.xcross, location.belowbar, color.new(bullCol, 30), size = size.tiny)
plotshape(showSweep and bearSweep, "Buy-side sweep", shape.xcross, location.abovebar, color.new(bearCol, 30), size = size.tiny)
// Fair value gaps
bullFvg = low > high[2] and low - high[2] >= atr * minFvg
bearFvg = high < low[2] and low[2] - high >= atr * minFvg
var float buTop = na
var float buBot = na
var int buBar = na
var float beTop = na
var float beBot = na
var int beBar = na
if bullFvg
buTop := low
buBot := high[2]
buBar := bar_index
if bearFvg
beTop := low[2]
beBot := high
beBar := bar_index
// ───────────────────────────── Trade tracking ─────────────────────────────
var int tDir = 0
var float tEn = na
var float tSl = na
var float tTp = na
var int tBar = na
var line lnEn = na
var line lnSl = na
var line lnTp = na
var int wins = 0
var int losses = 0
var float sumR = 0.0
if tDir != 0 and bar_index > tBar
if showLevels
line.set_x2(lnEn, bar_index)
line.set_x2(lnSl, bar_index)
line.set_x2(lnTp, bar_index)
hitSl = tDir == 1 ? low <= tSl : high >= tSl
hitTp = tDir == 1 ? high >= tTp : low <= tTp
if hitSl // stop checked first = conservative when both hit in one bar
losses += 1
sumR -= 1.0
tDir := 0
else if hitTp
wins += 1
sumR += math.abs(tTp - tEn) / math.abs(tEn - tSl)
tDir := 0
grade(int s) => s == 5 ? "A+" : s == 4 ? "A" : s == 3 ? "B" : "C"
tick(bool b) => b ? "✓" : "✗"
// ───────────────────────────── Bullish setup ─────────────────────────────
var int bS = 0
var float bSw = na
var float bMss = na
var float bLeg = na
var float bZT = na
var float bZB = na
var int bSwBar = na
var int bMssBar = na
var int bMssFrom = na
var int bZBar = na
var bool bDisp = false
var bool bFvg = false
buySig = false
int buyScore = 0
string buyTip = ""
if bullSweep and (bS != 2 or low < bSw)
bS := 1
bSw := low
bSwBar := bar_index
useInt = not na(intHi) and intHi > close
bMss := useInt ? intHi : fbHi
bMssFrom := useInt ? intHiBar : bar_index - 1
bFvg := false
else if bS == 1
bSw := math.min(bSw, low)
if bar_index - bSwBar > maxToMss
bS := 0
else if close > bMss
bS := 2
bMssBar := bar_index
bLeg := high
bDisp := bodyUpMax >= atr * dispMult
if showMss
line.new(bMssFrom, bMss, bar_index, bMss, color = bullCol, style = line.style_dashed)
label.new(bar_index, bMss, "MSS", style = label.style_none, textcolor = bullCol, size = size.tiny)
else if bS == 2
bLeg := math.max(bLeg, high)
if bS == 2 and not bFvg and buBar >= bSwBar
bFvg := true
bZT := buTop
bZB := buBot
bZBar := buBar
if showFvg
box.new(buBar - 2, buTop, buBar + 10, buBot, border_color = color.new(bullCol, 60), bgcolor = color.new(bullCol, 85))
if bS == 2 and bar_index > bMssBar
if close < bSw or bar_index - bMssBar > maxToEntry or (bFvg and close < bZB)
bS := 0
else
zt = bFvg ? bZT : bMss
canEnter = (bFvg and bar_index > bZBar) or (not bFvg and not needFvg)
if canEnter and low <= zt
disc = close <= (bSw + bLeg) / 2
buyScore := (bDisp ? 1 : 0) + (bFvg ? 1 : 0) + (kzOk ? 1 : 0) + (biasBull ? 1 : 0) + (disc ? 1 : 0)
buyTip := "Sweep ✓\nMSS ✓\nDisplacement " + tick(bDisp) + "\nFVG " + tick(bFvg) + "\nKillzone " + tick(kzOk) + "\nHTF bias " + tick(biasBull) + "\nDiscount " + tick(disc)
buySig := buyScore >= minScore
bS := 0
// ───────────────────────────── Bearish setup ─────────────────────────────
var int sS = 0
var float sSw = na
var float sMss = na
var float sLeg = na
var float sZT = na
var float sZB = na
var int sSwBar = na
var int sMssBar = na
var int sMssFrom = na
var int sZBar = na
var bool sDisp = false
var bool sFvg = false
sellSig = false
int sellScore = 0
string sellTip = ""
if bearSweep and (sS != 2 or high > sSw)
sS := 1
sSw := high
sSwBar := bar_index
useInt = not na(intLo) and intLo < close
sMss := useInt ? intLo : fbLo
sMssFrom := useInt ? intLoBar : bar_index - 1
sFvg := false
else if sS == 1
sSw := math.max(sSw, high)
if bar_index - sSwBar > maxToMss
sS := 0
else if close < sMss
sS := 2
sMssBar := bar_index
sLeg := low
sDisp := bodyDnMax >= atr * dispMult
if showMss
line.new(sMssFrom, sMss, bar_index, sMss, color = bearCol, style = line.style_dashed)
label.new(bar_index, sMss, "MSS", style = label.style_none, textcolor = bearCol, size = size.tiny)
else if sS == 2
sLeg := math.min(sLeg, low)
if sS == 2 and not sFvg and beBar >= sSwBar
sFvg := true
sZT := beTop
sZB := beBot
sZBar := beBar
if showFvg
box.new(beBar - 2, beTop, beBar + 10, beBot, border_color = color.new(bearCol, 60), bgcolor = color.new(bearCol, 85))
if sS == 2 and bar_index > sMssBar
if close > sSw or bar_index - sMssBar > maxToEntry or (sFvg and close > sZT)
sS := 0
else
zb = sFvg ? sZB : sMss
canEnter = (sFvg and bar_index > sZBar) or (not sFvg and not needFvg)
if canEnter and high >= zb
prem = close >= (sSw + sLeg) / 2
sellScore := (sDisp ? 1 : 0) + (sFvg ? 1 : 0) + (kzOk ? 1 : 0) + (biasBear ? 1 : 0) + (prem ? 1 : 0)
sellTip := "Sweep ✓\nMSS ✓\nDisplacement " + tick(sDisp) + "\nFVG " + tick(sFvg) + "\nKillzone " + tick(kzOk) + "\nHTF bias " + tick(biasBear) + "\nPremium " + tick(prem)
sellSig := sellScore >= minScore
sS := 0
// ───────────────────────────── Signals & trades ─────────────────────────────
if buySig
label.new(bar_index, low, "BUY " + grade(buyScore) + "\n" + str.tostring(buyScore) + "/5", yloc = yloc.belowbar, style = label.style_label_up, color = bullCol, textcolor = color.white, size = size.small, tooltip = buyTip)
if tDir == 0
tDir := 1
tBar := bar_index
tEn := close
tSl := bSw - atr * slBuf
risk = tEn - tSl
tTp := tEn + risk * rrIn
if tpMode == "Opposite liquidity" and not extHiTaken and not na(extHi) and extHi - tEn >= risk * rrIn
tTp := extHi
if showLevels
lnEn := line.new(bar_index, tEn, bar_index + 1, tEn, color = color.gray)
lnSl := line.new(bar_index, tSl, bar_index + 1, tSl, color = bearCol)
lnTp := line.new(bar_index, tTp, bar_index + 1, tTp, color = bullCol)
if sellSig
label.new(bar_index, high, "SELL " + grade(sellScore) + "\n" + str.tostring(sellScore) + "/5", yloc = yloc.abovebar, style = label.style_label_down, color = bearCol, textcolor = color.white, size = size.small, tooltip = sellTip)
if tDir == 0
tDir := -1
tBar := bar_index
tEn := close
tSl := sSw + atr * slBuf
risk = tSl - tEn
tTp := tEn - risk * rrIn
if tpMode == "Opposite liquidity" and not extLoTaken and not na(extLo) and tEn - extLo >= risk * rrIn
tTp := extLo
if showLevels
lnEn := line.new(bar_index, tEn, bar_index + 1, tEn, color = color.gray)
lnSl := line.new(bar_index, tSl, bar_index + 1, tSl, color = bearCol)
lnTp := line.new(bar_index, tTp, bar_index + 1, tTp, color = bullCol)
// ───────────────────────────── Stats table ─────────────────────────────
var table tb = table.new(position.top_right, 2, 6, bgcolor = color.new(color.black, 20), border_width = 1)
if showTable and barstate.islast
total = wins + losses
wr = total > 0 ? 100.0 * wins / total : 0.0
table.cell(tb, 0, 0, "ICT Signals", text_color = color.white, text_size = size.small)
table.cell(tb, 1, 0, "min " + str.tostring(minScore) + "/5", text_color = color.silver, text_size = size.small)
table.cell(tb, 0, 1, "Closed trades", text_color = color.silver, text_size = size.small)
table.cell(tb, 1, 1, str.tostring(total), text_color = color.white, text_size = size.small)
table.cell(tb, 0, 2, "Wins / Losses", text_color = color.silver, text_size = size.small)
table.cell(tb, 1, 2, str.tostring(wins) + " / " + str.tostring(losses), text_color = color.white, text_size = size.small)
table.cell(tb, 0, 3, "Win rate", text_color = color.silver, text_size = size.small)
table.cell(tb, 1, 3, str.tostring(wr, "#.0") + "%", text_color = wr >= 50 ? bullCol : color.white, text_size = size.small)
table.cell(tb, 0, 4, "Net R", text_color = color.silver, text_size = size.small)
table.cell(tb, 1, 4, str.tostring(sumR, "#.##"), text_color = sumR >= 0 ? bullCol : bearCol, text_size = size.small)
table.cell(tb, 0, 5, "HTF bias", text_color = color.silver, text_size = size.small)
table.cell(tb, 1, 5, biasMode == "Off" ? "off" : biasBull ? "bullish" : biasBear ? "bearish" : "neutral", text_color = biasBull and biasMode != "Off" ? bullCol : biasBear and biasMode != "Off" ? bearCol : color.white, text_size = size.small)
// ───────────────────────────── Alerts ─────────────────────────────
alertcondition(buySig, "ICT Buy", "ICT BUY {{ticker}} {{interval}} @ {{close}}")
alertcondition(sellSig, "ICT Sell", "ICT SELL {{ticker}} {{interval}} @ {{close}}")
if buySig
alert("ICT BUY " + grade(buyScore) + " " + syminfo.ticker + " @ " + str.tostring(close, format.mintick) + " | SL " + str.tostring(bSw - atr * slBuf, format.mintick), alert.freq_once_per_bar_close)
if sellSig
alert("ICT SELL " + grade(sellScore) + " " + syminfo.ticker + " @ " + str.tostring(close, format.mintick) + " | SL " + str.tostring(sSw + atr * slBuf, format.mintick), alert.freq_once_per_bar_close)