Calculate the Relative Strength Index (RSI) with Python.

Calculate the Relative Strength Index (RSI) with Python.

The Relative Strength Index of a stock can be calculated with Python with a simple function. The function utilizes a time series dataframe provided by the Python Pandas module and outputs a value between 0 and 100, 0 being oversold and 100 being overbought.

The RSI is a technical indicator which measures the momentum of the change in price of a stock. Overbought and oversold conditions allow an investor to determine the directions and strength of a stocks movement within a specific time period. Shorter time periods will show more aggressive and volatile RSI movements while longer time periods may take longer to reach a specific RSI value.

When using the RSI in Python programs I mainly use it as a watch event, meaning if a specific RSI value is reached my program will take note and start watching for buy or sell signals.

def RSI(prices, n=14):
    deltas = (prices-prices.shift(1)).fillna(0)
    avg_of_gains = deltas[1:n+1][deltas > 0].sum() / n
    avg_of_losses = -deltas[1:n+1][deltas < 0].sum() / n
    rsi_series = pd.Series(50.0, deltas.index)
    up = lambda x: x if x > 0 else 0
    down = lambda x: -x if x < 0 else 0
    i = n+1
    for d in deltas[n+1:]:
        avg_of_gains = ((avg_of_gains * (n-1)) + up(d)) / n
        avg_of_losses = ((avg_of_losses * (n-1)) + down(d)) / n
        if avg_of_losses != 0:
            rs = avg_of_gains / avg_of_losses
            rsi_series[i] = 100 - (100 / (1 + rs))
        else:
            rsi_series[i] = 100
        i += 1
    return rsi_series

Full example of RSI with Python

import pandas as pd
import sys
import matplotlib.pyplot as plt

def RSI(prices, n=14):
    deltas = (prices-prices.shift(1)).fillna(0)
    avg_of_gains = deltas[1:n+1][deltas > 0].sum() / n
    avg_of_losses = -deltas[1:n+1][deltas < 0].sum() / n
    rsi_series = pd.Series(50.0, deltas.index)
    up = lambda x: x if x > 0 else 0
    down = lambda x: -x if x < 0 else 0
    i = n+1
    for d in deltas[n+1:]:
        avg_of_gains = ((avg_of_gains * (n-1)) + up(d)) / n
        avg_of_losses = ((avg_of_losses * (n-1)) + down(d)) / n
        if avg_of_losses != 0:
            rs = avg_of_gains / avg_of_losses
            rsi_series[i] = 100 - (100 / (1 + rs))
        else:
            rsi_series[i] = 100
        i += 1
    return rsi_series

api_key = 'demo'

ticker = sys.argv[1]  

api_url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=' + ticker + '&interval=5min&apikey=' + api_key + '&datatype=csv'

data = pd.read_csv(api_url)

stock_data = pd.DataFrame(index=range(0,len(data)),columns=['timestamp', 'open','high','low','close','volume'])

stock_data = stock_data.reindex(index=stock_data.index[::-1])


rsi_fairval = []



for i in range(0,len(data)):
    ts = data['timestamp'][i]
    stock_data['timestamp'][i] = data['timestamp'][i]
    stock_data['close'][i] = float(data['close'][i])
    stock_data['high'][i] = float(data['high'][i])
    stock_data['low'][i] = float(data['low'][i])
    stock_data['open'][i] = float(data['open'][i])
    stock_data['volume'][i] = int(data['volume'][i])
    rsi_fairval.append(50)
  

stock_data.index = stock_data.timestamp

stock_data = stock_data.dropna()

timestamp = stock_data['timestamp']

close = stock_data['close']

high = stock_data['high']

low = stock_data['low']

open = stock_data['open']

volume = stock_data['volume']



rsi = RSI(close,14)


plt.rcParams.update({
    "lines.color": "white",
    "patch.edgecolor": "white",
    "text.color": "white",
    "axes.facecolor": "black",
    "axes.edgecolor": "lightgray",
    "axes.labelcolor": "white",
    "xtick.color": "white",
    "ytick.color": "white",
    "grid.color": "lightgray",
    "figure.facecolor": "black",
    "figure.edgecolor": "black",
    "savefig.facecolor": "black",
    "savefig.edgecolor": "black"})

#Define our chart with 2 subcharts and a size of 1200x1200.
fig, axs = plt.subplots(2, 1,figsize=(12,12))




####Sub Graph 1

axs[0].set_title('Stock Chart For: ' + ticker,color='black')
axs[0].set_facecolor('#000000')
axs[0].plot(timestamp,close)
axs[0].set_xticks([0,len(close)-1], minor=False)
axs[0].set_ylabel(ticker + ' CLOSE',fontsize=18,color='white')
axs[0].tick_params(axis='x', colors='red')
axs[0].tick_params(axis='y', colors='green')
axs[0].grid(True,color='#292b2e')
fig.tight_layout()




####Sub Graph 2

axs[1].set_title('Relative Strength Index',color='black')
axs[1].set_facecolor('#000000')
axs[1].plot(timestamp,rsi)
axs[1].plot(timestamp,rsi_fairval,color='yellow')

axs[1].set_xticks([0, len(close)-1], minor=False)
axs[1].set_yticks([0, 30,50, 70, 100], minor=False)
axs[1].tick_params(axis='x', colors='red')
axs[1].tick_params(axis='y', colors='green')
axs[1].grid(True,color='#292b2e')
axs[1].set_ylabel('RSI',fontsize=18,color='white')
print("Done.")
plt.show()

Leave a Reply