#getting historical monthly prices of TCS
tcsmonthlyprices <- get.hist.quote(instrument="TCS.NS",start="2004-08-01",
                                 end="2018-06-01",quote="AdjClose",provider ="yahoo",origin = "1970-01-01",compression ="m",retclass = "zoo")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
#getting historical daily prices of TCS
tcsdailyprices <- get.hist.quote(instrument="TCS.NS",start="2004-07-01",
                                 end="2018-06-01",quote="AdjClose",provider = "yahoo",origin = "1970-01-01",compression ="d",retclass = "zoo")
## Warning: TCS.NS contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## time series starts 2004-08-25
nrow(tcsdailyprices)
## [1] 3411
start(tcsdailyprices)
## [1] "2004-08-25"
tcsdailyprices <- na.omit(tcsdailyprices)
nrow(tcsdailyprices)
## [1] 3398
#getting historical daily prices of Infosys
infydailyprices <- get.hist.quote(instrument="INFY.NS",start="2004-07-01",
                                 end="2018-06-01",quote="AdjClose",provider = "yahoo",origin = "1970-01-01",compression ="d",retclass = "zoo")
## Warning: INFY.NS contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
nrow(infydailyprices)
## [1] 3450
start(infydailyprices)
## [1] "2004-07-01"
infydailyprices <- na.omit(infydailyprices)
nrow(infydailyprices)
## [1] 3436
#getting historical monthly prices of Infosys
infymonthlyprices <- get.hist.quote(instrument="INFY.NS",start="2004-07-01",
                                 end="2018-06-01",quote="AdjClose",provider = "yahoo",origin = "1970-01-01",compression ="m",retclass = "zoo")
nrow(infymonthlyprices)
## [1] 168
start(infymonthlyprices)
## [1] "2004-07-01"
infymonthlyprices <- na.omit(infymonthlyprices)
nrow(infymonthlyprices)
## [1] 168
#getting historical daily prices of SP500
sp500dailyprices <- get.hist.quote(instrument="^gspc",start="2004-07-01",
                                 end="2018-06-01",quote="AdjClose",provider = "yahoo",origin = "1970-01-01",compression ="d",retclass = "zoo")
## time series ends   2018-05-31
nrow(sp500dailyprices)
## [1] 3504
start(sp500dailyprices)
## [1] "2004-07-01"
sp500dailyprices <- na.omit(sp500dailyprices)
nrow(infydailyprices)
## [1] 3436
#getting historical monthly prices of SP500
sp500monthlyprices <- get.hist.quote(instrument="^gspc",start="2004-07-01",
                                 end="2018-06-01",quote="AdjClose",provider = "yahoo",origin = "1970-01-01",compression ="m",retclass = "zoo")
## time series ends   2018-05-01
nrow(sp500monthlyprices)
## [1] 167
start(sp500monthlyprices)
## [1] "2004-07-01"
sp500monthlyprices <- na.omit(sp500monthlyprices)
nrow(infymonthlyprices)
## [1] 168
#merge all the 3 asset prices
tcsinfysp500dailyprices <- merge(tcsdailyprices , infydailyprices, sp500dailyprices)

tcsinfysp500dailyprices <- na.omit(tcsinfysp500dailyprices)

head(tcsinfysp500dailyprices)
##            Adjusted.tcsdailyprices Adjusted.infydailyprices
## 2004-08-25                66.53886                 18.06204
## 2004-08-26                65.93596                 18.18560
## 2004-08-27                64.83519                 18.41799
## 2004-08-30                66.45860                 18.56094
## 2004-08-31                66.54858                 18.53801
## 2004-09-01                66.53563                 18.60506
##            Adjusted.sp500dailyprices
## 2004-08-25                   1104.96
## 2004-08-26                   1105.09
## 2004-08-27                   1107.77
## 2004-08-30                   1099.15
## 2004-08-31                   1104.24
## 2004-09-01                   1105.91

check if all the rows in the merged dataset are complete and have no missing values

if(nrow(tcsinfysp500dailyprices) == length(complete.cases(tcsinfysp500dailyprices))){
  cat("NO NULL values in the dataset\n")
}else{
  cat("caution : NULL values in the dataset . May yield to biased results\n")
}
## NO NULL values in the dataset

Daily stock returns to monitor the magnitude of this change. The daily return measures the dollar change in a stock’s price as a percentage of the previous day’s closing price. A positive return means the stock has grown in value, while a negative return means it has lost value.

#Daily returns
tcsdailyreturns <- Return.calculate(xts(tcsdailyprices),method ="simple")
infydailyreturns <- Return.calculate(xts(infydailyprices),method = "simple")
sp500dailyreturns <- Return.calculate(xts(sp500dailyprices),method = "simple")

Monthly Return is the period returns re-scaled to a period of 1 month..

