Build Your Own Matlab Market Barometer

Note: Please read the disclaimer. The author is not providing professional investing advice.

There are matlab scripts that go with this article: examine_indices.m, get_hist_stock_data.m

One of the primary rules taught by William O’Neil and his Investor’s Business Daily (IBD) group is that your investing strategy should take into account what the stock market as a whole is doing. By “stock market as a whole” he’s referring to the market indices, such as the NASDAQ, S&P 500, and Dow Jones industrial average.

Why? If a stock has a low P/E, PEG, or price relative to value, isn’t that enough to signal a buy? Actually it’s all about stacking the odds in your favor since he says his research has shown that 3 out of 4 individual stocks follow the direction of the market indices (up or down). No need to bet on inferior odds or swim against the current if you don’t have to.

In addition, one might also want to consider monitoring the stock market sectors, as Jim Cramer in his book Mad Money claims that 50% of the movement of a stock’s price is determined by which way the sector that it belongs to moves.

It is definitely a bit of an art to determine when the market has switched directions for the long term. When the market is headed down there are usually short-term rallies that may take prices up for a few days, only to lose support and end up failing. Likewise in up markets there are usually occasional corrections when prices dip for a few days before later continuing to rise above previous highs. So, you see, you can easily be fooled by false signals.

The IBD newspaper and website monitors the indices closely in their daily column titled “The Big Picture” and will have an opinion on any given day about where the market is headed (and this section of the website isn’t free). The general rules that they follow are set out in Chapter 1 of this book. Click here for an interesting example from their website about how they used this algorithm to call the market top in 2000.

It might be illegal for me to reproduce their exact forecasting algorithm here – better to get it from the horse’s mouth anyway – but I will say that an important part is to look over the past few weeks of the indices’ price and volume action to identify and tally up the number of days of heavy buying or selling. Heavy volume is said to be representative of significant trading by institutional investors who manage hedge and mutual funds. As Jim Cramer would say, they ARE the market and they determine where prices go.

Particularly useful to watch for in the indices are recent days when:

(1) an index closes higher in price than the previous day, in higher than the previous day’s volume and/or higher than average recent volume (may signal that institutional investors are buying)

(2) an index closes lower in price than the previous day, but again in higher than the previous day’s volume and/or higher than average recent volume (may signal that institutional investors are selling)

(3) any days where there is a heavy volume of trading, but the closing price doesn’t really change (may indicate a new high or low has been reached, and the market is about to switch directions)

Part of the IBD algorithm is to identify days that meet the criteria above over a time period of a few weeks. Based on the number of days of accumulation (heavy buying), distribution (heavy selling), and stalling action (heavy volume, little price movement) they make predictions about where the market is headed.

As mentioned before, it’s a bit of an art learning to interpret the technical indicators, though their book does provide the general rules. But if you want to try your luck doing it yourself – or compare your analysis with the pros, the following Matlab script may be helpful.


This examine_indices.m “main” script first calls the get_hist_stock_data.m “function” to retrieve recent price and volume data from Yahoo! Finance for the Dow, NASDAQ, and S&P500. Then it plots the result and identifies (based on your own parameter settings) possible days of accumulation, distribution, and stalling. What you do with that information is up to you. 🙂

Please know that the default parameters picked in the script are just for example. I have no way of knowing whether these are optimal or reliable. The point of the script is just to provide a starting point for you to develop your own market barometer strategy.

Good Luck!