Writing Scans with OR Clauses

Writing Scans with OR Clauses

Using OR clauses in your scans can take some time to master, but these clauses provide so much flexibility to your scans.

Most scan clauses are joined together with the AND operator, which means that a stock has to meet the criteria in ALL the scan clauses. Clauses joined together with the OR operator work differently - they are part of a list of criteria, and a stock just has to meet one of the criteria on the list. For more detailed information on how OR clauses work, check out the OR Clause section of our Writing Scans article in the Support Center.

OR clause lists are very handy when there are multiple variations on a scan criteria that you want to test for all at once. For example, you may be interested in stocks that trade on either the NYSE or the NASDAQ exchange. An OR clause allows you to scan for both exchanges at once, rather than having to run two separate scans: one to find stocks trading on the NYSE and another to find stocks trading on the NASDAQ.

Below we cover several different kinds of OR clause lists that you can use in your scans.

Defining the Scan Universe with OR Clauses

A classic example of defining your scan universe with OR clauses is the S&P 1500. You can find stocks in the S&P 1500 by scanning for stocks that are in the S&P 500 OR in the S&P 400 OR in the S&P 600:

[type = stock]
AND [
  [group is SP500] 
  OR [group is SP400]
  OR [group is SP600]
]

Note the use of an additional set of brackets around the entire OR clause list. It isn't always clear to the Scan Engine which clauses are part of your list, so it's important to put an additional set of square brackets around the list. This tells the Scan Engine how to process your scan correctly. For more details on why these brackets are so important, check out the OR Clause section of our Troubleshooting Scans article in the Support Center.

OR clauses aren't just for determining index membership; they can also be used to define your universe as a list of exchanges, industries, or even ChartLists.

Defining Technical Conditions with OR Clauses

Once you've defined the universe for your scan, the next step is to define the technical conditions that your scan results must meet. OR clauses can be used here too.

In this example, we're looking for a stock that is in an uptrend. We've specified two different ways to test for an uptrend in this scan: checking that the 20-day SMA is above the 50-day SMA, or looking for the Uptrend candlestick pattern:

[type = stock]
AND [ 
  [today's sma(20,close) > today's sma(50,close)] 
  OR [Uptrend is true]
]

OR clauses can be used with any kind of technical condition criteria. You can see in the example above that we used both technical indicators and candlestick patterns in a single OR clause list.

Defining Signals with OR Clauses

Good scans typically have at least one signal criteria, but often there are multiple technical signals that would meet your requirements. Take the following example:

[type = stock] 
AND [
  [MACD Line(12,26,9) x 0] 
  OR [ROC(14) x 0] 
  OR [CMF(20) x 0]
]

In this example, there are three possible signals that a stock could have: either the MACD, ROC, or CMF could cross above zero. All three of these oscillator crossovers indicate a change from negative to positive momentum, so if a stock has any one of these crossovers, it indicates a bullish move. It is not necessary for all three crossovers to happen for the stock at the same time; as long as at least one crossover happens, the stock is returned as part of the scan results.

Using OR Clauses to Scan Multiple Timeframes

Sometimes it is useful to scan for a specific signal happening “recently”, but not necessarily today. OR clauses come to the rescue here too, allowing you to scan for a signal that occurred today or yesterday or the day before, and so on.

[type = stock]
AND [ 
  [Plus DI(14) x Minus DI(14)] 
  OR [1 day ago Plus DI(14) x 1 day ago Minus DI(14)]
]

This scan looks for Plus DI crossing over Minus DI either today or yesterday. While this approach is not really suited to scanning over a long period of time, it's a useful technique when looking for signals that happened sometime in the last few days.

Combining AND and OR Clauses

The examples given above are fairly simple, grouping together two or three single clauses with OR operators, but OR clause scans can be made more complex by combining AND clauses and OR clauses in different ways. Let's look at a couple of examples.

Energy and Materials Stocks in the S&P 1500

In this first example, we're looking for S&P 1500 stocks that are in the Energy or Materials sectors:

[type = stock]
AND [
  [group is SP500] 
  OR [group is SP400]
  OR [group is SP600]
]
AND [
  [group is EnergySector]
  OR [group is MaterialsSector]
]

This scan contains two different OR clause lists: one for confirming membership in the S&P 1500, and one for limiting to Energy and Materials stocks. An AND operator connecting the two OR clause lists tells us that at least one criteria from each list needs to be met. Brackets are crucial here to help the Scan Engine understand which clauses are part of which OR clause list.

Recent SMA Crossover on Strong Volume

In our second example, we want each of the items on our OR clause list to be two-part criteria. We want to scan for stocks with an SMA crossover AND a large volume increase today, OR stocks with an SMA crossover AND a large volume increase yesterday.

[type = stock]
AND [
  [
    [today's close x sma(10,close)] 
    AND [today's volume > yesterday's volume *1.25]
  ]
  OR
  [
    [yesterday's close x yesterday's sma(10,close)] 
    AND [yesterday's volume > 2 days ago volume *1.25]
  ]
]

To accomplish this, we nest AND clauses inside of our OR clause list. Again, brackets help the Scan Engine understand which clauses are grouped together. Notice that in addition to the brackets around the entire OR clause list, we have extra brackets around the “AND clause list” inside each OR clause.

The Scan Engine works its way from the inside brackets to the outside brackets. First, it finds symbols that had an SMA crossover on strong volume today (our first AND clause list). Then it finds symbols that had an SMA crossover on strong volume yesterday (our second AND clause list). Once these innermost brackets have been processed, it moves on to the next level of brackets, processing our OR clause list and determining which symbols are in either the first set OR the second set of symbols.

Check out Michael Daumer's Momentum Buy Signal scan for a great example of a properly bracketed scan that combines multiple levels of AND and OR clauses.

Conclusions

OR clauses allow you to broaden the scope of a single scan, defining more than one possible criteria that will return results. They can be used to list multiple criteria options for ticker properties (to define the universe of your scan), technical conditions, signals, and even multiple timeframes.

The most important thing to remember when using OR clauses is to include extra square brackets to group clauses together. This helps the Scan Engine understand what you are looking for, so it returns the correct scan results.

Additional Resources

Articles

Tutorials