top of page

Relative Strength Index (RSI) | Python

Please refer to Extract Historical Stock Data using API to extract the historical data of the stock for which we want to identify the RSI.

Let us understand how to identify RSI-

Assuming that we have already extracted the historical stock data and stored in a Python dictionary, we will now look at the calculation behind the RSI.

Relative Strength Index (RSI)

RSI is used for technical analysis indicates momentum in price which measures the magnitude of recent price movement. This is determined to evaluate the overbought and oversold positions in the market.

RSI = 100 - ( 100 / ( 1 + RS ) )


RS = Average Gain / Average Loss

To calculate RSI for all the stocks in one go, we will place our code inside a loop function which will refer to each item specified in the ticker list. An empty Python dictionary is defined so that calculated RSI of each item can be stored classified with the respective ticker symbol and later, it can be retrieved as and when required.

(learn more about for loop)

To call the calculated RSI from that dictionary, we simply specify the dictionary name followed by the ticker symbol inside the brackets with double quotes and the output of the same is shown below-

(Since the time bucket in RSI calculation is 14-Days in our example, we get NAN values for initial 13-Days due to absence of Close Price data. We will drop those NAN values using '.dropna()' command.)

If we were to plot this, Close Price over the period and the RSI Value on a chart using matplotlib library, it will look like as shown below-

First plot represents Stock Price over the period and the second plot represents Relative Strength Index (RSI) Value over the period-


Feel free to share your views and ask for models in the #comment section. If you like our content, please hit a #like button.

65 views0 comments


bottom of page