# find_closest_value_to_close_00
#https://usethinkscript.com/threads/label-to-show-next-support-or-resistance-closest-to-last-price.13943/
#Label to show next support or resistance closest to last price
#def x = Min(close, Lowest (close, 20));
#def y = Min(close, Lowest (close, 63));
#def a = Min(close, Highest (close, 20));
#def b = Min(close, Highest (close, 63));
#def c = Min(close, MovingAverage(AverageType.SIMPLE, AggregationPeriod.WEEK, 200));
#def d = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.WEEK, 21));
#def e = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.WEEK, 5));
#def f = Min(close, MovingAverage(AverageType.SIMPLE, AggregationPeriod.DAY, 200));
#def g = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.DAY, 21));
#def h = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.DAY, 5));
#-------------------
def na = double.nan;
def cls = close;
def aggw = AggregationPeriod.WEEK;
def aggd = AggregationPeriod.DAY;
def a = Lowest(close, 20);
def b = Lowest(close, 63);
def c = Highest(close, 20);
def d = Highest(close, 63);
def e = MovingAverage(AverageType.SIMPLE, close(period = aggw), 200);
def f = MovingAverage(AverageType.EXPONENTIAL, close(period = aggw), 21);
def g = MovingAverage(AverageType.EXPONENTIAL, close(period = aggw), 5);
def h = MovingAverage(AverageType.SIMPLE, close(period = aggd), 200);
def i = MovingAverage(AverageType.EXPONENTIAL, close(period = aggd), 21);
def j = MovingAverage(AverageType.EXPONENTIAL, close(period = aggd), 5);
# signal diff to close
def a2 = (a - cls);
def b2 = (b - cls);
def c2 = (c - cls);
def d2 = (d - cls);
def e2 = (e - cls);
def f2 = (f - cls);
def g2 = (g - cls);
def h2 = (h - cls);
def i2 = (i - cls);
def j2 = (j - cls);
def y = 1000;
# find signal closest and above close
# above , up
def au = if a >= cls then a2 else y;
def bu = if b >= cls then b2 else y;
def cu = if c >= cls then c2 else y;
def du = if d >= cls then d2 else y;
def eu = if e >= cls then e2 else y;
def fu = if f >= cls then f2 else y;
def gu = if g >= cls then g2 else y;
def hu = if h >= cls then h2 else y;
def iu = if i >= cls then i2 else y;
def ju = if j >= cls then j2 else y;
def abovemin = Min(au, Min(bu, Min(cu, Min(du, Min(eu, Min(fu, Min(gu, Min(hu, Min(iu, ju)))))))));
def minup;
if au == abovemin then {
minup = 1;
} else if bu == abovemin then {
minup = 2;
} else if cu == abovemin then {
minup = 3;
} else if du == abovemin then {
minup = 4;
} else if eu == abovemin then {
minup = 5;
} else if fu == abovemin then {
minup = 6;
} else if gu == abovemin then {
minup = 7;
} else if hu == abovemin then {
minup = 8;
} else if iu == abovemin then {
minup = 9;
} else if ju == abovemin then {
minup = 10;
} else {
minup = 99;
}
#---------------------------
# find signal closest and below
# below , down
def ad = if a <= cls then absvalue(a2) else y;
def bd = if b <= cls then absvalue(b2) else y;
def cd = if c <= cls then absvalue(c2) else y;
def dd = if d <= cls then absvalue(d2) else y;
def ed = if e <= cls then absvalue(e2) else y;
def fd = if f <= cls then absvalue(f2) else y;
def gd = if g <= cls then absvalue(g2) else y;
def hd = if h <= cls then absvalue(h2) else y;
def id = if i <= cls then absvalue(i2) else y;
def jd = if j <= cls then absvalue(j2) else y;
def belowmin = Min(ad, Min(bd, Min(cd, Min(dd, Min(ed, Min(fd, Min(gd, Min(hd, Min(id, jd)))))))));
#--------------------------
def mindwn;
if ad == belowmin then {
mindwn = 1;
} else if bd == belowmin then {
mindwn = 2;
} else if cd == belowmin then {
mindwn = 3;
} else if dd == belowmin then {
mindwn = 4;
} else if ed == belowmin then {
mindwn = 5;
} else if fd == belowmin then {
mindwn = 6;
} else if gd == belowmin then {
mindwn = 7;
} else if hd == belowmin then {
mindwn = 8;
} else if id == belowmin then {
mindwn = 9;
} else if jd == belowmin then {
mindwn = 10;
} else {
mindwn = 99;
}
#---------------------------
addlabel(1, " ", color.black);
addlabel(1, abovemin + " closest above, signal # " + minup, color.green);
addlabel(1, belowmin + " closest below, signal # " + mindwn, color.red);
#---------------------------
#---------------------------
addlabel(1, " ", color.black);
input test_values = yes;
addlabel(test_values, " price differences ", color.cyan);
addlabel(test_values, a2, color.cyan);
addlabel(test_values, b2, color.cyan);
addlabel(test_values, c2, color.cyan);
addlabel(test_values, d2, color.cyan);
addlabel(test_values, e2, color.cyan);
addlabel(test_values, f2, color.cyan);
addlabel(test_values, g2, color.cyan);
addlabel(test_values, h2, color.cyan);
addlabel(test_values, i2, color.cyan);
addlabel(test_values, j2, color.cyan);
addlabel(1, " ", color.black);
input test_adj_values = yes;
addlabel(test_adj_values, "values above close", color.lime);
addlabel(test_adj_values, au, color.lime);
addlabel(test_adj_values, bu, color.lime);
addlabel(test_adj_values, cu, color.lime);
addlabel(test_adj_values, du, color.lime);
addlabel(test_adj_values, eu, color.lime);
addlabel(test_adj_values, fu, color.lime);
addlabel(test_adj_values, gu, color.lime);
addlabel(test_adj_values, hu, color.lime);
addlabel(test_adj_values, iu, color.lime);
addlabel(test_adj_values, ju, color.lime);
addlabel(1, " ", color.black);
addlabel(1, " ", color.black);
addlabel(test_adj_values, "values below close", color.magenta);
addlabel(test_adj_values, ad, color.magenta);
addlabel(test_adj_values, bd, color.magenta);
addlabel(test_adj_values, cd, color.magenta);
addlabel(test_adj_values, dd, color.magenta);
addlabel(test_adj_values, ed, color.magenta);
addlabel(test_adj_values, fd, color.magenta);
addlabel(test_adj_values, gd, color.magenta);
addlabel(test_adj_values, hd, color.magenta);
addlabel(test_adj_values, id, color.magenta);
addlabel(test_adj_values, jd, color.magenta);
addlabel(1, " ", color.black);
# plot signals
input show_signals = no;
plot z1 = if show_signals then a else na;
plot z2 = if show_signals then b else na;
plot z3 = if show_signals then c else na;
plot z4 = if show_signals then d else na;
plot z5 = if show_signals then e else na;
plot z6 = if show_signals then f else na;
plot z7 = if show_signals then g else na;
plot z8 = if show_signals then h else na;
plot z9 = if show_signals then i else na;
plot z10 = if show_signals then j else na;
#