I am probably being very very academic about the terms you have used here and have been scratching my head for a couple of days. I think the [for X bars] is misleading a bit and needs to be changed to something more suitable. E.g. When we say MA(10) is greater than MA(20) for 4 bars, in layman terms it means the MA(10) is higher than MA(20) for all of the last 4 bars. The code though is saying it needs to be greater at least once which from the layman perspective is incorrect.
The atLeastOnce check should be changed to a count whose value should equal the X bars value. I have included my suggested solution.
The change also affects the IsLowerCount comparison.
The current code is good though but should use a different phrase, maybe call it [(> X) Is greater in last X bars] or something like that.
regards
beetrader