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.
 

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

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

# https://usethinkscript.com/threads/scanning-for-price-above-ema-for-certain-number-of-price-bars.7000/
# 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.
#AddLabel(1, "stay above ema", Color.YELLOW);

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


EWT9Q9C.jpg
 
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

# https://usethinkscript.com/threads/scanning-for-price-above-ema-for-certain-number-of-price-bars.7000/
# 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.
#AddLabel(1, "stay above ema", Color.YELLOW);

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


EWT9Q9C.jpg
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

# https://usethinkscript.com/threads/scanning-for-price-above-ema-for-certain-number-of-price-bars.7000/
# 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.
#AddLabel(1, "stay above ema", Color.YELLOW);

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


EWT9Q9C.jpg


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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
359 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