OPEN-SOURCE SCRIPT
Institutional Reload Zones

//version=5
indicator("MSS Institutional Reload Zones (HTF + Sweep + Displacement) [Stable]", overlay=true, max_boxes_count=20, max_labels_count=50)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Inputs
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
pivotLeft = input.int(3, "Pivot Left", minval=1)
pivotRight = input.int(3, "Pivot Right", minval=1)
htfTf = input.timeframe("60", "HTF Timeframe (60=1H, 240=4H)")
emaFastLen = input.int(50, "HTF EMA Fast", minval=1)
emaSlowLen = input.int(200, "HTF EMA Slow", minval=1)
atrLen = input.int(14, "ATR Length", minval=1)
dispMult = input.float(1.2, "Displacement ATR Mult", minval=0.5, step=0.1)
closeTopPct = input.float(0.25, "Close within top %", minval=0.05, maxval=0.5, step=0.05)
sweepLookbackBars = input.int(60, "Sweep lookback (bars)", minval=10, maxval=500)
sweepValidBars = input.int(30, "Sweep active for N bars", minval=5, maxval=200)
cooldownBars = input.int(30, "Signal cooldown (bars)", minval=0, maxval=300)
extendBars = input.int(200, "Extend zones (bars)", minval=20)
showOB = input.bool(true, "Show Pullback OB zone")
showFib = input.bool(true, "Show 50-61.8% zone")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// HTF trend filter
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
htfClose = request.security(syminfo.tickerid, htfTf, close)
htfEmaFast = request.security(syminfo.tickerid, htfTf, ta.ema(close, emaFastLen))
htfEmaSlow = request.security(syminfo.tickerid, htfTf, ta.ema(close, emaSlowLen))
htfBull = (htfEmaFast > htfEmaSlow) and (htfClose >= htfEmaFast)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LTF structure pivots
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
atr = ta.atr(atrLen)
ph = ta.pivothigh(high, pivotLeft, pivotRight)
pl = ta.pivotlow(low, pivotLeft, pivotRight)
var float lastSwingHigh = na
var float lastSwingLow = na
if not na(ph)
lastSwingHigh := ph
if not na(pl)
lastSwingLow := pl
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Sweep filter (simple + robust)
// “sweep” = breaks below lowest low of last N bars and reclaims (close back above that level)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
sweepLevel = ta.lowest(low, sweepLookbackBars)[1]
sweepNow = (low < sweepLevel) and (close > sweepLevel)
var int sweepUntil = na
if sweepNow
sweepUntil := bar_index + sweepValidBars
sweepActive = not na(sweepUntil) and (bar_index <= sweepUntil)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Displacement filter
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
cRange = high - low
closeTopOk = close >= (high - cRange * closeTopPct)
dispOk = (cRange >= atr * dispMult) and closeTopOk and (close > open)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// MSS bullish (filtered)
// base MSS: close crosses above last swing high
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
baseMssBull = (not na(lastSwingHigh)) and ta.crossover(close, lastSwingHigh)
var int lastSignalBar = na
cooldownOk = na(lastSignalBar) ? true : (bar_index - lastSignalBar >= cooldownBars)
mssBull = baseMssBull and htfBull and sweepActive and dispOk and cooldownOk
if mssBull
lastSignalBar := bar_index
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Find last bearish candle before MSS for OB zone
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_lastBearish(_lookback) =>
float obH = na
float obL = na
int found = 0
for i = 1 to _lookback
if found == 0 and close < open
obH := high
obL := low
found := 1
[obH, obL]
[obHigh, obLow] = f_lastBearish(30)
// Impulse anchors for fib zone (use lastSwingLow to current high on MSS bar)
impLow = lastSwingLow
impHigh = high
fib50 = (not na(impLow)) ? (impLow + (impHigh - impLow) * 0.50) : na
fib618 = (not na(impLow)) ? (impLow + (impHigh - impLow) * 0.618) : na
fibTop = (not na(fib50) and not na(fib618)) ? math.max(fib50, fib618) : na
fibBot = (not na(fib50) and not na(fib618)) ? math.min(fib50, fib618) : na
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Boxes (delete previous, draw new) — SINGLE LINE calls only
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var box obBox = na
var box fibBox = na
if mssBull
if showOB and not na(obHigh) and not na(obLow)
if not na(obBox)
box.delete(obBox)
obBox := box.new(left=bar_index, top=obHigh, right=bar_index + extendBars, bottom=obLow, bgcolor=color.new(color.gray, 82), border_color=color.new(color.gray, 30))
if showFib and not na(fibTop) and not na(fibBot)
if not na(fibBox)
box.delete(fibBox)
fibBox := box.new(left=bar_index, top=fibTop, right=bar_index + extendBars, bottom=fibBot, bgcolor=color.new(color.teal, 85), border_color=color.new(color.teal, 35))
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Visuals
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plotshape(mssBull, title="MSS Bull (Filtered)", style=shape.labelup, text="MSS✔", size=size.tiny, color=color.new(color.green, 0), textcolor=color.white, location=location.belowbar)
plot(htfEmaFast, title="HTF EMA Fast", color=color.new(color.orange, 80))
plot(htfEmaSlow, title="HTF EMA Slow", color=color.new(color.purple, 80))
indicator("MSS Institutional Reload Zones (HTF + Sweep + Displacement) [Stable]", overlay=true, max_boxes_count=20, max_labels_count=50)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Inputs
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
pivotLeft = input.int(3, "Pivot Left", minval=1)
pivotRight = input.int(3, "Pivot Right", minval=1)
htfTf = input.timeframe("60", "HTF Timeframe (60=1H, 240=4H)")
emaFastLen = input.int(50, "HTF EMA Fast", minval=1)
emaSlowLen = input.int(200, "HTF EMA Slow", minval=1)
atrLen = input.int(14, "ATR Length", minval=1)
dispMult = input.float(1.2, "Displacement ATR Mult", minval=0.5, step=0.1)
closeTopPct = input.float(0.25, "Close within top %", minval=0.05, maxval=0.5, step=0.05)
sweepLookbackBars = input.int(60, "Sweep lookback (bars)", minval=10, maxval=500)
sweepValidBars = input.int(30, "Sweep active for N bars", minval=5, maxval=200)
cooldownBars = input.int(30, "Signal cooldown (bars)", minval=0, maxval=300)
extendBars = input.int(200, "Extend zones (bars)", minval=20)
showOB = input.bool(true, "Show Pullback OB zone")
showFib = input.bool(true, "Show 50-61.8% zone")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// HTF trend filter
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
htfClose = request.security(syminfo.tickerid, htfTf, close)
htfEmaFast = request.security(syminfo.tickerid, htfTf, ta.ema(close, emaFastLen))
htfEmaSlow = request.security(syminfo.tickerid, htfTf, ta.ema(close, emaSlowLen))
htfBull = (htfEmaFast > htfEmaSlow) and (htfClose >= htfEmaFast)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LTF structure pivots
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
atr = ta.atr(atrLen)
ph = ta.pivothigh(high, pivotLeft, pivotRight)
pl = ta.pivotlow(low, pivotLeft, pivotRight)
var float lastSwingHigh = na
var float lastSwingLow = na
if not na(ph)
lastSwingHigh := ph
if not na(pl)
lastSwingLow := pl
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Sweep filter (simple + robust)
// “sweep” = breaks below lowest low of last N bars and reclaims (close back above that level)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
sweepLevel = ta.lowest(low, sweepLookbackBars)[1]
sweepNow = (low < sweepLevel) and (close > sweepLevel)
var int sweepUntil = na
if sweepNow
sweepUntil := bar_index + sweepValidBars
sweepActive = not na(sweepUntil) and (bar_index <= sweepUntil)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Displacement filter
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
cRange = high - low
closeTopOk = close >= (high - cRange * closeTopPct)
dispOk = (cRange >= atr * dispMult) and closeTopOk and (close > open)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// MSS bullish (filtered)
// base MSS: close crosses above last swing high
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
baseMssBull = (not na(lastSwingHigh)) and ta.crossover(close, lastSwingHigh)
var int lastSignalBar = na
cooldownOk = na(lastSignalBar) ? true : (bar_index - lastSignalBar >= cooldownBars)
mssBull = baseMssBull and htfBull and sweepActive and dispOk and cooldownOk
if mssBull
lastSignalBar := bar_index
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Find last bearish candle before MSS for OB zone
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_lastBearish(_lookback) =>
float obH = na
float obL = na
int found = 0
for i = 1 to _lookback
if found == 0 and close < open
obH := high
obL := low
found := 1
[obH, obL]
[obHigh, obLow] = f_lastBearish(30)
// Impulse anchors for fib zone (use lastSwingLow to current high on MSS bar)
impLow = lastSwingLow
impHigh = high
fib50 = (not na(impLow)) ? (impLow + (impHigh - impLow) * 0.50) : na
fib618 = (not na(impLow)) ? (impLow + (impHigh - impLow) * 0.618) : na
fibTop = (not na(fib50) and not na(fib618)) ? math.max(fib50, fib618) : na
fibBot = (not na(fib50) and not na(fib618)) ? math.min(fib50, fib618) : na
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Boxes (delete previous, draw new) — SINGLE LINE calls only
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var box obBox = na
var box fibBox = na
if mssBull
if showOB and not na(obHigh) and not na(obLow)
if not na(obBox)
box.delete(obBox)
obBox := box.new(left=bar_index, top=obHigh, right=bar_index + extendBars, bottom=obLow, bgcolor=color.new(color.gray, 82), border_color=color.new(color.gray, 30))
if showFib and not na(fibTop) and not na(fibBot)
if not na(fibBox)
box.delete(fibBox)
fibBox := box.new(left=bar_index, top=fibTop, right=bar_index + extendBars, bottom=fibBot, bgcolor=color.new(color.teal, 85), border_color=color.new(color.teal, 35))
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Visuals
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plotshape(mssBull, title="MSS Bull (Filtered)", style=shape.labelup, text="MSS✔", size=size.tiny, color=color.new(color.green, 0), textcolor=color.white, location=location.belowbar)
plot(htfEmaFast, title="HTF EMA Fast", color=color.new(color.orange, 80))
plot(htfEmaSlow, title="HTF EMA Slow", color=color.new(color.purple, 80))
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.