And finally for Trading View traders, here is the version for version 5 of Pine script.
Transitioning this architecture to TradingView’s Pine Script v5 requires a complete paradigm shift. Because Pine operates on a Time Series basis—executing the entire script on every historical bar sequentially before moving to real-time ticks—you do not use arrays, loops, or object-oriented classes to track state.
Instead, we use the var keyword to create a persistent State Machine. Variables declared with var retain their values across bars unless explicitly updated, allowing us to perfectly replicate the Sweep → MSS → FVG chronological sequence.
Here is the professional, fully automated strategy() implementation in Pine Script v5.
The Pine Script v5 Implementation
Code: Select all
//@version=5
strategy("Silver Bullet ICT", overlay=true, calc_on_every_tick=true, currency=currency.USD, initial_capital=10000)
// =========================================================================
// 1. INPUTS
// =========================================================================
risk_pct = input.float(1.0, "Risk %", group="Risk & Sizing")
rr_target = input.float(1.0, "R:R Target", group="Trade Management")
partial_pct = input.float(50.0, "Partial Close %", group="Trade Management")
trail_pips = input.float(15.0, "Trailing Distance (Pips)", group="Trade Management")
// Time windows natively handle EST/EDT daylight saving shifts
session_time = input.session("1000-1100", "ICT NY AM Session", timezone="America/New_York")
lookback = input.int(50, "Liquidity Lookback", group="Market Structure")
// =========================================================================
// 2. TIME & SESSION PARSING
// =========================================================================
in_session = not na(time(timeframe.period, session_time, "America/New_York"))
new_session = in_session and not in_session[1]
// =========================================================================
// 3. PERSISTENT STATE MACHINE
// =========================================================================
var int STATE_IDLE = 0
var int STATE_SWEPT_BSL = 1
var int STATE_SWEPT_SSL = 2
var int STATE_MSS_BEAR = 3
var int STATE_MSS_BULL = 4
var int state = STATE_IDLE
var float bsl = na
var float ssl = na
var float mss_level = na
var float entry_price = na
var float stop_loss = na
var float take_profit = na
// Reset State at the exact minute the session opens
if new_session
state := STATE_IDLE
bsl := ta.highest(high, lookback)[1]
ssl := ta.lowest(low, lookback)[1]
strategy.cancel_all() // Clear stale FVG limit orders from yesterday
// =========================================================================
// 4. MICROSTRUCTURE SCANNING
// =========================================================================
if in_session and state == STATE_IDLE
// Buyside Sweep (Pierced BSL, closed below)
if high > bsl and close < bsl
state := STATE_SWEPT_BSL
mss_level := ta.lowest(low, 5)[1] // Identify structural fractal low
// Sellside Sweep (Pierced SSL, closed above)
else if low < ssl and close > ssl
state := STATE_SWEPT_SSL
mss_level := ta.highest(high, 5)[1] // Identify structural fractal high
// Market Structure Shift (MSS)
if state == STATE_SWEPT_BSL and close < mss_level
state := STATE_MSS_BEAR
else if state == STATE_SWEPT_SSL and close > mss_level
state := STATE_MSS_BULL
// =========================================================================
// 5. FVG DETECTION & EXECUTION
// =========================================================================
// Convert User Pips to Broker Points
pips_to_points = trail_pips * (syminfo.mintick * 10)
if state == STATE_MSS_BEAR
// Bearish FVG Check: High of recent candle fails to reach Low of origin candle
if high < low[2]
entry_price := high + ((low[2] - high) / 2) // CE (50% midpoint)
stop_loss := high[1] // Safe SL above displacement wick
take_profit := entry_price - (math.abs(entry_price - stop_loss) * rr_target)
// Sizing Math
risk_amt = strategy.equity * (risk_pct / 100)
qty = risk_amt / (math.abs(entry_price - stop_loss) * syminfo.pointvalue)
strategy.entry("Short", strategy.short, qty=qty, limit=entry_price)
state := STATE_IDLE // Lock state to prevent duplicate orders
else if state == STATE_MSS_BULL
// Bullish FVG Check: Low of recent candle fails to reach High of origin candle
if low > high[2]
entry_price := low + ((high[2] - low) / 2) // CE (50% midpoint)
stop_loss := low[1] // Safe SL below displacement wick
take_profit := entry_price + (math.abs(entry_price - stop_loss) * rr_target)
// Sizing Math
risk_amt = strategy.equity * (risk_pct / 100)
qty = risk_amt / (math.abs(entry_price - stop_loss) * syminfo.pointvalue)
strategy.entry("Long", strategy.long, qty=qty, limit=entry_price)
state := STATE_IDLE // Lock state to prevent duplicate orders
// =========================================================================
// 6. TRADE MANAGEMENT (PARTIALS & TRAILING)
// =========================================================================
if strategy.position_size > 0
// Exit 1: The 1R Partial Close
strategy.exit("1R TP", from_entry="Long", qty_percent=partial_pct, limit=take_profit, stop=stop_loss)
strategy.exit("1R TP", from_entry="Short", qty_percent=partial_pct, limit=take_profit, stop=stop_loss)
// Exit 2: The Runner (Activates Trailing at 1R)
strategy.exit("Runner", from_entry="Long", stop=stop_loss, trail_price=take_profit, trail_points=pips_to_points)
strategy.exit("Runner", from_entry="Short", stop=stop_loss, trail_price=take_profit, trail_points=pips_to_points)
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.