# sma200_approach_upper
#https://usethinkscript.com/threads/price-cross-150ma-moving-to-200-ma.19236/
#price cross 150ma -- moving to 200 ma
#declare lower;
def na = double.nan;
def bn = barnumber();
def data = close;
def up = close > open;
def dwn = close < open;
input avg1_type = AverageType.Simple;
input avg1_length = 150;
def avg1 = MovingAverage(avg1_type, data, avg1_length );
input avg2_type = AverageType.Simple;
input avg2_length = 200;
def avg2 = MovingAverage(avg2_type, data, avg2_length );
def xup = avg1 crosses above avg2;
def xdwn = avg1 crosses below avg2;
def pr_xup150 = close crosses above avg1;
def pr_xdwn150 = close crosses below avg1;
def x = pr_xup150 or pr_xdwn150;
# price chg
def avg3_type = AverageType.exponential;
def avg3_length = 5;
def avg3 = MovingAverage(avg3_type, data, avg3_length );
#plot z = avg3;
def t = 2;
def chgper = round(100 * (avg3 - avg3[t])/avg3[t],2);
# min chg %
def minper = 0.18;
def triggerup = chgper > minper and pr_xup150;
#plot z = triggerup;
#----------------------------------
# chart version
input show_arrow = yes;
plot zup = if show_arrow and triggerup then low else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zup.SetDefaultColor(Color.cyan);
zup.setlineweight(3);
zup.hidebubble();
input test_bubble = yes;
addchartbubble(test_bubble,low*0.998,
chgper
, (if triggerup then color.yellow else color.gray), no);
input show_avg1_line = yes;
input show_avg2_line = yes;
plot zavg1 = if show_avg1_line then avg1 else na;
plot zavg2 = if show_avg2_line then avg2 else na;
zavg1.SetDefaultColor(Color.cyan);
zavg1.setlineweight(1);
zavg1.hidebubble();
zavg2.SetDefaultColor(Color.yellow);
zavg2.setlineweight(1);
zavg2.hidebubble();
addlabel(show_avg1_line, "MA"+avg1_length , zavg1.takeValueColor());
addlabel(show_avg2_line, "MA"+avg2_length , zavg2.takeValueColor());
#