How to Construct a Relative Strength Matrix for Stocks and ETFs with Python

Bryant Sheehy
6 min readJan 17, 2022

How and why to use the Relative Strength concept to find promising stock market investments, with links to Python code on Github.

Photo by John Arano on Unsplash

Among technical analysts, the concept of Relative Strength is very popular. The concept is simple but powerful. Relative Strength is simply the measurement of one security’s price performance relative to another security, to a benchmark index, or to another group of securities. This is a different calculation than the Relative Strength Index (RSI), which is also popular. The RSI measures the performance of a security against its own past performance, usually over a 14-day period. The Relative Strength calculation that I’m talking about is derived by dividing the price of that security by the price of the comparative security or index.

By observing the trend in this ratio, up or down, you can compare the relative performance of the subject security against a benchmark over time. Relative Strength is usually measured for an intermediate time range of six to twelve months. This gives you enough time series data to see a few reversals back and forth among the contenders.

The power in Relative Strength is that it tends to persist for a measurable length of time, often for weeks and months. There are a number of well-documented reasons for this. The most obvious reason is that Relative Strength identifies and takes advantage of the herd mentality on Wall Street. These observations are identified and discussed at length in a white paper produced by Clark Capital Management Group on their website.

For example, suppose that we want to measure the Relative Strength of Apple’s stock (ticker AAPL) against the S&P 500 index. We could simply divide the daily closing price of AAPL by the SPY ETF (a convenient representation of the S&P 500 index) over the last six months. Then we can observe the daily trend of this ratio over time on a chart. A Point & Figure chart is most often used in this type of analysis because it cuts out a lot of market noise and shows both long-term and short-term trends. Here’s what that chart would look like lately:

You can see from the blue lines on the chart that the Relative Strength of AAPL versus the S&P 500 index is in a rising trend, although slowing over the last few months. This does not always mean that AAPL’s stock has been steadily going up faster than the index. If the index has been declining, a strong Relative Strength showing for AAPL could also mean that AAPL has been declining at a slower rate.

One of the features I like best about the Relative Strength concept is that you can use it to find the strongest performing asset in a group of assets by comparing each asset in the group against every other asset in a “round-robin” tournament style. The result of this type of analysis is called a Relative Strength Matrix. In this article, I will explain how an RS Matrix is calculated and I’ll include links to the Python notebook I wrote for this purpose.

Let’s say you are looking at a group of five technology stocks (Apple, Meta, Microsoft, Oracle and Amazon) and you’d like to see which one is the strongest in terms of Relative Strength. You would need to calculate the RS ratio (stock 1 close price / stock 2 close price) for each of the five stocks against each of the other stocks. Each stock would get its turn in both the numerator and the denominator. So you would end up creating twenty-five ratios measured over a time series of six months.

Then you would use a Point & Figure algorithm to determine the most recent short-term direction indicator (X vs O) and long-term trend indicator (BUY vs SELL) for each RS ratio pair. To find the winner, you add up the number of X’s and BUYs for each stock and rank them by the BUY count followed by the X count. The end result would look like this, using a 6% box size parameter:

In this scenario, Apple is showing the strongest RS trend of the five. This means that Apple could have the best odds of outperforming the group over the next few months.

An even more powerful way to use this tool is to compare sectors and industries against each other. Momentum research has shown that 80% of a stock’s price performance can be explained by the strength of the industry that it belongs to.

In this scenario, we could compare the ETFs that represent the eleven sectors comprising the S&P 500 index, plus the S&P 500 itself. This would show us which sectors are outperforming versus underperforming the index in a way that should persist for at least a few months going forward. The RS Matrix ranking would look like this, using a 3.5% box size parameter:

This result indicates to me that the Energy and Real Estate sectors should be best positioned to outperform the S&P 500 index over the next several months, and would be good hunting grounds to look for individual stocks or smaller industry groups that could potentially outperform.

I’m also observing that only two of the eleven sectors appear to be showing more strength than the overall S&P 500 index itself. I suspect that might be an indicator of underlying weakness in the market. Check out the last few paragraphs of my recent article on Point & Figure calculations (BPI stats) for more evidence of that trend.

So how do we actually run these calculations? First, you need a reliable source of stock market data. I like to use Intrinio.com because they provide relatively clean data, easy-to-use APIs and good customer service. Then here is the list of coding steps, put in the form of “pseudo-code” which you could use to create a calculation algorithm in Python, R, Scala or any other data manipulation language.

I have published the actual Python code in my Github account at

The Jupyter Notebook file you would look for is “RS Matrix Generator — Intrinio Data Source”. In the meantime, stay tuned for more articles on how to use Relative Strength and Point & Figure calculations to identify stock market winners.

Bryant Sheehy has 15+ years of experience in financial data sales and business development, and is now transitioning to data engineering and analytics. You can follow him on Medium or contact him on LinkedIn.

The opinions expressed here do not constitute investment advice. If you intend to use these ideas to make your own investments, please seek the advice of a licensed professional advisor.

--

--

Bryant Sheehy

For the last decade-plus, I’ve done financial data sales and business development. I’m now pivoting to data engineering and analytics.