How to Calculate and Analyze Relative Strength Index (RSI) Using Python

Automate the calculation of RSI for a list of stocks, and then analyze its accuracy at predicting future price movements.

Cameron Shadmehry
Hands-Off Investing

--

An outline of the process to calculate RSI and its historical accuracy for a stock. (Image by Author)

The relative strength index is a momentum oscillator commonly used to predict when a company is oversold or overbought. The calculation process is straightforward:

  1. Observe the last 14 closing prices of a stock.
  2. Determine whether the current day’s closing price is higher or lower than the previous day.
  3. Calculate the average gain and loss over the last 14 days.
  4. Compute the relative strength (RS): (AvgGain/AvgLoss)
  5. Compute the relative strength index (RSI): (100–100 / ( 1 + RS))

The RSI will then be a value between 0 and 100. It is widely accepted that when the RSI is 30 or below, the stock is undervalued and when it is 70 or above, the stock is overvalued.

The code in this walk-through will calculate the RSI for each stock in a user-defined list of tickers. It will then highlight every crossover in each stock’s historical data and using this information, determine the accuracy of the RSI at predicting future price movements.

--

--