Scan Engines are designed to find charts with a specific set of technical criteria on a specific date. Occasionally, we get a question from someone who is trying to use the scan engine to find stocks with a specific set of technical criteria over a range of dates. We call this the “within” problem since they are looking for something that happened “within” a certain time period. For example, “Show me all the stocks that had a MACD crossover within the past month.”
Our scan engine doesn't support these “within” scans, and frankly, they're not that useful in a real-world trading environment. From a high-level perspective, the purpose of scanning is to help you decide which stocks to buy or sell “soon” - i.e. before the data used in the scan changes significantly. The standard scenario is to run your scans after the market closes in preparation for placing orders early the next day.
If you were to scan for stocks that had a MACD crossover within the last month, some of the results from that “within” scan might still be valid today, but other results might not. A stock that had a MACD crossover three weeks ago would still appear in your scan results - even if it crossed back under the next day - because it had a MACD crossover “within the last month”. Even more frustrating, your scan does not tell you which stocks still meet the scan criteria and which do not.
If you're encountering this “within” problem in your scan, there are a couple of techniques you might use to work around it, depending on what you are trying to accomplish with your scan.
If you want to scan “within” a very short time period for just one of the clauses in your scan, then the OR clause technique might be a suitable option for you.
For example, let's say you have a complicated scan like this:
[type = stock] AND [country = US] AND [SMA(60,Close) > 10] AND [SMA(20,Volume) > 100000] AND [Close > SMA(50,Close)] AND [ADX Line(14) > 20] AND [Plus DI(14) x Minus DI(14)]
The first five lines define the universe of stocks you are interested in and the technical conditions that should be present for any scan results. The last line defines the signal - the event that happens today that makes you interested in this stock right now. But let's say you are interested in stocks where Plus DI crossed over Minus DI any time in the last three days.
To accomplish this, you can replace the last clause with this more complex one:
AND [ [Plus DI(14) x Minus DI(14)] OR [yesterday's Plus DI(14) x yesterday's Minus DI(14)] OR [2 days ago Plus DI(14) x 2 days ago Minus DI(14)] ]
This will scan for stocks that meet all your other technical conditions today and that had the crossover signal either today, yesterday, or the day before yesterday. This can be useful for signals that might take a day or two to be confirmed by other indicators.
Learn More: OR Clauses
While this OR Clause technique is a viable option for simple signals and short timeframes, once you try to scan for more than one signal and/or scan “within” a longer time period, the scan syntax quickly becomes unmanageable.
While the OR clause technique just focuses on a single clause, there may be situations where you want to check if all the clauses in your scan are true at the same time. If you want to scan for a stock that meets multiple criteria all on the same day, then the Starting Date Adjuster technique is recommended. This technique can be very useful for backtesting, but is generally not used when scanning for securities to invest in.
To use this technique, first change your “within” scan so that it refers to “today” instead. For example, take our original “Show me all the stocks that had a MACD crossover within the past month” and turn it into “Show me all the stocks that had a MACD crossover today.”
You can then run the scan one time for each day in your “within” time period, changing the value of the Starting Date Adjuster on the Advanced Scan Workbench for each run.
When you use the Starting Date Adjuster to run your scan, it pretends that it is running the scan on the day you specified. So, it scans for stocks that met all your scan criteria on that particular day. If your scan has more to it than the MACD crossover clause, the scan will check if all those other criteria were also true on the day you specified in the Starting Date Adjuster.
Learn More: Starting Date Adjuster
While it can be time-consuming to run all those separate scans for a large “within” period, one advantage is that you can easily see which stocks met the criteria on which days.
Scanning across a range of dates can be tricky to do, and isn't always very useful for real-world trading. Since you will be buying and selling the securities right now, it's best to focus on what those securities are doing right now, rather than what they did days or weeks ago.
However, scanning across a few days using OR clauses can be helpful in certain circumstances. The goal is to keep things simple, focusing on a very small date range and a single scan clause.
If you're scanning multiple criteria across a range of dates for backtesting purposes, the Starting Date Adjuster is the best approach. This will run your scan as though it was run on that day in the past, and confirm that all of your scanning criteria were true on that date.