Liquidity Sweep Detector — Market Structure Drop
Liquidity Sweep Detector — Market Structure Drop
Most "breakouts" at obvious swing highs and lows are actually liquidity grabs — price pokes through to trigger stops, then reverses. This indicator flags those single-bar stop-runs and rejections so you can stop fading every wick and start fading the ones that matter.
## What it does
- Tracks recent Williams-style pivot highs and pivot lows (configurable left/right bar strength).
- Keeps those levels "active" for a defined lookback window so old, stale pivots stop firing signals.
- Flags a **Bear Sweep** when a bar's *high* pierces the active pivot high but the *close* prints back below it.
- Flags a **Bull Sweep** when a bar's *low* pierces the active pivot low but the *close* prints back above it.
- Draws dashed level lines, red/green arrows, a background tint on the sweep bar, and fires alerts.
## Recommended settings
- Pivot Left Bars: **5**
- Pivot Right Bars: **5**
- Pivot Lookback: **50** bars
- Min Penetration %: **0.0** (raise to 0.05–0.10 on noisy futures to require a real poke)
- Show Levels: **on**
- Show Status Label: **on**
## Ideal timeframe
5-min through 1-hour intraday for futures and large-cap equities; daily for swing-trade setups on indexes and majors.
## How to use
Treat a confirmed sweep as a *signal*, not a *trade*. The cleanest setups are bull sweeps into a higher-timeframe demand zone or bear sweeps into supply / a prior breakdown level — enter on a re-test of the swept level with a stop just beyond the wick. Skip sweeps that print mid-range or against a strong trend on the higher timeframe; those tend to chop. Pair it with VWAP, the prior day high/low, or an HTF EMA stack for context.
## thinkScript code
```
# ─────────────────────────────────────────────
# │ MarketFragments.com | DNA & Market │
# │ info@marketfragments.com │
# │ www.marketfragments.com │
# ─────────────────────────────────────────────
# Time →
# │
# █ █ █│ █
# █ █ █ │ █ █
# █ █ █ │ █ █ █ ╭─╮
# █ █ █ │ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ ╰─────╯
# ──────┴──────────────────────────────────────────────────────────────
# T1 T2 T3 T4 T5 T6
#
# ============================================================
# MF Liquidity Sweep Detector (Market Structure)
# ------------------------------------------------------------
# Flags stop-runs at recent swing highs/lows that get rejected
# inside the same bar:
# Bear Sweep = bar wicks ABOVE a recent pivot high but CLOSES
# below it (failed breakout, longs trapped)
# Bull Sweep = bar wicks BELOW a recent pivot low but CLOSES
# above it (failed breakdown, shorts trapped)
# Pivot levels are tracked with Williams-style left/right bars
# and stay "active" for `lookback` bars after they confirm.
# Free for non-commercial use. Build on it, share back.
# ============================================================
declare upper;
input leftBars = 5;
input rightBars = 5;
input lookback = 50;
input minPenetrationPct = 0.0;
input showLevels = yes;
input showLabel = yes;
# --- Williams pivots, confirmed `rightBars` bars after the fact ---
def isPivotHigh = high[rightBars] == Highest(high, leftBars + rightBars + 1)[rightBars];
def isPivotLow = low[rightBars] == Lowest(low, leftBars + rightBars + 1)[rightBars];
def pivotHigh = if isPivotHigh then high[rightBars] else Double.NaN;
def pivotLow = if isPivotLow then low[rightBars] else Double.NaN;
# --- Carry forward most recent pivot + age it ---
def lastPivotHigh = CompoundValue(1,
if !IsNaN(pivotHigh) then pivotHigh else lastPivotHigh[1],
Double.NaN);
def lastPivotLow = CompoundValue(1,
if !IsNaN(pivotLow) then pivotLow else lastPivotLow[1],
Double.NaN);
def barsSinceHigh = CompoundValue(1,
if !IsNaN(pivotHigh) then 0 else barsSinceHigh[1] + 1,
999);
def barsSinceLow = CompoundValue(1,
if !IsNaN(pivotLow) then 0 else barsSinceLow[1] + 1,
999);
def activeHigh = if barsSinceHigh <= lookback then lastPivotHigh else Double.NaN;
def activeLow = if barsSinceLow <= lookback then lastPivotLow else Double.NaN;
# --- Sweep conditions (single-bar wick + reject) ---
def bearSweep = !IsNaN(activeHigh)
and high > activeHigh * (1 + minPenetrationPct / 100)
and close < activeHigh;
def bullSweep = !IsNaN(activeLow)
and low < activeLow * (1 - minPenetrationPct / 100)
and close > activeLow;
# --- Plots: pivot levels ---
plot PivotHighLine = if showLevels then activeHigh else Double.NaN;
PivotHighLine.SetDefaultColor(Color.RED);
PivotHighLine.SetStyle(Curve.SHORT_DASH);
PivotHighLine.SetLineWeight(1);
plot PivotLowLine = if showLevels then activeLow else Double.NaN;
PivotLowLine.SetDefaultColor(Color.GREEN);
PivotLowLine.SetStyle(Curve.SHORT_DASH);
PivotLowLine.SetLineWeight(1);
# --- Plots: sweep arrows ---
plot BearArrow = if bearSweep then high else Double.NaN;
BearArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearArrow.SetDefaultColor(Color.RED);
BearArrow.SetLineWeight(3);
plot BullArrow = if bullSweep then low else Double.NaN;
BullArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullArrow.SetDefaultColor(Color.GREEN);
BullArrow.SetLineWeight(3);
# --- Background tint on sweep bar ---
AssignBackgroundColor(if bearSweep then Color.DARK_RED
else if bullSweep then Color.DARK_GREEN
else Color.CURRENT);
# --- Status label ---
AddLabel(showLabel,
"Liquidity Sweep L:" + leftBars + " R:" + rightBars + " Lookback:" + lookback,
Color.GRAY);
# --- Alerts ---
Alert(bullSweep, "Bull Sweep: stop run below pivot low reclaimed", Alert.BAR, Sound.Chimes);
Alert(bearSweep, "Bear Sweep: stop run above pivot high rejected", Alert.BAR, Sound.Ring);
```
Drop your tweaks, screenshots, and questions below — that's how the library gets better.

