You are on page 1of 3

#Clearing Memory = Remove all the variables from the memory

rm(list=ls())

#Loading Data
setwd("D:\\CMS\\")
data1=read.csv("Nifty.csv", header=T)
data2=read.csv("SP.csv", header=T)
data3=read.csv("USDINR.csv", header=T)
data4=read.csv("WTI.csv", header=T)

#View Data
head(data1)
tail(data1)

#Extract Prices
nifty=data1[,2]
sp=data2[,2]
usdinr=data3[,2]
wti=data4[,2]

#Plot Prices
plot(nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Price")
plot(sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Price")
plot(usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Price")
plot(wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Price")

#Comparative Plots
old.par <- par(mfrow=c(2,2))
plot(nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Price")
plot(sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Price")
plot(usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Price")
plot(wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Price")
par(old.par)

#Download data using get.hist.quote from package tseries


#How to install a library
library(tseries)
d<- get.hist.quote(instrument = "^NSEI", start="2000-01-01, end=2019-09-17",
quote = "Adj", provider = "yahoo")
data=na.omit(d)
plot(data,type="l")

#Test of Stationarity
adf.test(nifty)
adf.test(sp)
adf.test(usdinr)
adf.test(wti)

#Calculate Returns
ret_nifty=diff(log(nifty))
ret_sp=diff(log(sp))
ret_usdinr=diff(log(usdinr))
ret_wti=diff(log(wti))

#Plot Returns
plot(ret_nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Return")
plot(ret_sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Return")
plot(ret_usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Return")
plot(ret_wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Return")
#Comparative Plots
old.par <- par(mfrow=c(2,2))
plot(ret_nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Return")
plot(ret_sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Return")
plot(ret_usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Return")
plot(ret_wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Return")
par(old.par)

#Test of Stationarity
adf.test(ret_nifty)
adf.test(ret_sp)
adf.test(ret_usdinr)
adf.test(ret_wti)

#Descriptive Statistics
all_data=cbind(ret_nifty,ret_sp,ret_usdinr,ret_wti)
library(fBasics)
basicStats(all_data)

#Test of Normality
jarque.bera.test(ret_nifty)
jarque.bera.test(ret_sp)
jarque.bera.test(ret_usdinr)
jarque.bera.test(ret_wti)

#Simulate a WN model with list(order = c(0, 0, 0))


white_noise <- arima.sim(model=list(order=c(0,0,0)), n=1000)
ts.plot(white_noise)
Box.test(white_noise,lag=20)

#Random walk
RW<-cumsum(white_noise)
ts.plot(RW)

#one path of RW
rn=rnorm(1000,0,1)
a=c(100,rn)
rwalk1=cumsum(a)
plot(rwalk1,type="l")

# Random Walk
m=100 #No of Paths
n=1000 #No. of steps in each path
Rwalk=matrix(nrow=m,ncol=n+1)
for (i in 1:m){
randnum=rnorm(n,0,1)
rw=cumsum(c(100,randnum))
Rwalk[i,]=rw
}
matplot(t(Rwalk),type="l")

#Autocorrelation Test for Return Series


Box.test(ret_nifty)
Box.test(ret_sp)
Box.test(ret_usdinr)
Box.test(ret_wti)

#Selecting lags for AR model


library(forecast)
auto.arima(ret_nifty, max.p=10,max.q=0,d=0,ic="aic")
auto.arima(ret_nifty, max.p=10,max.q=0,d=0,ic="bic")
auto.arima(ret_sp, max.p=10,max.q=0,d=0,ic="aic")
auto.arima(ret_sp, max.p=10,max.q=0,d=0,ic="bic")
auto.arima(ret_usdinr, max.p=10,max.q=0,d=0,ic="aic")
auto.arima(ret_usdinr, max.p=10,max.q=0,d=0,ic="bic")
auto.arima(ret_wti, max.p=10,max.q=0,d=0,ic="aic")
auto.arima(ret_wti, max.p=10,max.q=0,d=0,ic="bic")

# Fit AR(p) model


out_1=arima(ret_nifty,c(2,0,0),include.mean = T)

# Model diagnostics (Test of autocorrelation in residuals)


Box.test(out_1$residuals,lag=10,type="Ljung")

# Predict the series


predict(out_1,10)

#MA
#ARMA
#ARIMA

You might also like