本页源码来自 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 — STRATEGY (A and A+ setups only)
//
// Same model as the indicator: liquidity sweep → CISD → entry, with the
// higher-timeframe trend. Only setups grading A (4/5) or A+ (5/5) are traded.
// Orders fill at the NEXT bar's open; stop beyond the sweep, target in R.
// Position size = fixed % of equity risked per trade.
// Results are a backtest, not a promise. Check them with realistic
// commission/slippage for your broker and forward-test before going live.
//@version=6
strategy("ICT HTF Liquidity Reversal Strategy", "ICT HTF Strat", overlay = true, initial_capital = 10000, pyramiding = 0, commission_type = strategy.commission.percent, commission_value = 0.02, slippage = 2, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// ───────────────────────────── Inputs ─────────────────────────────
const string G0 = "Backtest"
startDate = input.time(timestamp("2018-01-01T00:00:00"), "Start date", group = G0)
riskPct = input.float(1.0, "Risk per trade (% of equity)", minval = 0.1, maxval = 10, step = 0.1, group = G0)
minScore = input.int(4, "Minimum grade (4 = A, 5 = A+ only)", minval = 4, maxval = 5, group = G0)
allowLong = input.bool(true, "Longs", group = G0)
allowShort = input.bool(true, "Shorts", group = G0)
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)
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)
const string G4 = "Risk"
tpMode = input.string("Fixed R", "Target", options = ["Fixed R", "Opposite liquidity"], group = G4)
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)
showCisd = input.bool(true, "CISD lines", group = G5)
showFvg = input.bool(true, "FVGs", group = G5)
showTable = input.bool(true, "Results 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)
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()
inDate = time >= startDate
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
[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)
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
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
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
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
// ───────────────────────────── 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)
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)
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
// ───────────────────────────── Orders ─────────────────────────────
var float curSl = na
var float curTp = na
flat = strategy.position_size == 0
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 flat and allowLong and inDate
sl = bLow - atr * slBuf
risk = close - sl
minT = close + 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)
tp = na(tgt) ? minT : tgt
qty = risk > 0 ? strategy.equity * riskPct / 100 / (risk * syminfo.pointvalue) : 0.0
if qty > 0
strategy.entry("Long", strategy.long, qty = qty, comment = "BUY " + grade(buyScore))
strategy.exit("Long exit", "Long", stop = sl, limit = tp, comment_loss = "SL", comment_profit = "TP")
curSl := sl
curTp := tp
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 flat and allowShort and inDate and not buySig
sl = sHigh + atr * slBuf
risk = sl - close
minT = close - 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)
tp = na(tgt) ? minT : tgt
qty = risk > 0 ? strategy.equity * riskPct / 100 / (risk * syminfo.pointvalue) : 0.0
if qty > 0
strategy.entry("Short", strategy.short, qty = qty, comment = "SELL " + grade(sellScore))
strategy.exit("Short exit", "Short", stop = sl, limit = tp, comment_loss = "SL", comment_profit = "TP")
curSl := sl
curTp := tp
inPos = strategy.position_size != 0
plot(inPos ? curSl : na, "Stop", bearCol, 1, plot.style_linebr, force_overlay = true)
plot(inPos ? curTp : na, "Target", bullCol, 1, plot.style_linebr, force_overlay = true)
// ───────────────────────────── Results 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 = strategy.closedtrades
wr = total > 0 ? 100.0 * strategy.wintrades / total : 0.0
pf = strategy.grossloss > 0 ? strategy.grossprofit / strategy.grossloss : na
txt = color.white
dim = color.silver
table.cell(tb, 0, 0, "ICT HTF Strategy", text_color = txt, text_size = size.small)
table.cell(tb, 1, 0, minScore == 5 ? "A+ only" : "A and A+", text_color = dim, text_size = size.small)
table.cell(tb, 0, 1, "Closed trades", text_color = dim, text_size = size.small)
table.cell(tb, 1, 1, str.tostring(total), text_color = txt, text_size = size.small)
table.cell(tb, 0, 2, "Win rate", text_color = dim, text_size = size.small)
table.cell(tb, 1, 2, str.tostring(wr, "#.0") + "%", text_color = txt, text_size = size.small)
table.cell(tb, 0, 3, "Profit factor", text_color = dim, text_size = size.small)
table.cell(tb, 1, 3, na(pf) ? "—" : str.tostring(pf, "#.##"), text_color = txt, text_size = size.small)
table.cell(tb, 0, 4, "Net profit", text_color = dim, text_size = size.small)
table.cell(tb, 1, 4, str.tostring(strategy.netprofit, "#,###.##") + " " + strategy.account_currency, text_color = strategy.netprofit >= 0 ? bullCol : bearCol, text_size = size.small)
table.cell(tb, 0, 5, "Max drawdown", text_color = dim, text_size = size.small)
table.cell(tb, 1, 5, str.tostring(strategy.max_drawdown, "#,###.##"), text_color = txt, text_size = size.small)