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!

6 thoughts on “Build Your Own Matlab Market Barometer”

  1. Hey,

    Just new to your blog, just enjoying reading through your backlog of posts!

    Like yourself, I also dabble in the stock market, and was particularly impressed at the above market barometer.

    Unfortunately I don’t have access to Matlab… you know of any other program or website/blog which has published a similar barometer…..though in excel for example?

    Thanks in advance,


  2. hey thanks much for writing JAD,

    yes a matlab license is quite expensive! i know it’s possible to to use excel to retrieve information from the web like matlab, but i’ve yet to take the time to learn how to do this.

    your other option is to use Java. you can download the free Netbeans compiler and be up and running in no time. before i decided to bite the bullet and buy matlab, i used java for all my stock simulations. it didn’t take too long to get up the learning curve.

    hope this helps and good luck!

  3. Matlab is expensive, but in the end I am just so much quicker programming in it that I decided to bite the bullet and buy a Matlab license. I have since heard that a program called Octave is essentially a free version of Matlab but have never used it myself. Don’t know if it supports the Java interface for retrieval of data from the web.

    – lumi

Leave a Reply

Your email address will not be published.