ICT Silver Bullet | Liquidity Sweep + FVG Strategy

blitz_locked · strategy · 197 行 · 点赞 152 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
strategy("ICT Silver Bullet", shorttitle="ICT SB", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=25, initial_capital=100000, process_orders_on_close=true, max_boxes_count=100, max_lines_count=100, max_labels_count=100)
 
// Inputs
const string G_T = "Trading"
pivLen   = input.int(3, "Swing length", minval=1, group=G_T)
expiry   = input.int(30, "Sweep valid for (bars)", minval=1, group=G_T)
rr       = input.float(2.0, "Reward : risk", minval=0.5, step=0.25, group=G_T)
bufTicks = input.int(2, "Stop buffer (ticks)", minval=0, group=G_T)
 
const string G_D = "Display"
showSess   = input.bool(true, "Shade sessions", group=G_D)
showLevels = input.bool(true, "Liquidity levels", group=G_D)
showSweeps = input.bool(true, "Sweep markers", group=G_D)
showRange  = input.bool(true, "Premium / discount zones", group=G_D)
showFvg    = input.bool(true, "Setup FVG boxes", group=G_D)
showTrade  = input.bool(true, "Trade levels", group=G_D)
showPanel  = input.bool(true, "Info panel", group=G_D)
colCandles = input.bool(false, "Colour candles by zone", group=G_D)
 
