```
# price_diff_emas_00_lower
# https://usethinkscript.com/threads/show-the-difference-between-candle-close-and-the-ema-lines.14254/
# Show the difference between candle close and the EMA lines
declare lower;
def na = double.nan;
def bn = barnumber();
# last bar ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
def price = close;
input ma1_len = 8;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);
input ma2_len = 20;
input ma2_type = AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);
input ma3_len = 50;
input ma3_type = AverageType.EXPONENTIAL;
def ma3 = MovingAverage(ma3_type, price, ma3_len);
input ma4_len = 1;
input ma4_type = AverageType.EXPONENTIAL;
def ma4 = MovingAverage(ma4_type, price, ma4_len);
def en1 = ma1_len > 1;
def en2 = ma2_len > 1;
def en3 = ma3_len > 1;
def en4 = ma4_len > 1;
#input show_average_lines = no;
#plot z1 = if show_average_lines and en1 then ma1 else na;
#z1.setdefaultcolor(getcolor(1));
##z1.setlineweight(1);
#z1.hidebubble();
#plot z2 = if show_average_lines and en2 then ma2 else na;
#z2.setdefaultcolor(getcolor(2));
##z2.setlineweight(1);
#z2.hidebubble();
#plot z3 = if show_average_lines and en3 then ma3 else na;
#z3.setdefaultcolor(getcolor(3));
##z3.setlineweight(1);
#z3.hidebubble();
#plot z4 = if show_average_lines and en4 then ma4 else na;
#z4.setdefaultcolor(getcolor(4));
##z4.setlineweight(1);
#z4.hidebubble();
def diff1b = round(close - ma1, 2);
def diff2b = round(close - ma2, 2);
def diff3b = round(close - ma3, 2);
def diff4b = round(close - ma4, 2);
def diff1 = absvalue(diff1b);
def diff2 = absvalue(diff2b);
def diff3 = absvalue(diff3b);
def diff4 = absvalue(diff4b);
def max1 = en1 and (diff1 > diff2 and diff1 > diff3 and diff1 > diff4);
def max2 = en2 and (diff2 > diff1 and diff2 > diff3 and diff2 > diff4);
def max3 = en3 and (diff3 > diff1 and diff3 > diff2 and diff3 > diff4);
def max4 = en4 and (diff4 > diff1 and diff4 > diff2 and diff4 > diff3);
#addlabel(en1, "MA"+ ma1_len + " diff " + diff1, getcolor(1));
#addlabel(en2, "MA"+ ma2_len + " diff " + diff2, getcolor(2));
#addlabel(en3, "MA"+ ma3_len + " diff " + diff3, getcolor(3));
#addlabel(en4, "MA"+ ma4_len + " diff " + diff4, getcolor(4));
addlabel(1,
(if en1 then "|" + diff1b else "") +
(if en2 then "|" + diff2b else "") +
(if en3 then "|" + diff3b else "") +
(if en4 then "|" + diff4b else "") + "|"
, color.black);
#addlabel(1,
#(if en1 then " | MA"+ ma1_len + " " + diff1b else "") +
#(if en2 then " | MA"+ ma2_len + " " + diff2b else "") +
#(if en3 then " | MA"+ ma3_len + " " + diff3b else "") +
#(if en4 then " | MA"+ ma4_len + " " + diff4b else "") + " |"
#, color.black);
AssignBackgroundColor(
#if 1 then color.current else
if max1 then getcolor(1)
else if max2 then getcolor(2)
else if max3 then getcolor(3)
else if max4 then getcolor(4)
else color.white);
addlabel(1,
max1+"|"+
max2+"|"+
max3+"|"+
max4
, color.blue);
#
```