#
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#
declare lower;
input fastLength = 8;
input slowLength = 21;
input MACDLength = 5;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = Value - Avg;
plot ZeroLine = 0;
plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
def bull = if Diff crosses above 0 then 1 else if Diff crosses below 0 then -1 else bull[1];
input showbubble = no;
AddChartBubble(showbubble and bull, Diff, Diff - Diff[1], if Diff > Diff[1] then Color.GREEN else Color.RED);
AddChartBubble(showbubble and !bull, Diff, Diff - Diff[1], if Diff < Diff[1] then Color.GREEN else Color.RED, no);
input showlabel = no;
AddLabel(showlabel and bull, "Bull: " + (Diff - Diff[1]), if Diff > Diff[1] then Color.GREEN else Color.RED);
AddLabel(showlabel and !bull, "Bear: " + (Diff - Diff[1]), if Diff < Diff[1] then Color.GREEN else Color.RED);
input show_x_labels = yes;
input show_last_x_bars = 8;
def bn = BarNumber();
def lastbar = if IsNaN(close[-1]) and !IsNaN(close) then bn else Double.NaN;
def lastdiff = if bn == HighestAll(lastbar) then Diff - Diff[1] else lastdiff[1];
def lastdiff1 = if bn == HighestAll(lastbar - 1) then Diff - Diff[1] else lastdiff1[1];
def lastdiff2 = if bn == HighestAll(lastbar - 2) then Diff - Diff[1] else lastdiff2[1];
def lastdiff3 = if bn == HighestAll(lastbar - 3) then Diff - Diff[1] else lastdiff3[1];
def lastdiff4 = if bn == HighestAll(lastbar - 4) then Diff - Diff[1] else lastdiff4[1];
def lastdiff5 = if bn == HighestAll(lastbar - 5) then Diff - Diff[1] else lastdiff5[1];
def lastdiff6 = if bn == HighestAll(lastbar - 6) then Diff - Diff[1] else lastdiff6[1];
def lastdiff7 = if bn == HighestAll(lastbar - 7) then Diff - Diff[1] else lastdiff7[1];
def lastdiff8 = if bn == HighestAll(lastbar - 8) then Diff - Diff[1] else lastdiff8[1];
def lastbull = if bn == HighestAll(lastbar) then bull else lastbull[1];
def lastbull1 = if bn == HighestAll(lastbar - 1) then bull else lastbull1[1];
def lastbull2 = if bn == HighestAll(lastbar - 2) then bull else lastbull2[1];
def lastbull3 = if bn == HighestAll(lastbar - 3) then bull else lastbull3[1];
def lastbull4 = if bn == HighestAll(lastbar - 4) then bull else lastbull4[1];
def lastbull5 = if bn == HighestAll(lastbar - 5) then bull else lastbull5[1];
def lastbull6 = if bn == HighestAll(lastbar - 6) then bull else lastbull6[1];
def lastbull7 = if bn == HighestAll(lastbar - 7) then bull else lastbull7[1];
def lastbull8 = if bn == HighestAll(lastbar - 8) then bull else lastbull8[1];
AddLabel(show_x_labels, if lastbull == 1 then "BULL: " else "BEAR: ", Color.WHITE);
AddLabel(show_x_labels and show_last_x_bars >= 1 and lastbull == 1 and lastbull == 1, lastdiff, if lastdiff > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 2 and lastbull == 1 and lastbull1 == 1, lastdiff1, if lastdiff1 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 3 and lastbull == 1 and lastbull2 == 1, lastdiff2, if lastdiff2 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 4 and lastbull == 1 and lastbull3 == 1, lastdiff3, if lastdiff3 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 5 and lastbull == 1 and lastbull4 == 1, lastdiff4, if lastdiff4 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 6 and lastbull == 1 and lastbull5 == 1, lastdiff5, if lastdiff5 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 7 and lastbull == 1 and lastbull6 == 1, lastdiff6, if lastdiff6 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 8 and lastbull == 1 and lastbull7 == 1, lastdiff7, if lastdiff7 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 1 and lastbull == -1, lastdiff, if lastdiff > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 2 and lastbull == -1 and lastbull1 == -1, lastdiff1, if lastdiff1 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 3 and lastbull == -1 and lastbull2 == -1, lastdiff2, if lastdiff2 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 4 and lastbull == -1 and lastbull3 == -1, lastdiff3, if lastdiff3 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 5 and lastbull == -1 and lastbull4 == -1, lastdiff4, if lastdiff4 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 6 and lastbull == -1 and lastbull5 == -1, lastdiff5, if lastdiff5 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 7 and lastbull == -1 and lastbull6 == -1, lastdiff6, if lastdiff6 > 0 then Color.GREEN else Color.RED);
AddLabel(show_x_labels and show_last_x_bars >= 8 and lastbull == -1 and lastbull7 == -1, lastdiff7, if lastdiff7 > 0 then Color.GREEN else Color.RED);