const string G_C = "Style"
colPrem   = input.color(#ff1f8f, "Premium", group=G_C)
colDisc   = input.color(#00e5ff, "Discount", group=G_C)
colHi     = input.color(#ffd23f, "Highlight", group=G_C)
colBull   = input.color(#089981, "Bullish", group=G_C)
colBear   = input.color(#f23645, "Bearish", group=G_C)
colLondon = input.color(#5b9cf6, "London session", group=G_C)
colAM     = input.color(#a78bfa, "New York AM session", group=G_C)
colPM     = input.color(#f0b45b, "New York PM session", group=G_C)
 
// Sessions (New York): 03:00-04:00, 10:00-11:00, 14:00-15:00
h      = hour(time, "America/New_York")
inSess = h == 3 or h == 10 or h == 14
 
string sessName = h == 3 ? "London" : h == 10 ? "NY AM" : h == 14 ? "NY PM" : "Closed"
color  sessCol  = not showSess ? na : h == 3 ? color.new(colLondon, 92) : h == 10 ? color.new(colAM, 92) : h == 14 ? color.new(colPM, 92) : na
bgcolor(sessCol, title="Session")
 
// Liquidity levels
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
 
var float lastPH    = na
var float lastPL    = na
var int   lastPHBar = na
var int   lastPLBar = na
 
if not na(ph)
    lastPH := ph
    lastPHBar := bar_index - pivLen
if not na(pl)
    lastPL := pl
    lastPLBar := bar_index - pivLen
 
plot(showLevels ? lastPH : na, "Buy-side liquidity", color.new(colBear, 45), 1, plot.style_steplinebr)
plot(showLevels ? lastPL : na, "Sell-side liquidity", color.new(colBull, 45), 1, plot.style_steplinebr)
 
// Dealing range: premium / discount
var float rTop = na
var float rBot = na
 
if not na(ph)
    rTop := ph
    if not na(rBot) and rBot >= rTop
        rBot := ta.lowest(low, pivLen + 1)
if not na(pl)
    rBot := pl
    if not na(rTop) and rTop <= rBot
        rTop := ta.highest(high, pivLen + 1)
if not na(rTop) and high > rTop
    rTop := high
if not na(rBot) and low < rBot
    rBot := low
 
hasRange = not na(rTop) and not na(rBot) and rTop > rBot
eq       = hasRange ? (rTop + rBot) / 2 : na
deepP    = hasRange ? eq + (rTop - eq) * 0.75 : na
deepD    = hasRange ? eq - (eq - rBot) * 0.75 : na
rngPct   = hasRange ? (close - rBot) / (rTop - rBot) * 100 : na
 
pTop   = plot(showRange and hasRange ? rTop : na, "Range high", color.new(colPrem, 70), 1, plot.style_steplinebr)
pBot   = plot(showRange and hasRange ? rBot : na, "Range low", color.new(colDisc, 70), 1, plot.style_steplinebr)
pEq    = plot(showRange and hasRange ? eq : na, "Equilibrium", color.new(chart.fg_color, 35), 2, plot.style_steplinebr)
pDeepP = plot(showRange and hasRange ? deepP : na, "Deep premium", color.new(colPrem, 100), 1, plot.style_steplinebr, display=display.none)
pDeepD = plot(showRange and hasRange ? deepD : na, "Deep discount", color.new(colDisc, 100), 1, plot.style_steplinebr, display=display.none)
 
fill(pTop, pEq, color.new(colPrem, 94), "Premium")
fill(pEq, pBot, color.new(colDisc, 94), "Discount")
fill(pTop, pDeepP, color.new(colPrem, 86), "Deep premium")
fill(pDeepD, pBot, color.new(colDisc, 86), "Deep discount")
 
barcolor(colCandles and hasRange ? (close >= eq ? colPrem : colDisc) : na, title="Zone candles")
 
// Sweep and bias
var int   bias     = 0
var int   biasBar  = 0
var float sweepExt = na
 
sweptHigh = not na(lastPH) and high > lastPH
sweptLow  = not na(lastPL) and low < lastPL
 
float lvlHigh = lastPH
float lvlLow  = lastPL
 
if showSweeps and sweptHigh and not sweptLow
    line.new(lastPHBar, lvlHigh, bar_index, lvlHigh, color=color.new(colBear, 30), style=line.style_dotted, width=1)
    label.new(bar_index, high, "BSL", style=label.style_label_down, color=color.new(colBear, 100), textcolor=colBear, size=size.tiny, tooltip="Buy-side liquidity swept at " + str.tostring(lvlHigh, format.mintick))
if showSweeps and sweptLow and not sweptHigh
    line.new(lastPLBar, lvlLow, bar_index, lvlLow, color=color.new(colBull, 30), style=line.style_dotted, width=1)
    label.new(bar_index, low, "SSL", style=label.style_label_up, color=color.new(colBull, 100), textcolor=colBull, size=size.tiny, tooltip="Sell-side liquidity swept at " + str.tostring(lvlLow, format.mintick))
 
if sweptHigh and not sweptLow
    bias := -1
    biasBar := bar_index
    sweepExt := high
    lastPH := na
else if sweptLow and not sweptHigh
    bias := 1
    biasBar := bar_index
    sweepExt := low
    lastPL := na
else if bias == -1
    sweepExt := math.max(sweepExt, high)
else if bias == 1
    sweepExt := math.min(sweepExt, low)
 
if bias != 0 and bar_index - biasBar > expiry
    bias := 0
 
// Fair value gap
bullFvg = low > high[2]
bearFvg = high < low[2]
 
buf = bufTicks * syminfo.mintick
 
if showFvg and inSess and bar_index > biasBar and ((bias == 1 and bullFvg) or (bias == -1 and bearFvg))
    bool  isBull = bias == 1
    color c      = isBull ? colBull : colBear
    box.new(bar_index - 2, isBull ? low : low[2], bar_index + 12, isBull ? high[2] : high, border_color=color.new(c, 40), bgcolor=color.new(c, 88))
 
// Entries
var float tEntry  = na
var float tStop   = na
var float tTarget = na
 
if inSess and strategy.position_size == 0 and bar_index > biasBar
    if bias == 1 and bullFvg
        stopPx = math.min(sweepExt, low[2]) - buf
        risk = close - stopPx
        if risk > syminfo.mintick
            strategy.entry("Long", strategy.long)
            strategy.exit("Long Exit", from_entry="Long", stop=stopPx, limit=close + risk * rr)
            tEntry := close
            tStop := stopPx
            tTarget := close + risk * rr
            label.new(bar_index, low, "LONG", style=label.style_label_up, color=color.new(colBull, 25), textcolor=color.white, size=size.small, tooltip="Entry " + str.tostring(close, format.mintick) + "\nStop " + str.tostring(stopPx, format.mintick) + "\nTarget " + str.tostring(tTarget, format.mintick))
            bias := 0
    else if bias == -1 and bearFvg
        stopPx = math.max(sweepExt, high[2]) + buf
        risk = stopPx - close
        if risk > syminfo.mintick
            strategy.entry("Short", strategy.short)
            strategy.exit("Short Exit", from_entry="Short", stop=stopPx, limit=close - risk * rr)
            tEntry := close
            tStop := stopPx
            tTarget := close - risk * rr
            label.new(bar_index, high, "SHORT", style=label.style_label_down, color=color.new(colBear, 25), textcolor=color.white, size=size.small, tooltip="Entry " + str.tostring(close, format.mintick) + "\nStop " + str.tostring(stopPx, format.mintick) + "\nTarget " + str.tostring(tTarget, format.mintick))
            bias := 0
 
// Trade levels
pE = plot(showTrade ? tEntry : na, "Entry", color.new(chart.fg_color, 40), 1, plot.style_steplinebr)
pS = plot(showTrade ? tStop : na, "Stop", colBear, 2, plot.style_steplinebr)
pT = plot(showTrade ? tTarget : na, "Target", colBull, 2, plot.style_steplinebr)
fill(pE, pT, color.new(colBull, 88), "Reward")
fill(pE, pS, color.new(colBear, 88), "Risk")
 
if strategy.position_size == 0 and strategy.position_size[1] == 0
    tEntry := na
    tStop := na
    tTarget := na
 
// Info panel
var table pan = table.new(position.top_right, 2, 7, bgcolor=color.new(chart.bg_color, 8), border_color=color.new(chart.fg_color, 85), border_width=1, frame_color=color.new(chart.fg_color, 70), frame_width=1)
 
pRow(int row, string k, string v, color vc) =>
    table.cell(pan, 0, row, k, text_color=color.new(chart.fg_color, 35), text_size=size.small, text_halign=text.align_left)
    table.cell(pan, 1, row, v, text_color=vc, text_size=size.small, text_halign=text.align_right)
 
if showPanel and barstate.islast
    table.cell(pan, 0, 0, "ICT SILVER BULLET", bgcolor=colDisc, text_color=#0a0b0d, text_size=size.small, text_halign=text.align_left)
    table.merge_cells(pan, 0, 0, 1, 0)
    pRow(1, "Session", sessName, inSess ? colHi : color.new(chart.fg_color, 35))
    pRow(2, "Bias", bias == 1 ? "LONG" : bias == -1 ? "SHORT" : "None", bias == 1 ? colBull : bias == -1 ? colBear : chart.fg_color)
    pRow(3, "Sweep expires", bias != 0 ? str.tostring(math.max(0, expiry - (bar_index - biasBar))) + " bars" : "—", chart.fg_color)
    pRow(4, "Range", hasRange ? (close >= eq ? "Premium " : "Discount ") + str.tostring(math.round(rngPct)) + "%" : "—", hasRange ? (close >= eq ? colPrem : colDisc) : chart.fg_color)
    pRow(5, "Position", strategy.position_size > 0 ? "Long" : strategy.position_size < 0 ? "Short" : "Flat", strategy.position_size > 0 ? colBull : strategy.position_size < 0 ? colBear : chart.fg_color)
    pRow(6, "Reward : risk", "1 : " + str.tostring(rr, "0.0#"), chart.fg_color)

← 返回列表