#hint:Looks for and indicates positive (price rising) and negative (price declining) divergences of price and the MACD
Input Bars = 3;#Hint Bars:The number of bars in the pattern to evaluate
def mcdv = MACD();
def MACD_rising = sum(mcdv > mcdv[1],bars) == bars;#True if MACD is rising for the last number-of-bars (Bars)
def price_rising = sum(close > close[1],bars) == bars;#True if close is rising for the last number-of-bars (Bars)
def MACD_falling = sum(mcdv < mcdv[1],bars) == bars;#True if MACD is falling for the last number-of-bars (Bars)
def price_falling = sum(close < close[1],bars) == bars;#True if close is falling for the last number-of-bars (Bars)
def pos_div = price_rising && MACD_falling;
def neg_div = price_falling && MACD_rising;
AddLabel(1, if pos_div then "pos" else if neg_div then "neg" else "none", if pos_div == 1 then color.green else if neg_div == 1 then color.red else color.pink );
AssignBackgroundColor(if pos_div then color.dark_green
else if neg_div then
color.dark_RED
else color.current);#Divergence with MACD rising & price falling is RED. The opposite is GREEN. Neither is the normal
color.
#=====to prove out the code or to use this as a study, plot the below
# and evaluate that the conditions and results are correct ========
plot Positive = pos_div;# A CYAN value of 'one' plots where true exists
Plot Negative = neg_div;# A PINK value of 'one' plots where true exists
#==================================================================
#end