OPEN-SOURCE SCRIPT

Liquidity Sweeps + MSS (Valid / Ignored)

149
//version=5
indicator("Liquidity Sweeps + MSS (Valid / Ignored)", overlay=true, max_labels_count=200, max_lines_count=200)

//──────────────────────────────────────────────────────────────
// Inputs
structureLookback = input.int(20, "Structure Lookback (recent highs/lows)", minval=10)
rangeLookback = input.int(80, "Range Lookback (to define extremes)", minval=30)
extremeZonePct = input.float(0.25, "Extreme Zone % (0.25 = top/bottom 25%)", minval=0.05, maxval=0.45, step=0.05)

useCloseReentry = input.bool(true, "Require close back inside level (reentry)")

// MSS / Swings
pivotLeft = input.int(3, "Swing Pivot Left", minval=1)
pivotRight = input.int(3, "Swing Pivot Right", minval=1)
mssMode = input.string("Close", "MSS Break uses", options=["Close","Wick"]) // Close = stricter

useImpulseFilter = input.bool(true, "Filter strong impulse candles")
impulseATRmult = input.float(1.0, "Max body size (ATR multiple)", minval=0.3, maxval=3.0, step=0.1)
atrLen = input.int(14, "ATR Length", minval=5)

showOnlyValid = input.bool(true, "Show only VALID signals")
showPending = input.bool(true, "Show PENDING label")
plotStructureLvls = input.bool(false, "Plot recent structure levels")
plotMssLevel = input.bool(true, "Plot MSS level while pending")

//──────────────────────────────────────────────────────────────
// Helpers
atr = ta.atr(atrLen)
body = math.abs(close - open)

// Recent structure (dynamic)
recentHigh = ta.highest(high, structureLookback)
recentLow = ta.lowest(low, structureLookback)

// Bigger “range” to determine if we are at extremes vs middle
rngHigh = ta.highest(high, rangeLookback)
rngLow = ta.lowest(low, rangeLookback)
rng = math.max(rngHigh - rngLow, syminfo.mintick)
pos = (close - rngLow) / rng // 0..1

isTopExtreme = pos >= (1.0 - extremeZonePct)
isBotExtreme = pos <= extremeZonePct

impulseOk = not useImpulseFilter or (body <= atr * impulseATRmult)

//──────────────────────────────────────────────────────────────
// Swings for MSS (pivot-based)
ph = ta.pivothigh(high, pivotLeft, pivotRight)
pl = ta.pivotlow(low, pivotLeft, pivotRight)

var float lastSwingHigh = na
var float lastSwingLow = na

// Update latest confirmed swing points (they appear pivotRight bars late, that's fine)
if not na(ph)
lastSwingHigh := ph
if not na(pl)
lastSwingLow := pl

// Optional: plot structure levels
plot(plotStructureLvls ? recentHigh : na, "Recent High", color=color.new(color.red, 70), style=plot.style_linebr, linewidth=2)
plot(plotStructureLvls ? recentLow : na, "Recent Low", color=color.new(color.lime,70), style=plot.style_linebr, linewidth=2)

//──────────────────────────────────────────────────────────────
// Raw sweeps (wick through recent level + optional close reentry)
rawSweepHigh = high > recentHigh[1] and (useCloseReentry ? close < recentHigh[1] : true)
rawSweepLow = low < recentLow[1] and (useCloseReentry ? close > recentLow[1] : true)

// Location filter
preValidHigh = rawSweepHigh and isTopExtreme and impulseOk
preValidLow = rawSweepLow and isBotExtreme and impulseOk

//──────────────────────────────────────────────────────────────
// State machine: PENDING → VALID/IGNORED based on MSS break
var int pendingDir = 0 // 1 = high sweep pending, -1 = low sweep pending, 0 = none
var int pendingStartBar = na
var float pendingMssLevel = na
var label pendingLabel = na

// MSS break condition
breakDown = mssMode == "Close" ? close < pendingMssLevel : low < pendingMssLevel
breakUp = mssMode == "Close" ? close > pendingMssLevel : high > pendingMssLevel

