Algo Trading: Technical Indicators Rate of Change(ROC) implementation in Python Programming.

roc

The Rate of Change(ROC) is a technical indicator is used to measure the percentage change between the most recent price and the price "n" days ago.The indicator fluctuates around the zero line say we take 12 days Moving Average and predict the next 12 days within in a prior.As we all know that trading days are of a year basically 12 months which get's divide in 2 parts 6 months and it's of months get a long term prediction with the help of "n" days moving average which we consider.

In short term we can utilized 2 days Moving Average in which below if it trades then it is to be considered as sell and above trade to be considered as buy.It basically consider Close price and in prior to yesterday's 12 days or 2 days moving average close and with the help of it we can analyse the after 2 days or after 12 days effect and this basically get multiplied with the days's it required to show the result of it ROC basically help's in to gauge the short-term momentum or long-term momentum.

Rising ROC gives bullish signal while a falling ROC gives a bearish signal.

Estimation:

ROC=[((Close price today - Close price "n" days ago)/Close price "n" days ago))]

Below we will Consider Video for Proper Instructions and python code to compute the Rate of Change(ROC) technical indicator.

Example code:2-day ROC for Nifty

In the code below we are using Series,shift,diff and then join functions.

In this we are considering Series which is a one dimensional array-like object containing array of data. In diff function to compute we consider current price close and then we diffirentiate with "n" days in which we want to consider it in prior for prediction which is used to get to fetch with the help of shift functions.Then we joins a given series with a specified series/data frame them in the end for the computation using join function .

Technical Indicators Rate of Change(ROC) for NIFTY implementation in Python Code as follows:


#import library modules
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt

#Compute 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

data=web.DataReader('^NSEI',data_source='yahoo',start='1/4/2017',end='6/4/2017')
data=pd.DataFrame(data)

n=5
ROC_Nifty=ROC(data,n)
ROC=ROC_Nifty['Rate of Change']

#represent them in chart
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)
plt.show()
--End of Code Input--