This independently written indicator implements the recurrence above on the chart timeframe. It keeps the displayed value unchanged while the current bar is open. The first Up or Down state has no reversal marker; later changes between +1 and -1 do. The Data Window exposes both preceding averages and the stored state for comparison with the table.
The missing else branch intentionally retains both stored variables. This is an indicator with alert conditions, not order placement or a trading strategy.pine
//@version=6
indicator("HiLo: prior SMA, confirmed hold", overlay = true)
int length = input.int(3, "SMA length", minval = 1)
bool validBar = not na(open) and not na(high) and not na(low) and not na(close) and high >= low and high >= math.max(open, close) and low <= math.min(open, close)
float priorValidFraction = ta.sma(validBar ? 1.0 : 0.0, length)[1]
bool validWindow = bar_index >= length and not na(priorValidFraction) and priorValidFraction == 1.0
float upperCandidate = ta.sma(high, length)[1]
float lowerCandidate = ta.sma(low, length)[1]
float upper = validWindow ? upperCandidate : na
float lower = validWindow ? lowerCandidate : na
bool ready = validBar and validWindow and not na(upper) and not na(lower)
var int direction = 0
var float activator = na
int previousDirection = nz(direction[1], 0)
if barstate.isconfirmed and ready
if close > upper
direction := 1
activator := lower
else if close < lower
direction := -1
activator := upper
bool upReversal = barstate.isconfirmed and ready and direction == 1 and previousDirection == -1
bool downReversal = barstate.isconfirmed and ready and direction == -1 and previousDirection == 1
plot(activator, "Confirmed activator", color = direction == 1 ? color.teal : color.orange, linewidth = 2, style = plot.style_stepline)
plot(upper, "Prior high SMA", display = display.data_window)
plot(lower, "Prior low SMA", display = display.data_window)
plot(direction, "State: +1 up, -1 down, 0 neutral", display = display.data_window)
plot(ready ? 1 : 0, "Valid current bar and prior window", display = display.data_window)
plotshape(upReversal, "Up reversal", shape.triangleup, location.belowbar, color.teal, text = "UP", size = size.tiny)
plotshape(downReversal, "Down reversal", shape.triangledown, location.abovebar, color.orange, text = "DN", size = size.tiny)
alertcondition(upReversal, "HiLo Up reversal", "Confirmed HiLo reversal from Down to Up")
alertcondition(downReversal, "HiLo Down reversal", "Confirmed HiLo reversal from Up to Down")