This indicator measures confirmed RSI crossings and plots the close of the latest accepted event as a reference price, not proven support. The cooldown measures distance from the previous raw confirmed crossing, including a crossing rejected by the cooldown. With a minimum of five bars, raw events at bars 10 and 15 can both pass; an intervening event at bar 13 resets the distance and prevents bar 15 from passing. The first event is allowed.
Pine Script v6: Event lookback with barssince and valuewhenpinescript
//@version=6
indicator("Event Lookback & Anchor Tracker [v6]", overlay = true)
// Input settings
int rsiLength = input.int(14, "RSI Length", minval = 1)
float rsiOversold = input.float(30.0, "Oversold Level", minval = 0, maxval = 100)
int minCooldownBars = input.int(5, "Minimum Cooldown Bars", minval = 1)
// Calculations
float rsiVal = ta.rsi(close, rsiLength)
bool crossed = ta.crossover(rsiVal, rsiOversold)
bool rawSignal = crossed and barstate.isconfirmed
// Bars since previous signal
int barsSincePrior = ta.barssince(rawSignal[1]) + 1
bool isReady = na(barsSincePrior) or (barsSincePrior >= minCooldownBars)
bool confirmedSignal = rawSignal and isReady
// Store price of the latest confirmed signal
float anchorPrice = ta.valuewhen(confirmedSignal, close, 0)
int barsSinceSignal = ta.barssince(confirmedSignal)
// Plotting
plot(anchorPrice, "Accepted event close", color = color.blue, linewidth = 2, style = plot.style_linebr)
plotshape(confirmedSignal, "Accepted event", shape.triangleup, location.belowbar, color.green, size = size.small)
// Reuse one label rather than creating one on every elapsed realtime bar.
var label statusLabel = label.new(na, na, "", color = color.purple, textcolor = color.white, style = label.style_label_down)
if barstate.islast and not na(barsSinceSignal)
label.set_xy(statusLabel, bar_index, high)
label.set_text(statusLabel, "Bars since accepted event: " + str.tostring(barsSinceSignal))