#avgs_converge_label
#https://usethinkscript.com/threads/moving-averages-converge-diverge-label.19643/
#WMA 8 and 16 label?
def na = Double.NaN;
def bn = BarNumber();
DefineGlobalColor("converge_dwn", color.green);
DefineGlobalColor("diverge_dwn", color.white);
DefineGlobalColor("converge_up", color.yellow);
DefineGlobalColor("diverge_up", color.cyan);
#GlobalColor("converge_dwn")
#GlobalColor("diverge_dwn")
#GlobalColor("converge_up")
#GlobalColor("diverge_up")
#input avg1_type = AverageType.exponential;
input avg1_type = AverageType.WEIGHTED;
input avg1_length = 8;
def avg1 = MovingAverage(avg1_type, close, avg1_length );
input avg2_type = AverageType.WEIGHTED;
input avg2_length = 16;
def avg2 = MovingAverage(avg2_type, close, avg2_length );
def xup = avg1 crosses above avg2;
def xdwn = avg1 crosses below avg2;
def gap = avg1 - avg2;
# WMA 8 and 16 are converging after a drop
def converge_dwn = avg1 < avg2 and absvalue(gap[0]) < absvalue(gap[1]);
def diverge_dwn = avg1 < avg2 and absvalue(gap[0]) > absvalue(gap[1]);
def converge_up = avg1 > avg2 and absvalue(gap[0]) < absvalue(gap[1]);
def diverge_up = avg1 > avg2 and absvalue(gap[0]) > absvalue(gap[1]);
addlabel(1, " ", color.black);
addlabel(converge_dwn, "converge down", GlobalColor("converge_dwn"));
addlabel(diverge_dwn, "diverge down", GlobalColor("diverge_dwn"));
addlabel(converge_up, "converge up", GlobalColor("converge_up"));
addlabel(diverge_up, "diverge up", GlobalColor("diverge_up"));
input converge_dwn_dots = yes;
plot cd = if converge_dwn_dots and converge_dwn then low*0.999 else na;
cd.SetPaintingStrategy(PaintingStrategy.POINTS);
cd.SetDefaultColor(GlobalColor("converge_dwn"));
cd.setlineweight(3);
cd.hidebubble();
input diverge_dwn_dots = yes;
plot dd = if diverge_dwn_dots and diverge_dwn then low*0.999 else na;
dd.SetPaintingStrategy(PaintingStrategy.POINTS);
dd.SetDefaultColor(GlobalColor("diverge_dwn"));
dd.setlineweight(3);
dd.hidebubble();
input converge_up_dots = yes;
plot cu = if converge_up_dots and converge_up then high*1.001 else na;
cu.SetPaintingStrategy(PaintingStrategy.POINTS);
cu.SetDefaultColor(GlobalColor("converge_up"));
cu.setlineweight(3);
cu.hidebubble();
input diverge_up_dots = yes;
plot du = if diverge_up_dots and diverge_up then high*1.001 else na;
du.SetPaintingStrategy(PaintingStrategy.POINTS);
du.SetDefaultColor(GlobalColor("diverge_up"));
du.setlineweight(3);
du.hidebubble();
input show_avg_lines = yes;
plot zavg1 = if show_avg_lines then avg1 else na;
plot zavg2 = if show_avg_lines then avg2 else na;
zavg1.SetDefaultColor(Color.CYAN);
zavg1.SetLineWeight(1);
zavg1.HideBubble();
zavg2.SetDefaultColor(Color.YELLOW);
zavg2.SetLineWeight(1);
zavg2.HideBubble();
#----------------------------
input test_vert_lines = no;
addverticalline(test_vert_lines and converge_dwn, "-", GlobalColor("converge_dwn"));
addverticalline(test_vert_lines and diverge_dwn, "-", GlobalColor("diverge_dwn"));
addverticalline(test_vert_lines and converge_up, "-", GlobalColor("converge_up"));
addverticalline(test_vert_lines and diverge_up, "-", GlobalColor("diverge_up"));
#https://toslc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
#input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.hull;
#input avg1_type = AverageType.Simple;
#input avg1_type = AverageType.weighted; WMA()
#input avg1_type = AverageType.wilders;
#