// This Pine Script® code is licensed under Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// © PineForge contributors 2026
//
// PF probe liquidity-sweep-probe-02-sweep-bar — wick-pierce + close-back-inside
// Clean-room: written from validation goal, not from source.
//
// Purpose: isolates the sweep-bar pattern: a candle whose wick pierces the
// most recently confirmed swing high (or low) but whose close returns
// inside the prior range. The engine surfaces under test are pivot-state
// retention (`var float last_ph`/`last_pl`) and the per-bar boolean
// composition (`high > last_ph and close < last_ph` for a bearish sweep
// of highs; symmetric for bullish sweep of lows). The bullish sweep-of-
// lows triggers a long entry; the bearish sweep-of-highs triggers a
// short entry — the standard liquidity-grab interpretation.
//
// Validation goal: bit-exact entries on the bar that completes a sweep
// pattern. Pass = identical trade list vs TradingView's broker emulator.
//
// TV setup: 15m chart, ETH-USDT-USDT (Binance USDT-M Perpetual). Full window
// matching corpus/data/ohlcv_ETH-USDT-USDT_15m.csv. Strategy properties:
// leave defaults from script header. Export "List of trades" → tv_trades.csv.
//@version=6
strategy("PF probe liquidity-sweep-probe-02-sweep-bar", shorttitle="PF_lsp02_SWP",
overlay=true, initial_capital=1000000,
commission_type=strategy.commission.percent, commission_value=0,
slippage=0, default_qty_type=strategy.fixed, default_qty_value=1,
pyramiding=1, process_orders_on_close=false)
// ---- inputs --------------------------------------------------------
int i_left = input.int(5, "Pivot left bars", minval=1)
int i_right = input.int(5, "Pivot right bars", minval=1)
// ---- pivot tracking -----------------------------------------------
float ph = ta.pivothigh(high, i_left, i_right)
float pl = ta.pivotlow(low, i_left, i_right)
var float last_ph = na
var float last_pl = na
if not na(ph)
last_ph := ph
if not na(pl)
last_pl := pl
// ---- sweep patterns ------------------------------------------------
bool sweep_high = not na(last_ph) and high > last_ph and close < last_ph
bool sweep_low = not na(last_pl) and low < last_pl and close > last_pl
// ---- order routing -------------------------------------------------
if sweep_low and strategy.position_size <= 0
strategy.entry("L", strategy.long, qty=1, comment="bullish sweep long")
if sweep_high and strategy.position_size >= 0
strategy.entry("S", strategy.short, qty=1, comment="bearish sweep short")