top of page

Pine Script Library

Public·1 member

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 via `ta.pivothigh` / `ta.pivotlow` (configurable left/right 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 line-break level plots, red/green triangle markers, a background tint on the sweep bar, and fires `alertcondition` events.


## 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 Pivot 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.


## Pine Script v5 code


```

//@version=5

// ─────────────────────────────────────────────

// │ 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.

// ============================================================


indicator("MF Liquidity Sweep Detector", shorttitle="MF LSD", overlay=true, max_labels_count=50)


// --- Inputs ---

leftBars = input.int(5, "Pivot Left Bars", minval=1)

rightBars = input.int(5, "Pivot Right Bars", minval=1)

lookback = input.int(50, "Pivot Lookback (bars)", minval=1)

minPenPct = input.float(0.0, "Min Penetration %", step=0.05)

showLevels = input.bool(true, "Show Pivot Levels")

showLabel = input.bool(true, "Show Status Label")


// --- Williams pivots (confirmed `rightBars` bars after the pivot bar) ---

ph = ta.pivothigh(high, leftBars, rightBars)

pl = ta.pivotlow(low, leftBars, rightBars)


// --- Carry forward most recent pivot + age it ---

var float lastPivotHigh = na

var float lastPivotLow = na

var int barsSinceHigh = 999

var int barsSinceLow = 999


if not na(ph)

lastPivotHigh := ph

barsSinceHigh := 0

else

barsSinceHigh := barsSinceHigh + 1


if not na(pl)

lastPivotLow := pl

barsSinceLow := 0

else

barsSinceLow := barsSinceLow + 1


activeHigh = barsSinceHigh <= lookback ? lastPivotHigh : na

activeLow = barsSinceLow <= lookback ? lastPivotLow : na


// --- Sweep conditions (single-bar wick + reject) ---

bearSweep = not na(activeHigh) and high > activeHigh * (1 + minPenPct / 100) and close < activeHigh

bullSweep = not na(activeLow) and low < activeLow * (1 - minPenPct / 100) and close > activeLow


// --- Plots: pivot levels ---

plot(showLevels ? activeHigh : na, title="Active Pivot High",

color=color.new(color.red, 0), style=plot.style_linebr, linewidth=1)

plot(showLevels ? activeLow : na, title="Active Pivot Low",

color=color.new(color.green, 0), style=plot.style_linebr, linewidth=1)


// --- Sweep arrows ---

plotshape(bearSweep, title="Bear Sweep", style=shape.triangledown,

location=location.abovebar, color=color.red, size=size.small)

plotshape(bullSweep, title="Bull Sweep", style=shape.triangleup,

location=location.belowbar, color=color.green, size=size.small)


// --- Background tint on the sweep bar ---

bgcolor(bearSweep ? color.new(color.red, 80) :

bullSweep ? color.new(color.green, 80) : na)


// --- Status label ---

var label statusLbl = na

if showLabel and barstate.islast

label.delete(statusLbl)

statusLbl := label.new(bar_index, high,

"Liquidity Sweep L:" + str.tostring(leftBars) +

" R:" + str.tostring(rightBars) +

" Lookback:" + str.tostring(lookback),

style=label.style_label_left,

color=color.new(color.gray, 70),

textcolor=color.white,

size=size.small)


// --- Alerts ---

alertcondition(bullSweep, title="Bull Sweep",

message="MF LSD: Bull Sweep — stop run below pivot low reclaimed")

alertcondition(bearSweep, title="Bear Sweep",

message="MF LSD: Bear Sweep — stop run above pivot high rejected")

```


Drop your tweaks, screenshots, and questions below — that's how the library gets better.


6 Views
Brain with financial data analysis.

Inquiries at :

Important Risk Notice: Trading involves substantial risk of loss. This is educational content only—not advice. Full details here  ------------>  

Proceed only if you're prepared.

tel#: (843) 321-8514

bottom of page