#price_cross_ema
#https://usethinkscript.com/threads/study-for-ema-21.17116/
# touching ema 21
def na = double.nan;
def t = ticksize();
input tick_factor = 2;
def f = t * tick_factor;
addlabel(1, "+- range " + f, color.yellow);
input bar_part_cross_avg_type = { default highorlow , any };
#input avg_type = AverageType.Simple;
input avg_type = AverageType.exponential;
input avg_data = close;
input avg_len = 21;
def avg = MovingAverage( avg_type , avg_data , avg_len );
plot zavg = avg;
def x;
def a;
switch( bar_part_cross_avg_type) {
case any:
a = 1;
x = if high >= avg and low <= avg then 1 else 0;
case highorlow:
a = 2;
x = if (high >= avg and (high - f) <= avg) or ((low + f) >= avg and (low <= avg)) then 1 else 0;
}
plot zcross1 = if x and a == 1 then high*1.004
else na;
zcross1.SetPaintingStrategy(PaintingStrategy.triangles);
zcross1.SetDefaultColor(Color.cyan);
zcross1.setlineweight(5);
zcross1.hidebubble();
plot zcross2 = if x and a == 2 and ((high - f) <= avg) then high*1.002
else if x and a == 2 and ((low + f) >= avg) then low*0.998
else na;
zcross2.SetPaintingStrategy(PaintingStrategy.POINTS);
zcross2.SetDefaultColor(Color.yellow);
zcross2.setlineweight(5);
zcross2.hidebubble();
#----------------------
input test1_diff = no;
addchartbubble(test1_diff, low*0.996,
(high - avg) + "\n" +
(low - avg)
, color.yellow, no);
addverticalline(0 and x, "-", color.yellow);
#