# scanning for price above EMA for certain number of price bars

#### vrinx3547

##### New member
How can I scan for the price bars closing above a certain EMA for a certain number of price bars in thinkorswim?

EXAMPLE: scanning for price has closed above the 80 ema for 20 price bars.

Thank you.

@vrinx3547 For that you would test for NOT or "!" close crosses below the 80ema within 20 bars...

@vrinx3547 For that you would test for NOT or "!" close crosses below the 80ema within 20 bars...

This will also find symbols that have been below the ema longer than 20 bars. You need to also check that the current close is above the EMA.

i didn't try this in a scan, but i think it will work. it works on a column and on a chart.

possible scan code , (not tested)
Code:
``````input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
input barsback = 20;
plot abovexbars = ( Sum( (close > ma1), barsback ) == barsback );``````

a column study , that shows for how many bars, close has been above ema for x bars.
zclsaboveema1
http://tos.mx/3smi07W

Code:
``````# zclsaboveema1
# priceaboveEMA_xbars_01 - upper
def bn = barnumber();
input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
input barsback = 20;
def abovexbars = ( Sum( (close > ma1), barsback ) == barsback );

def xbarscnt = if bn == 1 then 0 else if !abovexbars then 0 else if (abovexbars and !abovexbars[1] ) then 1 else if abovexbars then xbarscnt[1] + 1 else xbarscnt[1];

plot z = xbarscnt;
z.setdefaultcolor(color.dark_gray);

assignbackgroundcolor( if abovexbars then color.yellow else color.current);
#``````

a chart version for testing. draws a count of bar numbers when close is > ema. after x bars of being above, it draws yellow down arrows.

Code:
``````# priceaboveEMA_xbars_01

# How can I scan for the price bars closing above a certain EMA for a certain number of price bars in thinkorswim?
# EXAMPLE: scanning for price has closed above the 80 ema for 20 price bars.

# https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
#  EXPONENTIAL
#  HULL
#  SIMPLE
#  WEIGHTED
#  WILDERS

# average line
def na = double.nan;
def bn = barnumber();

input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
plot avg = ma1;
avg.SetDefaultColor(Color.cyan);

# plot down arrow when close > ema, for the past x bars
input barsback = 20;
def abovexbars = ( Sum( (close > ma1), barsback ) == barsback );
plot x = if abovexbars then high else na;
x.SetPaintingStrategy(PaintingStrategy.ARROW_down);
x.SetDefaultColor(Color.yellow);

# display bar counts
def clsabove = (close > ma1);
def cnt = if bn == 1 then 0 else if !clsabove then 0 else if (clsabove and !clsabove[1] ) then 1 else if clsabove then cnt[1] + 1 else cnt[1];
plot num = if (cnt >0) then cnt else na;
num.SetPaintingStrategy(PaintingStrategy.VALUES_below);
num.SetDefaultColor(Color.light_gray);
#``````

i didn't try this in a scan, but i think it will work. it works on a column and on a chart.

possible scan code , (not tested)
Code:
``````input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
input barsback = 20;
plot abovexbars = ( Sum( (close > ma1), barsback ) == barsback );``````

a column study , that shows for how many bars, close has been above ema for x bars.
zclsaboveema1
http://tos.mx/3smi07W

Code:
``````# zclsaboveema1
# priceaboveEMA_xbars_01 - upper
def bn = barnumber();
input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
input barsback = 20;
def abovexbars = ( Sum( (close > ma1), barsback ) == barsback );

def xbarscnt = if bn == 1 then 0 else if !abovexbars then 0 else if (abovexbars and !abovexbars[1] ) then 1 else if abovexbars then xbarscnt[1] + 1 else xbarscnt[1];

plot z = xbarscnt;
z.setdefaultcolor(color.dark_gray);

assignbackgroundcolor( if abovexbars then color.yellow else color.current);
#``````

a chart version for testing. draws a count of bar numbers when close is > ema. after x bars of being above, it draws yellow down arrows.

Code:
``````# priceaboveEMA_xbars_01

# How can I scan for the price bars closing above a certain EMA for a certain number of price bars in thinkorswim?
# EXAMPLE: scanning for price has closed above the 80 ema for 20 price bars.

