Help on for command

Hello

I would like to know the nearest flat SSB (ichimoku) value above closing price. I wrote this but this does not work properly and I don’t know why… any idea?

flatichimokulevel=input(9)
isflat(x) =>
out = true
for i = 0 to flatichimokulevel-1
out := out and (x[i] == x[i+1])
out
lowestFlatSSBabove(p) =>
out=close
ssb=leadingSpan2
for i = 0 to (p+displacement)
ssbi=ssb[i]
out := (ssb[i] <= ssb[i+1] and ssb[i] >= close and isflat(ssbi)) ? ssb[i] : out
out