Technical indicators Relative Strength Index (RSI)


Background

RSI is a momentum indicator that measures the speed and magnitude of a security’s recent price changes to evaluate overvalued or undervalued conditions in the price of that security. It is displayed as an oscillator (a line graph) on a scale of zero to 100.

Besides the indication of overbought and oversold securities, RSI can also point securities that may be primed for a trend reversal or corrective pullback in price. Simply speaking, it can signal when to buy and sell. Traditionally, an RSI reading of 70 or above indicates an overbought situation. A reading of 30 or below indicates an oversold condition.

Python implemenation

import pandas as pd
import sqlite3

# load S&P500 data from stored database
sp500_db = sqlite3.connect(database="sp500_data.sqlite")

df = pd.read_sql_query(sql="SELECT * FROM SP500",
                       con=sp500_db,
                       parse_dates={"Date"})
df.head()

level_0indexDateTickerAdj CloseCloseHighLowOpenVolumegarmin_klass_volrsi
0002014-04-29A35.04953438.12589338.30472237.17453438.1187404688612.0-0.002274NaN
1112014-04-29AAL33.47674235.50999835.65000234.97000135.2000018994200.0-0.000788NaN
2222014-04-29AAPL18.63333321.15464221.28500021.05392821.205000337377600.0-0.006397NaN
3332014-04-29ABBV34.03498551.36999951.52999950.75999850.9399995601300.0-0.062705NaN
4442014-04-29ABT31.83151838.54000138.72000138.25999838.3699994415600.0-0.013411NaN
import pandas_ta

# calculate relative strength index
df['rsi'] = df.groupby(by='Ticker')['Adj Close'].transform(lambda x: pandas_ta.rsi(close=x, length=14))
aapl = df[df['Ticker'] == 'AAPL'].set_index('Date').drop('index',axis=1)
aapl.tail()

level_0TickerAdj CloseCloseHighLowOpenVolumegarmin_klass_volrsi
Date
2024-04-191229877AAPL165.000000165.000000166.399994164.080002166.21000767772100.00.00007837.902497
2024-04-221230380AAPL165.839996165.839996167.259995164.770004165.52000448116400.00.00011139.784337
2024-04-231230883AAPL166.899994166.899994167.050003164.919998165.35000649537800.00.00004942.166124
2024-04-241231386AAPL169.020004169.020004169.300003166.210007166.53999348251800.00.00008546.706439
2024-04-251231889AAPL169.889999169.889999170.610001168.149994169.52999950558300.00.00010448.493460
# update the database
df.drop('level_0', axis=1).to_sql(name="SP500",
          con=sp500_db,
          if_exists="replace",
          index=True)
sp500_db.close()
import matplotlib.pyplot as plt
from datetime import datetime

# only select the data from 2023-01-01
aapl_new = aapl[aapl.index > datetime(2023,1,1)]

# set the theme of the chart
plt.style.use('fivethirtyeight')
plt.rcParams['figure.figsize'] = (20,16)

# create two charts on the same figure
ax1 = plt.subplot2grid((10,1),(0,0), rowspan=4, colspan=1)
ax2 = plt.subplot2grid((10,1),(5,0), rowspan=4, colspan=1)

# plot the closing price on the first chart
ax1.plot(aapl_new['Adj Close'])
ax1.set_title('AAPL Adjust Close Price')

# plot the RSI on the second chart
ax2.plot(aapl_new['rsi'], color='orange', linewidth=1)
ax2.set_title('Relative Strength Index')

# add two horizontal lines, signaling the buy and sell ranges
# oversold
ax2.axhline(30, linestyle='--', linewidth=1.5, color='green')
# overbought
ax2.axhline(70, linestyle='--', linewidth=1.5, color='red')
<matplotlib.lines.Line2D at 0x183fbdf2a10>

AAPL Relative Strength Index

Discussion

Overbought or oversold

Generally, when the RSI indicator crosses 30 on the RSI chart (refer above), it is a bullish sign and when it crosses 70, it is a bearish sign. Therefore when the RSI values are 70 or above, it indicates the security is becoming overbought or overvalued, on the other hand, if an RSI value is 30 or below, it is interpreted that the security is oversold or undervalued.

Uptrend and downtrend

During an uptrend, the RSI tends to stay above 30 and should frequently hit 70. During a downtrend, it is rare to see the RSI exceed 70, and it frequently hits 30 or below.

Limitation

The RSI’s signals are most reliable when they conform to the long-term trend.

True reversal signals are rare and can be difficult to separate from false alarms.

The RSI is most useful in an oscillating market where the asset price is alternating between bullish and bearish movements.

Reference

Relative Strength Index (RSI) Indicator Explained With Formula by JASON FERNANDO


Author: wenvenn
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source wenvenn !