# https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
#  EXPONENTIAL
#  HULL
#  SIMPLE
#  WEIGHTED
#  WILDERS

# average line
def na = double.nan;
def bn = barnumber();

input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
plot avg = ma1;
avg.SetDefaultColor(Color.cyan);

# plot down arrow when close > ema, for the past x bars
input barsback = 20;
def abovexbars = ( Sum( (close > ma1), barsback ) == barsback );
plot x = if abovexbars then high else na;
x.SetPaintingStrategy(PaintingStrategy.ARROW_down);
x.SetDefaultColor(Color.yellow);

# display bar counts
def clsabove = (close > ma1);
def cnt = if bn == 1 then 0 else if !clsabove then 0 else if (clsabove and !clsabove[1] ) then 1 else if clsabove then cnt[1] + 1 else cnt[1];
plot num = if (cnt >0) then cnt else na;
num.SetPaintingStrategy(PaintingStrategy.VALUES_below);
num.SetDefaultColor(Color.light_gray);
#``````

Thank you for this code. Very much appreciated.

Last edited:
i didn't try this in a scan, but i think it will work. it works on a column and on a chart.

possible scan code , (not tested)
Code:
``````input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
input barsback = 20;
plot abovexbars = ( Sum( (close > ma1), barsback ) == barsback );``````

a column study , that shows for how many bars, close has been above ema for x bars.
zclsaboveema1
http://tos.mx/3smi07W

Code:
``````# zclsaboveema1
# priceaboveEMA_xbars_01 - upper
def bn = barnumber();
input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
input barsback = 20;
def abovexbars = ( Sum( (close > ma1), barsback ) == barsback );

def xbarscnt = if bn == 1 then 0 else if !abovexbars then 0 else if (abovexbars and !abovexbars[1] ) then 1 else if abovexbars then xbarscnt[1] + 1 else xbarscnt[1];

plot z = xbarscnt;
z.setdefaultcolor(color.dark_gray);

assignbackgroundcolor( if abovexbars then color.yellow else color.current);
#``````

a chart version for testing. draws a count of bar numbers when close is > ema. after x bars of being above, it draws yellow down arrows.

Code:
``````# priceaboveEMA_xbars_01

# How can I scan for the price bars closing above a certain EMA for a certain number of price bars in thinkorswim?
# EXAMPLE: scanning for price has closed above the 80 ema for 20 price bars.

# https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
#  EXPONENTIAL
#  HULL
#  SIMPLE
#  WEIGHTED
#  WILDERS

# average line
def na = double.nan;
def bn = barnumber();

input avg1_len = 80;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
plot avg = ma1;
avg.SetDefaultColor(Color.cyan);

# plot down arrow when close > ema, for the past x bars
input barsback = 20;
def abovexbars = ( Sum( (close > ma1), barsback ) == barsback );
plot x = if abovexbars then high else na;
x.SetPaintingStrategy(PaintingStrategy.ARROW_down);
x.SetDefaultColor(Color.yellow);

# display bar counts
def clsabove = (close > ma1);
def cnt = if bn == 1 then 0 else if !clsabove then 0 else if (clsabove and !clsabove[1] ) then 1 else if clsabove then cnt[1] + 1 else cnt[1];
plot num = if (cnt >0) then cnt else na;
num.SetPaintingStrategy(PaintingStrategy.VALUES_below);
num.SetDefaultColor(Color.light_gray);
#``````

Hey, sorry to ask this of you, but I've changed the script slightly to count the number above a 10EMA close, but now I'm having trouble reversing it to count the number of closes beneath the 10EMA for going short, but it keeps equaling 0 or if edited it equals an outrageously high number.

def bn = barnumber();
input barsback = 3;
def abovexbars = (sum (close < movavgexponential(close,10), barsback ) == barsback );

def xbarscnt = if bn == 1 then 0 else if !abovexbars then 0 else if (abovexbars and !abovexbars[1] ) then 1 else if abovexbars then xbarscnt[1] + 1 else xbarscnt[1];

plot z = xbarscnt;
z.setdefaultcolor(color.white);

#assignbackgroundcolor( if abovexbars then color.red else color.current);

87k+ Posts
306 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?