How To Limit Repeating Signals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © allanster

//@version=5
indicator("How To Limit Repeating Signals")
inputN = input.int(1, 'Allow N', tooltip = '  0 Disables Signal\n-N Disables Filter')

limitN(_condition, _reset, _N)=>                             // create a function
    var countN = 0                                           // create a counter
    countN := _reset ? 0 : _condition ? countN + 1 : countN  // if reset 0, else if condition +1, else previous count
    allowN  = _N >= 0 ? countN <= _N : true                  // if filter enabled and within allowed N else true
    signal  = _condition and allowN                          // if condition and within allowed N

taRSI = ta.rsi(close, 14)                                    // create an oscillator
level = 50                                                   // create a level
above = taRSI > level                                        // create a condition
below = taRSI < level                                        // create a condition

sigHi = limitN(above, below, inputN)                         // create a signal using the function
sigLo = limitN(below, above, inputN)                         // create a signal using the function

if sigHi                                                     // if above within N occurrences, reset N on below
    alert('signal Hi')                                       // trigger an alert function event
if sigLo                                                     // if below within N occurrences, reset N on above
    alert('signal Lo')                                       // trigger an alert function event

plotshape(sigHi ? taRSI : na, '', shape.triangleup,
 location.absolute, color.lime)                            // if signal plot shape at Y value of RSI else not available
plotshape(sigLo ? taRSI : na, '', shape.triangledown,
 location.absolute, color.red)                             // if signal plot shape at Y value of RSI else not available

hline(30), hline(50), hline(70)                              // plot some levels
plot(taRSI, '', color.purple)                              // plot oscillator