Best of s.c.a.n.

« Previous Article | Top | Next Article »

Best of s.c.a.n. is an ongoing collection of the the best responses to the questions submitted to our StockCharts Answer Network. Selected by our customer support team and s.c.a.n. editors, these responses are the answers to your questions that we wish we'd written ourselves.

If you have a question you'd like to ask the StockCharts community, please visit the s.c.a.n. homepage. You can create your s.c.a.n. account and submit the question from there.

The Max() Conundrum

Quick!  Without thinking about it very long - how would you write an advanced scan to find all stocks that are closed at a new 200-day high?  Wouldn't you write something like this?

[type = stock] and [today's high > max(200, high)]


That looks pretty good.  Let's run it and see what we get...

Picture 1  
Curses!  The dreaded "Count: 0" scan result has foiled me again.  But where did I go wrong?

The first possibility is that no stocks are hitting new 200-day highs.  While possible (especially in today's economy), that's not the situation today (March 16, 2009).  There's a functional problem with the way I defined the scan - can you spot it?

The problem is that "today's high" is included in the calculation of "max(200, high)".

Imagine a stock that really is hitting a new 200-day high of, let's say 100, right now.  In that case "today's high" equals 100 and "max(200,high)" also equals 100.  Substituting those values into the scan criteria, we're left with "100 > 100" which will always be false.  That's why we're getting zero results back all the time.

There are two ways to fix the problem:

1.) Change the ">" to "=":

[type = stock] and [today's high = max(200, high)]

2.) Modify the max() clause to start "yesterday":

[type = stock] and [today's high > yesterday's max(200, high)]

(Really observant people will notice that #2 needs a minor adjustment to fully match #1.  I'll leave that adjustment up to the reader as an exercise.)

To read other questions and answers or to submit your own questions, please visit the s.c.a.n. homepage.

« Previous Article | Top | Next Article »

Other StockCharts Blogs

Subscribe to this blog