#MACD_Crosses_Verticals
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#
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-Above", Color.GREEN);
DefineGlobalColor("Above-Below", Color.LIME);
DefineGlobalColor("Below-Below", Color.RED);
DefineGlobalColor("Below-Above", Color.PINK);
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-Above") else GlobalColor("Above-Below"));
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-Below") else GlobalColor("Below-Above"));
input bubbles = yes;
input bubbleoffset = 3;
AddChartBubble(bubbles and value crosses above avg, low - TickSize() * bubbleoffset, "Up" , if avg > 0 or value > 0 then GlobalColor("Above-Above") else GlobalColor("Above-Below"), no);
AddChartBubble(bubbles and value crosses below avg, high + TickSize() * bubbleoffset, "Dn" , if avg < 0 or value < 0 then GlobalColor("Below-Below") else GlobalColor("Below-Above"), yes);
input verticalline = yes;
AddVerticalLine(verticalline and value crosses above avg , "", if value > 0 or avg > 0 then GlobalColor("Above-Above") else GlobalColor("Above-Below"), Curve.LONG_DASH);
AddVerticalLine(verticalline and value crosses below avg, "", if value < 0 or avg < 0 then GlobalColor("Below-Below") else GlobalColor("Below-Above"), Curve.LONG_DASH);
input alert = yes;
Alert(alert, if value crosses above avg then "Cross ABOVE" else "Cross BELOW", Alert.BAR, Sound.Chimes);
#