If you missed it, I'd like to point to our convenient looping functions in Advanced code: https://www.medvedtrader.com/trader/WebHelp/index.html?advanced_looping_functions.htm
From our support emails, it seems that paintbar users may not be aware of them.
For example, if you have a variable SMA20 which is an SMA and you want to do something if it has been rising for the last 4 candles, all you have to do is call:
All(4, SMA20 > SMA20) // will return true for that condition. Don't have to use a for loop etc.
or you want to check if the Close was above SMA20 in at least 5 out of 6 last candles...
Count(6, Close>SMA20) >= 5
nice and simple.