@vrinx3547 For that you would test for NOT or "!" close crosses below the 80ema within 20 bars...
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 );
# 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);
#
# 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);
#
Thank you for this code. Very much appreciated.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); #
![]()
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); #
![]()
Can someone please help out to come up with an script to add the numbers from different time frames when the scanner
# 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.black);
assignbackgroundcolor( if abovexbars then color.green else color.current);
#
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
B | scanning for price | Questions | 4 | |
A | Scanning for 100% more Volume than previous period | Questions | 1 | |
P | Scanning for News | Questions | 1 | |
![]() |
Need help with scanning Volume withing 5 bars | Questions | 1 | |
![]() |
Help scanning for: lastClose > sma20 * 1.5 | Questions | 1 |
Start a new thread and receive assistance from our community.
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.
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.