#monthly, daily returns of TCS, Infosys, SP500
tcsmonthlyreturns <- Return.calculate(xts(tcsmonthlyprices),method ="simple")
infymonthlyreturns <- Return.calculate(xts(infymonthlyprices),method = "simple")
sp500monthlyreturns <- Return.calculate(xts(sp500monthlyprices),method = "simple")

tcsdailyreturns <- Return.calculate(xts(tcsdailyprices),method ="simple")
infydailyreturns <- Return.calculate(xts(infydailyprices),method = "simple")
sp500dailyreturns <- Return.calculate(xts(sp500dailyprices),method = "simple")

omit daily NA values

#omit daily NA values in TCS, Infosys, SP500
tcsdailyreturns<-na.omit(tcsdailyreturns)
infydailyreturns<-na.omit(infydailyreturns)
sp500dailyreturns<-na.omit(sp500dailyreturns)
head(tcsdailyreturns)
##                 Adjusted
## 2004-08-26 -0.0090608409
## 2004-08-27 -0.0166945172
## 2004-08-30  0.0250390721
## 2004-08-31  0.0013538202
## 2004-09-01 -0.0001945496
## 2004-09-02  0.0058224143
head(infydailyreturns)
##                Adjusted
## 2004-07-02  0.010328212
## 2004-07-05  0.004284995
## 2004-07-06 -0.002064389
## 2004-07-07 -0.023868937
## 2004-07-08 -0.028870095
## 2004-07-09  0.022369737
head(sp500dailyreturns)
##                Adjusted
## 2004-07-02 -0.003153344
## 2004-07-06 -0.008148398
## 2004-07-07  0.001899280
## 2004-07-08 -0.008244410
## 2004-07-09  0.003336075
## 2004-07-12  0.001383809

omit monthly

#omit monthly NA values of TCS, Infosys, SP500
tcsmonthlyreturns<-na.omit(tcsmonthlyreturns)
infymonthlyreturns<-na.omit(infymonthlyreturns)
sp500monthlyreturns<-na.omit(sp500monthlyreturns)
head(tcsmonthlyreturns)
##               Adjusted
## 2004-09-01  0.03946953
## 2004-10-01  0.12576070
## 2004-11-01  0.12610816
## 2004-12-01  0.04769249
## 2005-01-01 -0.02625046
## 2005-02-01  0.06190765
head(infymonthlyreturns)
##               Adjusted
## 2004-08-01  0.01416138
## 2004-09-01  0.07616871
## 2004-10-01  0.12435681
## 2004-11-01  0.18023268
## 2004-12-01 -0.02675611
## 2005-01-01 -0.01101516
head(sp500monthlyreturns)
##                Adjusted
## 2004-08-01  0.002287350
## 2004-09-01  0.009363876
## 2004-10-01  0.014014244
## 2004-11-01  0.038594936
## 2004-12-01  0.032458213
## 2005-01-01 -0.025290467

finding mean and standard deviation

mean

mean(tcsdailyreturns)
## [1] 0.001289141
mean(tcsmonthlyreturns)
## [1] 0.0233973
mean(infydailyreturns)
## [1] 0.001505352
mean(infymonthlyreturns)
## [1] 0.04141818
mean(sp500dailyreturns)
## [1] 0.0003182043
mean(sp500monthlyreturns)
## [1] 0.006197399

standard deviation

sd(tcsdailyreturns)
## [1] 0.02736953
sd(tcsmonthlyreturns)
## [1] 0.08214848
sd(infydailyreturns)
## [1] 0.0331111
sd(infymonthlyreturns)
## [1] 0.2279357
sd(sp500dailyreturns)
## [1] 0.01171315
sd(sp500monthlyreturns)
## [1] 0.03902271

equity curves, shows how our investment amount in each asset grows over time.

infymonthlyreturns = na.omit(infymonthlyreturns)
tcs_eqtycurve <- cumprod(1+tcsmonthlyreturns)
infy_eqtycurve <-cumprod(1+infymonthlyreturns)
sp500_eqtycurve <-cumprod(1+sp500monthlyreturns)

tcs_eqtycurve<-na.omit(tcs_eqtycurve)
infy_eqtycurve<-na.omit(infy_eqtycurve)
sp500_eqtycurve<-na.omit(sp500_eqtycurve)

merge all 3 compounded returns to produce equity curves

datatoplot <- merge(tcs_eqtycurve,infy_eqtycurve,sp500_eqtycurve)
datatoplot <- na.omit(datatoplot)
head(datatoplot)
##            Adjusted Adjusted.1 Adjusted.2
## 2004-09-01 1.039470   1.091409   1.011673
## 2004-10-01 1.170194   1.227133   1.025850
## 2004-11-01 1.317765   1.448302   1.065443
## 2004-12-01 1.380612   1.409551   1.100025
## 2005-01-01 1.344371   1.394025   1.072205
## 2005-02-01 1.427598   1.509555   1.092474
colnames(datatoplot) <- c("TCS" , "INFOSYS" , "SP500")
plot.zoo(datatoplot, plot.type = "single" , main="",col = c("red","blue","black"), ylab="cummulative returns", lwd=2)
legend(x="topleft", legend = colnames(datatoplot), col=c("red" , "blue" ,"black"),lwd = 2)

