本页源码来自 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 HTF Liquidity Reversal — built for 1H / 4H / Daily charts
//
// Model: Liquidity sweep → Change in State of Delivery (CISD) → entry,
// traded WITH the higher-timeframe trend.
//
// 1. Sweep : price runs a liquidity pool — previous week / day high-low,
// a swing high-low, or an N-bar extreme (Turtle Soup).
// 2. CISD : a candle closes back through the OPEN of the last run of
// opposite candles that drove into the sweep, AND back inside
// the swept level. Sweeps may take several HTF bars to form.
// 3. Entry : on the CISD close (default) or on a retrace into the FVG.
// Grade 0-5 : trend aligned · major pool (PWH/PWL/PDH/PDL) · displacement ·
// FVG present · discount (buys) / premium (sells).
//
// Research behind the components (ICT itself has no peer-reviewed studies):
// • Osler (2003, Journal of Finance): stop-loss orders cluster just beyond
// highs/lows and round numbers; take-profits cluster at them → price
// often runs those stops then reverses. Basis for "liquidity sweeps".
// • Connors & Raschke (1995, Street Smarts): "Turtle Soup" — fading false
// breaks of a 20-bar extreme made ≥ 4 bars earlier.
// • Moskowitz, Ooi & Pedersen (2012, J. Financial Economics): time-series
// momentum — trading with the higher-timeframe trend. Basis for the
// trend filter: sweeps are taken as pullbacks inside the trend.
//
// Non-repainting: confirmed pivots, prior closed HTF bars, closed-bar signals.
// The stats table shows win rate per grade on YOUR chart — use it to pick the
// minimum grade. No indicator guarantees a win rate; forward-test first.
//@version=6
indicator("ICT HTF Liquidity Reversal", "ICT HTF", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// ───────────────────────────── Inputs ─────────────────────────────
const string G1 = "Liquidity pools"
useWeek = input.bool(true, "Previous week high/low", group = G1)
useDay = input.bool(true, "Previous day high/low (intraday charts)", group = G1)
useSwing = input.bool(true, "Swing highs/lows", group = G1)
swingLen = input.int(5, "Swing length", minval = 2, group = G1)
useTS = input.bool(true, "N-bar extreme (Turtle Soup)", group = G1)
tsLen = input.int(20, "N-bar extreme length", minval = 5, group = G1)
maxToConf = input.int(12, "Max bars: sweep → CISD", minval = 1, group = G1)
const string G2 = "Confirmation & entry"
entryMode = input.string("Confirmation close", "Entry", options = ["Confirmation close", "FVG retrace"], group = G2, tooltip = "Confirmation close: more fills, the usual choice on 4H/Daily. FVG retrace: better price, but some moves leave without you.")
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)
maxToEntry = input.int(10, "Max bars: CISD → FVG entry", minval = 1, group = G2)
const string G3 = "Filters"
trendMode = input.string("Auto", "Trend timeframe", options = ["Auto", "D", "W", "M"], group = G3, tooltip = "Auto: up to 1H → Daily, up to 4H → Weekly, above → Monthly.")
emaLen = input.int(20, "Trend EMA length (on trend timeframe)", minval = 2, group = G3)
needTrend = input.bool(true, "Only trade with the trend", group = G3)
rangeLen = input.int(50, "Dealing range length (premium/discount)", minval = 10, group = G3)
minScore = input.int(3, "Minimum grade to signal (0-5)", minval = 0, maxval = 5, group = G3, tooltip = "5 = A+, 4 = A, 3 = B. Check the per-grade win rates in the table, then raise this.")
const string G4 = "Risk"
tpMode = input.string("Fixed R", "Target", options = ["Fixed R", "Opposite liquidity"], group = G4, tooltip = "Opposite liquidity: nearest untaken swing / previous day / previous week level that 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"
showLiq = input.bool(true, "Previous week/day levels", group = G5)
showSweep = input.bool(true, "Sweep marks", group = G5)
showCisd = input.bool(true, "CISD lines", group = G5)
showFvg = input.bool(true, "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)
// ───────────────────────────── Helpers ─────────────────────────────
tick(bool b) => b ? "✓" : "✗"
grade(int s) => s == 5 ? "A+" : s == 4 ? "A" : s == 3 ? "B" : "C"
score(bool a, bool b, bool c, bool d, bool e) => (a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) + (d ? 1 : 0) + (e ? 1 : 0)
// CISD level for a bullish reversal: open of the earliest candle in the run of
// bearish candles that ends at the current (low) bar.
cisdUp() =>
float lvl = na
for i = 0 to 10
if close[i] < open[i]
lvl := open[i]
else if i > 0
break
na(lvl) ? high : lvl
cisdDn() =>
float lvl = na
for i = 0 to 10
if close[i] > open[i]
lvl := open[i]
else if i > 0
break
na(lvl) ? low : lvl
// ───────────────────────────── Core series ─────────────────────────────
atr = ta.atr(14)
bodyUpMax = ta.highest(close - open, 3)
bodyDnMax = ta.highest(open - close, 3)
rngMid = (ta.highest(high, rangeLen) + ta.lowest(low, rangeLen)) / 2
cUp = cisdUp()
cDn = cisdDn()
// Higher-timeframe trend (prior closed HTF bars only)
tfSec = timeframe.in_seconds()
trendTf = trendMode != "Auto" ? trendMode : tfSec <= 3600 ? "D" : tfSec <= 14400 ? "W" : "M"
trendData() =>
e = ta.ema(close, emaLen)
[close[1], e[1], e[2]]
[tC, tE1, tE2] = request.security(syminfo.tickerid, trendTf, trendData(), lookahead = barmerge.lookahead_on)
upTrend = tC > tE1 and tE1 > tE2
dnTrend = tC < tE1 and tE1 < tE2
// Previous week / day liquidity
[pwh, pwl] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead = barmerge.lookahead_on)
[pdh, pdl] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead = barmerge.lookahead_on)
weekOk = useWeek and tfSec < 604800
dayOk = useDay and tfSec < 86400
var bool pwhT = false
var bool pwlT = false
var bool pdhT = false
var bool pdlT = false
if timeframe.change("W")
pwhT := false
pwlT := false
if timeframe.change("D")
pdhT := false
pdlT := false
plot(showLiq and weekOk ? pwh : na, "Prev. week high", color.new(color.orange, 20), 2, plot.style_linebr, force_overlay = true)
plot(showLiq and weekOk ? pwl : na, "Prev. week low", color.new(color.orange, 20), 2, plot.style_linebr, force_overlay = true)
plot(showLiq and dayOk ? pdh : na, "Prev. day high", color.new(color.gray, 30), 1, plot.style_linebr, force_overlay = true)
plot(showLiq and dayOk ? pdl : na, "Prev. day low", color.new(color.gray, 30), 1, plot.style_linebr, force_overlay = true)
// Swings
ph = ta.pivothigh(high, swingLen, swingLen)
pl = ta.pivotlow(low, swingLen, swingLen)
var float swHi = na
var float swLo = na
var bool swHiT = true
var bool swLoT = true
if not na(ph)
swHi := ph
swHiT := false
if not na(pl)
swLo := pl
swLoT := false
// Turtle Soup: new N-bar extreme where the prior extreme is ≥ 4 bars old
llPrev = ta.lowest(low, tsLen)[1]
hhPrev = ta.highest(high, tsLen)[1]
lbOff = ta.lowestbars(low, tsLen)
hbOff = ta.highestbars(high, tsLen)
tsBull = useTS and low < llPrev and -lbOff[1] >= 3
tsBear = useTS and high > hhPrev and -hbOff[1] >= 3
// Liquidity taken this bar
takePWL = weekOk and not pwlT and low < pwl
takePDL = dayOk and not pdlT and low < pdl
takeSL = useSwing and not swLoT and low < swLo
bullTake = takePWL or takePDL or takeSL or tsBull
bullMajor = takePWL or takePDL
bullType = takePWL ? "Prev. week low" : takePDL ? "Prev. day low" : takeSL ? "Swing low" : "N-bar low"
float bullLv = na
if tsBull
bullLv := llPrev
if takeSL
bullLv := na(bullLv) ? swLo : math.min(bullLv, swLo)
if takePDL
bullLv := na(bullLv) ? pdl : math.min(bullLv, pdl)
if takePWL
bullLv := na(bullLv) ? pwl : math.min(bullLv, pwl)
takePWH = weekOk and not pwhT and high > pwh
takePDH = dayOk and not pdhT and high > pdh
takeSH = useSwing and not swHiT and high > swHi
bearTake = takePWH or takePDH or takeSH or tsBear
bearMajor = takePWH or takePDH
bearType = takePWH ? "Prev. week high" : takePDH ? "Prev. day high" : takeSH ? "Swing high" : "N-bar high"
float bearLv = na
if tsBear
bearLv := hhPrev
if takeSH
bearLv := na(bearLv) ? swHi : math.max(bearLv, swHi)
if takePDH
bearLv := na(bearLv) ? pdh : math.max(bearLv, pdh)
if takePWH
bearLv := na(bearLv) ? pwh : math.max(bearLv, pwh)
if weekOk and low < pwl
pwlT := true
if weekOk and high > pwh
pwhT := true
if dayOk and low < pdl
pdlT := true
if dayOk and high > pdh
pdhT := true
if low < swLo
swLoT := true
if high > swHi
swHiT := true
plotshape(showSweep and bullTake, "Sell-side liquidity taken", shape.xcross, location.belowbar, color.new(bullCol, 40), size = size.tiny, force_overlay = true)
plotshape(showSweep and bearTake, "Buy-side liquidity taken", shape.xcross, location.abovebar, color.new(bearCol, 40), size = size.tiny, force_overlay = true)
// 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 int tScore = 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 winR = 0.0
var array<int> gW = array.new_int(6, 0)
var array<int> gL = array.new_int(6, 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
array.set(gL, tScore, array.get(gL, tScore) + 1)
tDir := 0
else if hitTp
wins += 1
winR += math.abs(tTp - tEn) / math.abs(tEn - tSl)
array.set(gW, tScore, array.get(gW, tScore) + 1)
tDir := 0
// ───────────────────────────── Bullish setup ─────────────────────────────
var int bS = 0
var float bLow = na
var float bLvl = na
var float bCisd = na
var float bZT = na
var float bZB = na
var int bLowBar = na
var int bConfBar = na
var int bZBar = na
var bool bMajor = false
var bool bDisp = false
var bool bFvg = false
var string bType = ""
buySig = false
int buyScore = 0
string buyTip = ""
if bS == 1
if low < bLow
bLow := low
bLowBar := bar_index
bCisd := cUp
if bullTake
bLvl := math.min(bLvl, bullLv)
if bullMajor and not bMajor
bMajor := true
bType := bullType
else if bullTake and (bS == 0 or low < bLow)
bS := 1
bLow := low
bLowBar := bar_index
bLvl := bullLv
bCisd := cUp
bMajor := bullMajor
bType := bullType
bFvg := false
if bS == 1
if bar_index - bLowBar > maxToConf
bS := 0
else if close > bCisd and close > bLvl
bConfBar := bar_index
bDisp := bodyUpMax >= atr * dispMult
bFvg := buBar >= bLowBar + 2
if bFvg
bZT := buTop
bZB := buBot
bZBar := buBar
if showFvg
box.new(buBar - 2, buTop, buBar + 8, buBot, border_color = color.new(bullCol, 60), bgcolor = color.new(bullCol, 85), force_overlay = true)
if showCisd
line.new(bLowBar, bCisd, bar_index, bCisd, color = bullCol, style = line.style_dotted, force_overlay = true)
label.new(bar_index, bCisd, "CISD", style = label.style_none, textcolor = bullCol, size = size.tiny, force_overlay = true)
if entryMode == "Confirmation close"
disc = close <= rngMid
buyScore := score(upTrend, bMajor, bDisp, bFvg, disc)
buyTip := "Liquidity: " + bType + "\nCISD ✓\nTrend " + tick(upTrend) + "\nMajor pool " + tick(bMajor) + "\nDisplacement " + tick(bDisp) + "\nFVG " + tick(bFvg) + "\nDiscount " + tick(disc)
buySig := buyScore >= minScore and (not needTrend or upTrend)
bS := 0
else
bS := 2
else if bS == 2 and bar_index > bConfBar
if not bFvg and buBar >= bLowBar + 2
bFvg := true
bZT := buTop
bZB := buBot
bZBar := buBar
if showFvg
box.new(buBar - 2, buTop, buBar + 8, buBot, border_color = color.new(bullCol, 60), bgcolor = color.new(bullCol, 85), force_overlay = true)
if close < bLow or bar_index - bConfBar > maxToEntry or (bFvg and close < bZB)
bS := 0
else if bFvg and bar_index > bZBar and low <= bZT
disc = close <= rngMid
buyScore := score(upTrend, bMajor, bDisp, bFvg, disc)
buyTip := "Liquidity: " + bType + "\nCISD ✓\nTrend " + tick(upTrend) + "\nMajor pool " + tick(bMajor) + "\nDisplacement " + tick(bDisp) + "\nFVG " + tick(bFvg) + "\nDiscount " + tick(disc)
buySig := buyScore >= minScore and (not needTrend or upTrend)
bS := 0
// ───────────────────────────── Bearish setup ─────────────────────────────
var int sS = 0
var float sHigh = na
var float sLvl = na
var float sCisd = na
var float sZT = na
var float sZB = na
var int sHighBar = na
var int sConfBar = na
var int sZBar = na
var bool sMajor = false
var bool sDisp = false
var bool sFvg = false
var string sType = ""
sellSig = false
int sellScore = 0
string sellTip = ""
if sS == 1
if high > sHigh
sHigh := high
sHighBar := bar_index
sCisd := cDn
if bearTake
sLvl := math.max(sLvl, bearLv)
if bearMajor and not sMajor
sMajor := true
sType := bearType
else if bearTake and (sS == 0 or high > sHigh)
sS := 1
sHigh := high
sHighBar := bar_index
sLvl := bearLv
sCisd := cDn
sMajor := bearMajor
sType := bearType
sFvg := false
if sS == 1
if bar_index - sHighBar > maxToConf
sS := 0
else if close < sCisd and close < sLvl
sConfBar := bar_index
sDisp := bodyDnMax >= atr * dispMult
sFvg := beBar >= sHighBar + 2
if sFvg
sZT := beTop
sZB := beBot
sZBar := beBar
if showFvg
box.new(beBar - 2, beTop, beBar + 8, beBot, border_color = color.new(bearCol, 60), bgcolor = color.new(bearCol, 85), force_overlay = true)
if showCisd
line.new(sHighBar, sCisd, bar_index, sCisd, color = bearCol, style = line.style_dotted, force_overlay = true)
label.new(bar_index, sCisd, "CISD", style = label.style_none, textcolor = bearCol, size = size.tiny, force_overlay = true)
if entryMode == "Confirmation close"
prem = close >= rngMid
sellScore := score(dnTrend, sMajor, sDisp, sFvg, prem)
sellTip := "Liquidity: " + sType + "\nCISD ✓\nTrend " + tick(dnTrend) + "\nMajor pool " + tick(sMajor) + "\nDisplacement " + tick(sDisp) + "\nFVG " + tick(sFvg) + "\nPremium " + tick(prem)
sellSig := sellScore >= minScore and (not needTrend or dnTrend)
sS := 0
else
sS := 2
else if sS == 2 and bar_index > sConfBar
if not sFvg and beBar >= sHighBar + 2
sFvg := true
sZT := beTop
sZB := beBot
sZBar := beBar
if showFvg
box.new(beBar - 2, beTop, beBar + 8, beBot, border_color = color.new(bearCol, 60), bgcolor = color.new(bearCol, 85), force_overlay = true)
if close > sHigh or bar_index - sConfBar > maxToEntry or (sFvg and close > sZT)
sS := 0
else if sFvg and bar_index > sZBar and high >= sZB
prem = close >= rngMid
sellScore := score(dnTrend, sMajor, sDisp, sFvg, prem)
sellTip := "Liquidity: " + sType + "\nCISD ✓\nTrend " + tick(dnTrend) + "\nMajor pool " + tick(sMajor) + "\nDisplacement " + tick(sDisp) + "\nFVG " + tick(sFvg) + "\nPremium " + tick(prem)
sellSig := sellScore >= minScore and (not needTrend or dnTrend)
sS := 0
// ───────────────────────────── Signals & trades ─────────────────────────────
if buySig
label.new(bar_index, low, "BUY " + grade(buyScore), yloc = yloc.belowbar, style = label.style_label_up, color = bullCol, textcolor = color.white, size = size.small, tooltip = buyTip, force_overlay = true)
if tDir == 0
tDir := 1
tScore := buyScore
tBar := bar_index
tEn := close
tSl := bLow - atr * slBuf
risk = tEn - tSl
minT = tEn + risk * rrIn
float tgt = na
if tpMode == "Opposite liquidity"
if not swHiT and swHi >= minT
tgt := swHi
if dayOk and not pdhT and pdh >= minT
tgt := na(tgt) ? pdh : math.min(tgt, pdh)
if weekOk and not pwhT and pwh >= minT
tgt := na(tgt) ? pwh : math.min(tgt, pwh)
tTp := na(tgt) ? minT : tgt
if showLevels
lnEn := line.new(bar_index, tEn, bar_index + 1, tEn, color = color.gray, force_overlay = true)
lnSl := line.new(bar_index, tSl, bar_index + 1, tSl, color = bearCol, force_overlay = true)
lnTp := line.new(bar_index, tTp, bar_index + 1, tTp, color = bullCol, force_overlay = true)
if sellSig
label.new(bar_index, high, "SELL " + grade(sellScore), yloc = yloc.abovebar, style = label.style_label_down, color = bearCol, textcolor = color.white, size = size.small, tooltip = sellTip, force_overlay = true)
if tDir == 0
tDir := -1
tScore := sellScore
tBar := bar_index
tEn := close
tSl := sHigh + atr * slBuf
risk = tSl - tEn
minT = tEn - risk * rrIn
float tgt = na
if tpMode == "Opposite liquidity"
if not swLoT and swLo <= minT
tgt := swLo
if dayOk and not pdlT and pdl <= minT
tgt := na(tgt) ? pdl : math.max(tgt, pdl)
if weekOk and not pwlT and pwl <= minT
tgt := na(tgt) ? pwl : math.max(tgt, pwl)
tTp := na(tgt) ? minT : tgt
if showLevels
lnEn := line.new(bar_index, tEn, bar_index + 1, tEn, color = color.gray, force_overlay = true)
lnSl := line.new(bar_index, tSl, bar_index + 1, tSl, color = bearCol, force_overlay = true)
lnTp := line.new(bar_index, tTp, bar_index + 1, tTp, color = bullCol, force_overlay = true)
// ───────────────────────────── Stats table ─────────────────────────────
gradeCell(int s) =>
w = array.get(gW, s)
l = array.get(gL, s)
t = w + l
t > 0 ? str.tostring(w) + "W / " + str.tostring(l) + "L · " + str.tostring(100.0 * w / t, "#") + "%" : "—"
var table tb = table.new(position.top_right, 2, 9, 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
netR = winR - losses
pf = losses > 0 ? winR / losses : na
txt = color.white
dim = color.silver
table.cell(tb, 0, 0, "ICT HTF", text_color = txt, text_size = size.small)
table.cell(tb, 1, 0, "min grade " + grade(minScore), text_color = dim, text_size = size.small)
table.cell(tb, 0, 1, trendTf + " trend", text_color = dim, text_size = size.small)
table.cell(tb, 1, 1, upTrend ? "up" : dnTrend ? "down" : "flat", text_color = upTrend ? bullCol : dnTrend ? bearCol : txt, text_size = size.small)
table.cell(tb, 0, 2, "Closed trades", text_color = dim, text_size = size.small)
table.cell(tb, 1, 2, str.tostring(total), text_color = txt, text_size = size.small)
table.cell(tb, 0, 3, "Win rate", text_color = dim, text_size = size.small)
table.cell(tb, 1, 3, str.tostring(wr, "#.0") + "%", text_color = txt, text_size = size.small)
table.cell(tb, 0, 4, "Net R", text_color = dim, text_size = size.small)
table.cell(tb, 1, 4, str.tostring(netR, "#.##"), text_color = netR >= 0 ? bullCol : bearCol, text_size = size.small)
table.cell(tb, 0, 5, "Profit factor", text_color = dim, text_size = size.small)
table.cell(tb, 1, 5, na(pf) ? "—" : str.tostring(pf, "#.##"), text_color = txt, text_size = size.small)
table.cell(tb, 0, 6, "A+ (5/5)", text_color = dim, text_size = size.small)
table.cell(tb, 1, 6, gradeCell(5), text_color = txt, text_size = size.small)
table.cell(tb, 0, 7, "A (4/5)", text_color = dim, text_size = size.small)
table.cell(tb, 1, 7, gradeCell(4), text_color = txt, text_size = size.small)
table.cell(tb, 0, 8, "B (3/5)", text_color = dim, text_size = size.small)
table.cell(tb, 1, 8, gradeCell(3), text_color = txt, text_size = size.small)
// ───────────────────────────── Alerts ─────────────────────────────
alertcondition(buySig, "ICT HTF Buy", "ICT HTF BUY {{ticker}} {{interval}} @ {{close}}")
alertcondition(sellSig, "ICT HTF Sell", "ICT HTF SELL {{ticker}} {{interval}} @ {{close}}")
if buySig
alert("ICT HTF BUY " + grade(buyScore) + " " + syminfo.ticker + " @ " + str.tostring(close, format.mintick) + " | SL " + str.tostring(bLow - atr * slBuf, format.mintick), alert.freq_once_per_bar_close)
if sellSig
alert("ICT HTF SELL " + grade(sellScore) + " " + syminfo.ticker + " @ " + str.tostring(close, format.mintick) + " | SL " + str.tostring(sHigh + atr * slBuf, format.mintick), alert.freq_once_per_bar_close)