Here the question is how to calculate at which candle that cross happened. Here is some idea: https://fxdreema.com/shared/eHWR6SX5d
But what do you mean by "price crosses the ma21" - it matters. Because in the history of prices you have 4 prices for each candle and these values.
The idea in this example is that I have one variable that I set with value 0 before the calculation, then I check whether the Condition is true for candle 0, and if it's not, I increment its value and run Condition with the updated value... as a result Condition runs one or more times, until it passes. After that the value of the variable will have some value - the number of times that Condition passed minus 1, which is the same as the last CandleID used in Condition.