Scanner for Multipe EMA Crossover 5/20/50

Go_Big

New member
Hello usethinkscript community!

I would like to get help with coding for creating a script that combines the 5/20 ema crossing above and below the 50 ema to scan for potential breakouts during premarket and during the regular trading session. I am specifically looking for all three moving averages to cross at the same point in time or at least within 15 minutes. I like the 5 and 15 minute timeframes to spot early entries into big movers. Thanks again.

Go Big and Godspeed
 
Solution
Hello usethinkscript community!

I would like to get help with coding for creating a script that combines the 5/20 ema crossing above and below the 50 ema to scan for potential breakouts during premarket and during the regular trading session. I am specifically looking for all three moving averages to cross at the same point in time or at least within 15 minutes. I like the 5 and 15 minute timeframes to spot early entries into big movers. Thanks again.

Go Big and Godspeed


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...
Hello usethinkscript community!

I would like to get help with coding for creating a script that combines the 5/20 ema crossing above and below the 50 ema to scan for potential breakouts during premarket and during the regular trading session. I am specifically looking for all three moving averages to cross at the same point in time or at least within 15 minutes. I like the 5 and 15 minute timeframes to spot early entries into big movers. Thanks again.

Go Big and Godspeed


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);
#


bNNOh48.jpg



-------------------------------


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);
#
 
Solution
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);
#
I love your upper study but I haven't been able to modify it for SELL signals. How could I?
 
First time I try any coding (if you can call it so) but here is the modification of the original from @halcyonguy, I added warn and sell signals for shorting and the option to show warn-sell or not. I hope I'm posting this correctly:

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);

#-------------------------------


def sell = if (dwn1x3bn - dwn1x2bn) <= barz and bn == dwn1x3bn then 1 else 0;

input show_orange_warn_lines = no;
AddVerticalLine(show_orange_warn_lines and dwn1x2, "sell-warn", Color.ORANGE);
AddVerticalLine(sell, "sell", Color.RED);

#-------------------------------

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);
#
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
636 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top