input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
def value = MACD(fastLength, slowLength, MACDLength, averageType);
def avg = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
def Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
def last_cross = if value crosses above avg then 1 else if value crosses below avg then 0 else last_cross[1];
DefineGlobalColor("Above-UP", Color.LIME);
DefineGlobalColor("Below-UP", Color.GREEN);
DefineGlobalColor("Below-Down", Color.PINK);
DefineGlobalColor("Above_Down", Color.RED);
input label = yes;
AddLabel(label and last_cross == 1, "Value crosses ABOVE Avg when" + (if Value > 0 or Avg > 0 then " Above 0 line" else " Below 0 line")
, if Avg > 0 or Value > 0 then GlobalColor("Above-UP") else GlobalColor("Below-UP"));
AddLabel(label and last_cross == 0, "Value crosses BELOW Avg when" + (if Value > 0 or Avg > 0 then " Above 0 line" else " Below 0 line")
, if Avg < 0 or Value < 0 then GlobalColor("Below-Down") else GlobalColor("Above_Down"));
input showbubbles = yes;
input bubbleoffset = 3;
input limit_bubbles = 10;
def count = if !IsNaN(close) then count[1] + 1 else count[1];
def cond = HighestAll(count) - count + 1;
AddChartBubble(showbubbles and Value crosses above Avg, if limit_bubbles and cond > limit_bubbles then Double.NaN else low - TickSize() * bubbleoffset, "Up" , if Avg > 0 or Value > 0 then GlobalColor("Above-UP") else GlobalColor("Below-UP"), no);
AddChartBubble(showbubbles and Value crosses below Avg, if limit_bubbles and cond > limit_bubbles then Double.NaN else high + TickSize() * bubbleoffset, "Dn" , if Avg < 0 or Value < 0 then GlobalColor("Below-Down") else GlobalColor("Above_Down"), yes);
#AddChartBubble(bubbles and value crosses above avg, low - TickSize() * bubbleoffset, "Up" , if avg > 0 or value > 0 then GlobalColor("Above-UP") else GlobalColor("Below-UP"), no);
#AddChartBubble(bubbles and value crosses below avg, high + TickSize() * bubbleoffset, "Dn" , if avg < 0 or value < 0 then GlobalColor("Below-Down") else GlobalColor("Above_Down"), yes);
input verticalline = yes;
AddVerticalLine(verticalline and value crosses above avg , "", if value > 0 or avg > 0 then GlobalColor("Above-UP") else GlobalColor("Below-UP"), Curve.LONG_DASH);
AddVerticalLine(verticalline and value crosses below avg, "", if value < 0 or avg < 0 then GlobalColor("Below-Down") else GlobalColor("Above_Down"), Curve.LONG_DASH);
input alert = yes;
Alert(alert, if Value crosses above Avg then "Cross ABOVE" else "Cross BELOW", Alert.BAR, Sound.Chimes);
#