confirmMSSHigh = pendingDir == 1 and not na(pendingMssLevel) and breakDown
confirmMSSLow = pendingDir == -1 and not na(pendingMssLevel) and breakUp

// If no MSS level exists at sweep time, we will ignore (strict = fewer signals)
noMssLevel = pendingDir != 0 and na(pendingMssLevel)

// Pending visualization of MSS level
plot(plotMssLevel and pendingDir != 0 ? pendingMssLevel : na, "Pending MSS Level", color=color.new(color.yellow, 0), style=plot.style_linebr, linewidth=2)

//──────────────────────────────────────────────────────────────
// Label helpers
makeIgnoredLabel(_isHigh, _reason) =>
if not showOnlyValid
float y = _isHigh ? high : low
labelStyle = _isHigh ? label.style_label_down : label.style_label_up
label.new(bar_index, y, "SWEEP IGNORED\n" + _reason, style=labelStyle, color=color.new(color.gray, 0), textcolor=color.white)

makePendingLabel(_isHigh, _mss) =>
if showPending
float y = _isHigh ? high : low
labelStyle = _isHigh ? label.style_label_down : label.style_label_up
string txt = "SWEEP PENDING\nMSS: " + (na(_mss) ? "na" : str.tostring(_mss, format.mintick))
label.new(bar_index, y, txt, style=labelStyle, color=color.new(color.orange, 0), textcolor=color.white)
else
na

setValidLabel(_lbl, _isHigh) =>
if not na(_lbl)
label.set_text(_lbl, "SWEEP VALID\n(MSS)")
label.set_color(_lbl, _isHigh ? color.new(color.red, 0) : color.new(color.lime, 0))
label.set_textcolor(_lbl, color.white)

//──────────────────────────────────────────────────────────────
// Main flow
if pendingDir == 0
// New HIGH sweep candidate
if rawSweepHigh
if preValidHigh
pendingDir := 1
pendingStartBar := bar_index
// MSS level for HIGH sweep = lastSwingLow (the low we want to break)
pendingMssLevel := lastSwingLow
pendingLabel := makePendingLabel(true, pendingMssLevel)

// If we cannot define MSS level => ignore (strict)
if na(pendingMssLevel)
makeIgnoredLabel(true, "no swing low (MSS) yet")
if not na(pendingLabel)
label.delete(pendingLabel)
pendingDir := 0
pendingStartBar := na
pendingMssLevel := na
pendingLabel := na
else
makeIgnoredLabel(true, "filters (location/impulse/reentry)")
// New LOW sweep candidate (only if no pending created above)
if rawSweepLow and pendingDir == 0
if preValidLow
pendingDir := -1
pendingStartBar := bar_index
// MSS level for LOW sweep = lastSwingHigh (the high we want to break)
pendingMssLevel := lastSwingHigh
pendingLabel := makePendingLabel(false, pendingMssLevel)

if na(pendingMssLevel)
makeIgnoredLabel(false, "no swing high (MSS) yet")
if not na(pendingLabel)
label.delete(pendingLabel)
pendingDir := 0
pendingStartBar := na
pendingMssLevel := na
pendingLabel := na
else
makeIgnoredLabel(false, "filters (location/impulse/reentry)")
else
// Pending: confirm with MSS
if confirmMSSHigh
setValidLabel(pendingLabel, true)
// Reset
pendingDir := 0
pendingStartBar := na
pendingMssLevel := na
pendingLabel := na
else if confirmMSSLow
setValidLabel(pendingLabel, false)
// Reset
pendingDir := 0
pendingStartBar := na
pendingMssLevel := na
pendingLabel := na

// Alerts only on VALID MSS
alertcondition(confirmMSSHigh, "Sweep VALID High (MSS)", "VALID liquidity sweep HIGH confirmed by MSS")
alertcondition(confirmMSSLow, "Sweep VALID Low (MSS)", "VALID liquidity sweep LOW confirmed by MSS")

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.