Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created December 25, 2021 12:03
Show Gist options
  • Save quantra-go-algo/8b333a601b5314a808457f70c0f86a42 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/8b333a601b5314a808457f70c0f86a42 to your computer and use it in GitHub Desktop.
Build Technical Indicators In Python - ROC
# Rate of Change code
# Load the necessary packages and modules
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
import yfinance
import pandas as pd
# Rate of Change (ROC)
def ROC(data,n):
N = data['Close'].diff(n)
D = data['Close'].shift(n)
ROC = pd.Series(N/D,name='Rate of Change')
data = data.join(ROC)
return data
# Retrieve the NIFTY data from Yahoo finance:
data = pdr.get_data_yahoo("^NSEI", start="2015-06-01", end="2016-01-01")
data = pd.DataFrame(data)
# Compute the 5-period Rate of Change for NIFTY
n = 5
NIFTY_ROC = ROC(data,n)
ROC = NIFTY_ROC['Rate of Change']
# Plotting the Price Series chart and the Ease Of Movement below
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(2, 1, 1)
ax.set_xticklabels([])
plt.plot(data['Close'],lw=1)
plt.title('NSE Price Chart')
plt.ylabel('Close Price')
plt.grid(True)
bx = fig.add_subplot(2, 1, 2)
plt.plot(ROC,'k',lw=0.75,linestyle='-',label='ROC')
plt.legend(loc=2,prop={'size':9})
plt.ylabel('ROC values')
plt.grid(True)
plt.setp(plt.gca().get_xticklabels(), rotation=30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment