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

By Bryant Sheehy
January 21, 2022

Among technical analysts, the concept of Relative Strength is very popular. The concept is simple but powerful.

## What is Relative Strength?

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. 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.

## Calculate Relative Strength

The Relative Strength of a security is calculated 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.

## Measuring the Relative Strength of Apple (AAPL)

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.

## How to Uncover the Strongest Performing Asset with Relative Strength

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.

## How to Compare the Relative Strength of Sectors and Industries

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.

## Free Relative Strength Calculator (Swipe My Python Code)

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:

https://gist.github.com/f78b005e1077c53834c4c1d6f19470b6.git

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

https://github.com/bnsheehy/Stock-Market_Data_ETL

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.