//@version=3
study("My Script")
numBars = input(title="Number of days", type=integer, defval=20, minval=1, maxval=300)
sma = sma(close, numBars)
// the next two string are used in case the chart's resolution differs from daily
securDaily = security(tickerid, 'D', close)
daylyRes = period == 'D' ? close : securDaily
crossed = 0
for i = 0 to numBars
if crossover(sma[i], daylyRes[i])
crossed := 1
break
plot(crossed)
В такой ситуации полезен цикл for.