I think this should do the job. I have included the count for bars since cross below as well. Then colored the background based on which count is greater. You can adjust the length and the average type in the user inputs. Which will make this applicable to a broad range of specifications.
input price = close;
input length = 20;
input type = AverageType.HULL;
input displace = 0;
def ma = MovingAverage(type, price, length)[-displace];
def crossAbove = close[1] < ma[1] and close > ma;
def crossBelow = close[1] > ma[1] and close < ma; rec countAbove = if crossAbove then 1 else if close > ma then countAbove[1] + 1 else 0;
rec countBelow = if crossBelow then -1 else if close < ma then countBelow[1] - 1 else 0; plot data = if countAbove > AbsValue(countBelow) then countAbove else countBelow;
data.AssignValueColor(if data == 0 then Color.CURRENT else Color.BLACK);
AssignBackgroundColor(if data > 0 then Color.GREEN else if data < 0 then Color.RED else Color.CURRENT);