i don't scan , so i don't make scans.
here is a lower that should work.
this looks for 2 crossings,
..ema5 crossing above ema20
..and ema5 crossing above ema50.
if the 2 crossings happen within x minutes, then the output is true.
can pick the max minutes between the crossings. default is 15.
set the chart/scan time to be less than the max minutes.
Code:
# three_ema_crossings_00_lower
#https://usethinkscript.com/threads/scanner-for-multipe-ema-crossover-5-20-50.15083/
#Scanner for Multipe EMA Crossover 5/20/50
declare lower;
def na = double.nan;
def bn = barnumber();
# last bar ( most recent)
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def valid = !isnan(close);
# emas ---------------------------------------
def price = close;
input MA1_len = 5;
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 show_avg_lines = no;
plot z1 = if show_avg_lines then ma1 else na;
#z1.SetDefaultColor(getcolor(1));
z1.SetDefaultColor(color.cyan);
#z1.setlineweight(1);
z1.hidebubble();
plot z2 = if show_avg_lines then ma2 else na;
z2.SetDefaultColor(color.yellow);
#z2.setlineweight(1);
z2.hidebubble();
plot z3 = if show_avg_lines then ma3 else na;
z3.SetDefaultColor(color.magenta);
#z3.setlineweight(1);
z3.hidebubble();
def up1x2 = ma1 crosses above ma2;
def up1x3 = ma1 crosses above ma3;
def dwn1x2 = ma1 crosses below ma2;
def dwn1x3 = ma1 crosses below ma3;
def up1x2bn = if bn == 1 then 0 else if up1x2 then bn else up1x2bn[1];
def up1x3bn = if bn == 1 then 0 else if up1x3 then bn else up1x3bn[1];
def dwn1x2bn = if bn == 1 then 0 else if dwn1x2 then bn else dwn1x2bn[1];
def dwn1x3bn = if bn == 1 then 0 else if dwn1x3 then bn else dwn1x3bn[1];
input max_minutes_between_crossings = 15;
#---------------------------------
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#---------------------------------
def barz = max_minutes_between_crossings / chartmin;
def buy = if (up1x3bn - up1x2bn) <= barz and bn == up1x3bn then 1 else 0;
#input show_yellow_warn_lines = no;
#addverticalline(show_yellow_warn_lines and up1x2, "buy-warn", color.yellow);
#addverticalline(buy, "buy", color.green);
plot z = buy;
#-------------------------------
addchartbubble(0, low*0.994,
bn + "\n" +
up1x3bn + " 13\n" +
up1x2bn + " 12\n" +
# chartmin + " min\n" +
barz + " max\n" +
buy + " B\n"
, color.yellow, no);
#
View attachment 18457
-------------------------------
here is an upper study for testing
Code:
# three_ema_crossings_00
#https://usethinkscript.com/threads/scanner-for-multipe-ema-crossover-5-20-50.15083/
#Scanner for Multipe EMA Crossover 5/20/50
def na = double.nan;
def bn = barnumber();
# last bar ( most recent)
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def valid = !isnan(close);
# emas ---------------------------------------
def price = close;
input MA1_len = 5;
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 show_avg_lines = yes;
plot z1 = if show_avg_lines then ma1 else na;
#z1.SetDefaultColor(getcolor(1));
z1.SetDefaultColor(color.cyan);
#z1.setlineweight(1);
z1.hidebubble();
plot z2 = if show_avg_lines then ma2 else na;
z2.SetDefaultColor(color.yellow);
#z2.setlineweight(1);
z2.hidebubble();
plot z3 = if show_avg_lines then ma3 else na;
z3.SetDefaultColor(color.magenta);
#z3.setlineweight(1);
z3.hidebubble();
def up1x2 = ma1 crosses above ma2;
def up1x3 = ma1 crosses above ma3;
def dwn1x2 = ma1 crosses below ma2;
def dwn1x3 = ma1 crosses below ma3;
def up1x2bn = if bn == 1 then 0 else if up1x2 then bn else up1x2bn[1];
def up1x3bn = if bn == 1 then 0 else if up1x3 then bn else up1x3bn[1];
def dwn1x2bn = if bn == 1 then 0 else if dwn1x2 then bn else dwn1x2bn[1];
def dwn1x3bn = if bn == 1 then 0 else if dwn1x3 then bn else dwn1x3bn[1];
input max_minutes_between_crossings = 15;
#---------------------------------
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#---------------------------------
def barz = max_minutes_between_crossings / chartmin;
def buy = if (up1x3bn - up1x2bn) <= barz and bn == up1x3bn then 1 else 0;
input show_yellow_warn_lines = no;
addverticalline(show_yellow_warn_lines and up1x2, "buy-warn", color.yellow);
addverticalline(buy, "buy", color.green);
#-------------------------------
addchartbubble(0, low*0.994,
bn + "\n" +
up1x3bn + " 13\n" +
up1x2bn + " 12\n" +
# chartmin + " min\n" +
barz + " max\n" +
buy + " B\n"
, color.yellow, no);
#