input Period = aggregationPeriod.FIVE_MIN;
input AvgType = averageType.EXPONENTIAL;
input Length = 9;
input ShowMAlabel = yes;
input ATR_distance = 0.25;
input ATR_touch_distance = 0.05;
def c = close(period = Period);
def h = high(period = Period);
def l = low(period = Period);
plot AVG = MovingAverage(AvgType, c, Length);
AVG.hide();
plot AVG_hi = avg + ATR_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);
plot AVG_lo = avg - ATR_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);
AVG_hi.hide();
AVG_lo.hide();
plot AVG_hi_touch = avg + ATR_touch_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);
plot AVG_lo_touch = avg - ATR_touch_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);
AVG_hi_touch.hide();
AVG_lo_touch.hide();
plot movavgbubble = if !isnan(close) and isnan(close[-1]) then avg else double.nan;
movavgbubble.setdefaultcolor(color.green);
Addlabel(ShowMAlabel, " 5M9 = "+ round(AVG) + " ", if close < AVG_hi_touch and close > AVG_lo_touch then color.cyan else if close < avg_lo or close > avg_hi then COLOR.GREEN else color.red);