input limitplot = yes;
input limit_x_plots = 1;
input pricecolor = yes;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = Value - Avg;
Diff.Hide();
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"));
AssignPriceColor(if !pricecolor then Color.CURRENT else Diff.TakeValueColor());
def upcond = if Diff crosses above 0 then 1 else 0;
def upcount = if BarNumber() == 1 then 0 else if upcond then upcount[1] + 1 else upcount[1];
plot UpSignal = if limitplot and HighestAll(upcount) - upcount <= limit_x_plots - 1 then upcond else if !limitplot then upcond else Double.NaN;
AddChartBubble(UpSignal, low, "Buy", Color.LIGHT_GREEN, no);
def dncond = if Diff crosses below 0 then 1 else 0;
def dncount = if BarNumber() == 1 then 0 else if (dncond) then dncount[1] + 1 else dncount[1];
plot downSignal = if limitplot and HighestAll(dncount) - dncount <= limit_x_plots - 1 then dncond else if !limitplot then dncond else Double.NaN;
AddChartBubble(downSignal, high, "Sell", Color.LIGHT_RED, yes);
#