Code:

```
# Bars Since MA Crossover
# blt
# 6.21.2016
# Displays bar number counting from 1) a moving average cross up and 2) cross down
input length1 = 8;
input length2 = 21;
input avgtyp = AverageType.EXPONENTIAL;
plot ma1 = MovingAverage(avgtyp, close, length1);
plot ma2 = MovingAverage(avgtyp, close, length2);
def countdn = if ma1 crosses below ma2
then BarNumber()
else countdn[1];
def countup = if ma1 crosses above ma2
then BarNumber()
else countup[1];
AddLabel(1, if countdn < countup
then "Bars since last cross of MA1 above MA2 = " + (BarNumber() - countup)
else "Bars since last cross of MA1 below MA2 = " + (BarNumber() - countdn) ,
if countup > countdn
then Color.GREEN
else Color.RED);
# End Study
```