Analysing Stock Market Returns

In the previous post, we have seen how to import the closing prices of the listed stocks of NSE. In this post we will see, how to fit models for predicting stock prices and creating a portfolio and analyse the risk. To do that, let us import the data “ClosingPrices.csv” as “close”, into a xts object.

library(quantmod)
## Warning: package 'quantmod' was built under R version 3.4.4
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
close <- read.csv("ClosingPrices.csv", stringsAsFactors = F)
close <- xts(close, order.by=as.Date(close$Index),"%d-%m-%Y")
close <- close[,2:1632]
mode(close) <- "numeric"
dim(close)
## [1] 2856 1631

From these stock prices, let us calculate daily returns of these listed stocks. For this, we shall use “diff(log())”. This function calculates logarithm of gross returns. Let us call the result as “returns”.

returns <- diff(log(close))

It took around 15 minutes to get these returns. So I will save this file and continue next article from here.

write.zoo(returns, "returns.csv", sep = ",")

2018-07-24