Scan for stocks above 50 SMA for the last X days?


Active member
2019 Donor
Hi all, i tried to convert these codes into a TOS scan but received an error message, "too complex exceptions......

May i request some help to convert this into a scan.

declare lower;

input days_above_50ma = 125;
def mystery = AbsValue(close>average(close,50)) + AbsValue(close[1]>average(close,50)[1]) + AbsValue(close[2]>average(close,50)[2]) + AbsValue(close[3]>average(close,50)[3]) +
AbsValue(close[4]>average(close,50)[4]) + AbsValue(close[5]>average(close,50)[5]) + AbsValue(close[6]>average(close,50)[6]) + AbsValue(close[7]>average(close,50)[7]) +
AbsValue(close[8]>average(close,50)[8]) + AbsValue(close[9]>average(close,50)[9]) + AbsValue(close[10]>average(close,50)[10]) + AbsValue(close[11]>average(close,50)[11]) +
AbsValue(close[12]>average(close,50)[12]) + AbsValue(close[13]>average(close,50)[13]) + AbsValue(close[14]>average(close,50)[14]) + AbsValue(close[15]>average(close,50)[15]) +
AbsValue(close[16]>average(close,50)[16]) + AbsValue(close[17]>average(close,50)[17]) + AbsValue(close[18]>average(close,50)[18]) + AbsValue(close[19]>average(close,50)[19]) +
AbsValue(close[20]>average(close,50)[20]) + AbsValue(close[21]>average(close,50)[21]) + AbsValue(close[22]>average(close,50)[22]) + AbsValue(close[23]>average(close,50)[23]) +
AbsValue(close[24]>average(close,50)[24]) + AbsValue(close[25]>average(close,50)[25]) + AbsValue(close[26]>average(close,50)[26]) + AbsValue(close[27]>average(close,50)[27]) +
AbsValue(close[28]>average(close,50)[28]) + AbsValue(close[29]>average(close,50)[29]) + AbsValue(close[30]>average(close,50)[30]) + AbsValue(close[31]>average(close,50)[31]) +
AbsValue(close[32]>average(close,50)[32]) + AbsValue(close[33]>average(close,50)[33]) + AbsValue(close[34]>average(close,50)[34]) + AbsValue(close[35]>average(close,50)[35]) +
AbsValue(close[36]>average(close,50)[36]) + AbsValue(close[37]>average(close,50)[37]) + AbsValue(close[38]>average(close,50)[38]) + AbsValue(close[39]>average(close,50)[39]) +
AbsValue(close[40]>average(close,50)[40]) + AbsValue(close[41]>average(close,50)[41]) + AbsValue(close[42]>average(close,50)[42]) + AbsValue(close[43]>average(close,50)[43]) +
AbsValue(close[44]>average(close,50)[44]) + AbsValue(close[45]>average(close,50)[45]) + AbsValue(close[46]>average(close,50)[46]) + AbsValue(close[47]>average(close,50)[47]) +
AbsValue(close[48]>average(close,50)[48]) + AbsValue(close[49]>average(close,50)[49]) + AbsValue(close[50]>average(close,50)[50]) + AbsValue(close[51]>average(close,50)[51]) +
AbsValue(close[52]>average(close,50)[52]) + AbsValue(close[53]>average(close,50)[53]) + AbsValue(close[54]>average(close,50)[54]) + AbsValue(close[55]>average(close,50)[55]) +
AbsValue(close[56]>average(close,50)[56]) + AbsValue(close[57]>average(close,50)[57]) + AbsValue(close[58]>average(close,50)[58]) + AbsValue(close[59]>average(close,50)[59]) +
AbsValue(close[60]>average(close,50)[60]) + AbsValue(close[61]>average(close,50)[61]) + AbsValue(close[62]>average(close,50)[62]) + AbsValue(close[63]>average(close,50)[63]) +
AbsValue(close[64]>average(close,50)[64]) + AbsValue(close[65]>average(close,50)[65]) + AbsValue(close[66]>average(close,50)[66]) + AbsValue(close[67]>average(close,50)[67]) +
AbsValue(close[68]>average(close,50)[68]) + AbsValue(close[69]>average(close,50)[69]) + AbsValue(close[70]>average(close,50)[70]) + AbsValue(close[71]>average(close,50)[71]) +
AbsValue(close[72]>average(close,50)[72]) + AbsValue(close[73]>average(close,50)[73]) + AbsValue(close[74]>average(close,50)[74]) + AbsValue(close[75]>average(close,50)[75]) +
AbsValue(close[76]>average(close,50)[76]) + AbsValue(close[77]>average(close,50)[77]) + AbsValue(close[78]>average(close,50)[78]) + AbsValue(close[79]>average(close,50)[79]) +
AbsValue(close[80]>average(close,50)[80]) + AbsValue(close[81]>average(close,50)[81]) + AbsValue(close[82]>average(close,50)[82]) + AbsValue(close[83]>average(close,50)[83]) +
AbsValue(close[84]>average(close,50)[84]) + AbsValue(close[85]>average(close,50)[85]) + AbsValue(close[86]>average(close,50)[86]) + AbsValue(close[87]>average(close,50)[87]) +
AbsValue(close[88]>average(close,50)[88]) + AbsValue(close[89]>average(close,50)[89]) + AbsValue(close[90]>average(close,50)[90]) + AbsValue(close[91]>average(close,50)[91]) +
AbsValue(close[92]>average(close,50)[92]) + AbsValue(close[93]>average(close,50)[93]) + AbsValue(close[94]>average(close,50)[94]) + AbsValue(close[95]>average(close,50)[95]) +
AbsValue(close[96]>average(close,50)[96]) + AbsValue(close[97]>average(close,50)[97]) + AbsValue(close[98]>average(close,50)[98]) + AbsValue(close[99]>average(close,50)[99]) +
AbsValue(close[100]>average(close,50)[100]) + AbsValue(close[101]>average(close,50)[101]) + AbsValue(close[102]>average(close,50)[102]) +
AbsValue(close[103]>average(close,50)[103]) + AbsValue(close[104]>average(close,50)[104]) + AbsValue(close[105]>average(close,50)[105]) +
AbsValue(close[106]>average(close,50)[106]) + AbsValue(close[107]>average(close,50)[107]) + AbsValue(close[108]>average(close,50)[108]) +
AbsValue(close[109]>average(close,50)[109]) + AbsValue(close[110]>average(close,50)[110]) + AbsValue(close[111]>average(close,50)[111]) +
AbsValue(close[112]>average(close,50)[112]) + AbsValue(close[113]>average(close,50)[113]) + AbsValue(close[114]>average(close,50)[114]) +
AbsValue(close[115]>average(close,50)[115]) + AbsValue(close[116]>average(close,50)[116]) + AbsValue(close[117]>average(close,50)[117]) +
AbsValue(close[118]>average(close,50)[118]) + AbsValue(close[119]>average(close,50)[119]) + AbsValue(close[120]>average(close,50)[120]) +
AbsValue(close[121]>average(close,50)[121]) + AbsValue(close[122]>average(close,50)[122]) + AbsValue(close[123]>average(close,50)[123]) +