daily

equity curves, shows how our investment amount in each asset grows over time daily.

infydailyreturns = na.omit(infydailyreturns)
tcs_eqtycurved <- cumprod(1+tcsdailyreturns)
infy_eqtycurved <-cumprod(1+infydailyreturns)
sp500_eqtycurved <-cumprod(1+sp500dailyreturns)

tcs_eqtycurved<-na.omit(tcs_eqtycurved)
infy_eqtycurved<-na.omit(infy_eqtycurved)
sp500_eqtycurved<-na.omit(sp500_eqtycurved)

merge all 3 compounded returns to produce equity curves daily

datatoplotd <- merge(tcs_eqtycurved,infy_eqtycurved,sp500_eqtycurved)
datatoplotd <- na.omit(datatoplotd)
head(datatoplotd)
##             Adjusted Adjusted.1 Adjusted.2
## 2004-08-26 0.9909392   1.097069  0.9788740
## 2004-08-27 0.9743959   1.111088  0.9812480
## 2004-08-30 0.9987939   1.119711  0.9736125
## 2004-08-31 1.0001461   1.118328  0.9781211
## 2004-09-01 0.9999515   1.122373  0.9796004
## 2004-09-02 1.0057736   1.137604  0.9905842
colnames(datatoplotd) <- c("TCS" , "INFOSYS" , "SP500")
plot.zoo(datatoplotd, plot.type = "single" , main="",col = c("red","blue","black"), ylab="cummulative returns", lwd=2)
legend(x="topleft", legend = colnames(datatoplotd), col=c("red" , "blue" ,"black"),lwd = 2)

tcsHist = hist(tcsmonthlyreturns, plot=FALSE, breaks=15)
par(mfrow =c(3,3))
hist(tcsmonthlyreturns, main="", col='cornflowerblue')
hist(infymonthlyreturns, main="", col='cornflowerblue')
hist(sp500monthlyreturns, main="", col='cornflowerblue')
hist(tcsdailyreturns, main="", col='cornflowerblue')
hist(infydailyreturns, main="", col='cornflowerblue')
hist(sp500dailyreturns, main="", col='cornflowerblue')

set.seed(144)
tcs_gwndaily = rnorm(length(tcsdailyreturns), mean=mean(tcsdailyreturns), sd = sd(tcsdailyreturns))
tcs_gwndaily = zoo(tcs_gwndaily, index(tcsdailyreturns))
tcs_gwnmonthly = rnorm(length(tcsmonthlyreturns), mean=mean(tcsmonthlyreturns), sd=sd(tcsmonthlyreturns))
tcs_gwnmonthly = zoo(tcs_gwnmonthly, index(tcsmonthlyreturns))

infy_gwndaily = rnorm(length(infydailyreturns), mean=mean(infydailyreturns), sd = sd(tcsdailyreturns))
infy_gwndaily = zoo(infy_gwndaily, index(infydailyreturns))
infy_gwnmonthly = rnorm(length(infymonthlyreturns), mean=mean(infymonthlyreturns), sd=sd(tcsmonthlyreturns))
infy_gwnmonthly = zoo(infy_gwnmonthly, index(infymonthlyreturns))

sp500_gwndaily = rnorm(length(sp500dailyreturns), mean=mean(sp500dailyreturns), sd = sd(tcsdailyreturns))
sp500_gwndaily = zoo(sp500_gwndaily, index(sp500dailyreturns))
sp500_gwnmonthly = rnorm(length(sp500monthlyreturns), mean=mean(sp500monthlyreturns), sd=sd(sp500monthlyreturns))
sp500_gwnmonthly = zoo(sp500_gwnmonthly, index(sp500monthlyreturns))
tcs_gwnmonthly <- na.omit(tcs_gwnmonthly)
infy_gwnmonthly <- na.omit(infy_gwnmonthly)
sp500_gwnmonthly <- na.omit(sp500_gwnmonthly)

tcs_gwndaily <- na.omit(tcs_gwndaily)
infy_gwndaily <- na.omit(infy_gwndaily)
sp500_gwndaily <- na.omit(sp500_gwndaily)
par(mfrow=c(2,2))
plot.zoo(tcsmonthlyreturns, main="monthly returns on TCS", lwd=2, col="blue", ylim=c(-0.4, 0.4))
abline(h=0)
plot.zoo(tcs_gwnmonthly, main="monthly returns on TCS", lwd=2, col="blue", ylim=c(-0.4, 0.4))
  abline(h=0)
hist(tcsmonthlyreturns, main='', col='cornflowerblue',xlab='returns')
  
hist(tcs_gwnmonthly, main='', col='cornflowerblue',xlab='returns')

par(mfrow =c(1,1))