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.

I will also touch on the analysis of this technical indicator. Using the algorithm below, we can extract a lot of information about each stock and their likelihood of being accurately predicted by the RSI. For example, we can quantify and visualize which stocks were most successfully predicted using the relative strength index.

Image for post

The final output of the analysis section in the code below.

Image for post

A visualization of the observed stocks where color signifies RSI’s predictive accuracy and size is the volume of momentum crossovers. (Image by Author)

In the first picture above, you can get an idea of what the final output of this code’s analysis section will look like. The variables we will be using to judge the RSI’s predictive power can be used to create an ROC curve for further visualization. In the second picture, we can see the list of observed stocks graphed by their respective accuracy and volume of crosses. I will dive further into the insights derived from this code later.

#data-science #technology #programming #finance #investing

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