plot x = mystery >= days_above_50ma;

AddCloud(if x then 1 else Double.NaN,   if x then 0 else Double.NaN,,   Color.CURRENT);
Last edited by a moderator:


Well-known member
@Nick Gosh, what a huge complicated mess, for sure the scanner will undoubtedly complain. Checking your code, looks like all you really want to do is to scan for stocks that are ABOVE the 50 day SMA for the past 125 days.

Your entire code can be collapsed into one single statement. Place the following into the scanner. I just ran this scan successfully on the S&P 500 with no problems

sum (close > Average(close,50), 125) >= 125


Well-known member
@tomsk so to scan on a 5 min chart for any stocks above 50 SMA on that current day...the code would be this?

sum (close > Average(close,50), 1) >= 1

and below 50 SMA would be this?

sum (close < Average(close,50), 1) <= 1


Well-known member
First of all when you refer to scanning 50 SMA on a 5 min chart, I assume you mean 50 period SMA rather than the 50 day SMA. This is because the scanner does not permit secondary aggregations

That said if you want to scan for stocks that had just crossed above the 50 period SMA on that day for a 5 min aggregation, here is the scan

close crosses above Average(close,50)

The scan code for the close crosses below the 50 period SMA would thus be

close crosses below Average(close,50)

Remember to select the aggregation period of 5 mins
Last edited:


New member
@tomsk I tried the code:
sum (close > Average(close,50), 125) >= 125
but i changed it to hourly and the Average to (close,13), but I get no results.
here is the code I put:
Sum("data" = close > Average(close, 13), "length" = 21) >= 21
I want the stock to be above the 13 sma for at least 21 days.
I'm looking for stocks that's been trending and is about to crossover.

how I set my scanner is:
ask 30 and above
stock volume 1,000,000 and above
and then the code:
Sum("data" = close > Average(close, 13), "length" = 21) >= 21

Last edited:


New member
@BenTen I use 13/30 sma. should I use the code like this: Sum("data" = close > Average(close, 13), "length" = 21) >= 21. I use an hourly chart. I am looking for stock that have been trending for at least 21 days and is ready to crossover.
this is the code I would use to look for trending stock about to crossover:

Sum("data" = close > Average(close, 13), "length" = 21) >= 21

MovingAvgCrossover("length1" = 13, "crossing type" = "below")


Sum("data" = close < Average(close, 13), "length" = 21) >= 21


MovingAvgCrossover("length1" = 13, "crossing type" = "above")

but I still get no result.
Last edited:


New member
@BenTen I mean the stock been trading above the 13 sma for 21 days. The reason for 21 day is because I use an hourly chart and 125 days from the original code didn't work on the scanner when I change it to hourly aggregation. And the "ready to crossover" after the stock been trading above the 13 sma it crosses the 30. I have found stock that do this but only after I take out the code and just use the crossover study. it would take me at least an hour to weed out stocks that crossover but hasn't been clearly trading above or below the 13 sma.
Last edited:


Staff member
@Rambo Yes

  • Like
Reactions: RBO


1: I am trying to scan for price is above 21 Day EMA for at least 10 days instead of within 10 days.
2: Scan for 21 day EMA is above 50 SimpleMovingAverage for at least 5 days
I can only find within in the study options. If anyone would help me it would be greatly appreciated thanks
Last edited:


Well-known member
i made a tutorial thread on this

its simple, i have it in the tutorial, all you had to do was change the var to what you want to scan for, in this case you could of used to wizard to get that part which is
low is greater than MovAvgExponential("length" = 21)."AvgExp";

then you just plug it into the var

declare lower;
# By XeoNoX via
def var = low is greater than MovAvgExponential("length" = 21)."AvgExp";
def barUpCount = CompoundValue(1, if var then barUpCount[1] + 1 else 0, 0);
plot scan  = barUpCount;

then to scan for greater than 10 days, you set the agreegation to DAY and use the following scan code and replace the
"plot scan = barUpCount;"
with the code below
plot scan  = barUpCount > 10;

Ill let your try the 21 day EMA is above 50 SimpleMovingAverage for at least 5 days, let us see what you came up with.
remember to thumbs up if you found this post useful
Last edited:

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.