Financial Analytics using R
The Project
The aim of this project is to understand how R can be used to perform stock analysis using various statistical tools. For this project, I have chosen stocks listed in National Stock Exchange, India (NSE).
Getting Data
OHLCV of the stock will be imported from Yahoo Finance, through “quantmod” package. List of stock tickers can be obtained from “https://www.nseindia.com/corporates/content/securities_info.htm”.
COncatenate the “SYMBOL” column with “.NS” to get Yahoo Ticker. We will importing these tickers as a column data into R.
stocks <- read.csv("EQUITY_L.csv")
stocks <- stocks$TICKER
print(paste("Number of listed stocks is",length(stocks), sep = " "))## [1] "Number of listed stocks is 1631"
We will use “getsymbols()” function from “quantmod” library to import OHLCV of all these stocks from Yahoo Finance. From the OHLCV object, we will extract the daily closing prices of the stocks from 1st January 2007 till date. Since there are 1631 stocks listed, we will run a “for()” loop on these stocks to import the data.
library(dplyr)
library(quantmod)
close <- NA
dates <- as.Date(c("2007-01-01", "2018-07-20"))
days <- dates[2]-dates[1]
for(i in c(1:length(stocks))){
tryCatch(
temp <- getSymbols(as.character(stocks[i]), from = dates[1], to = dates[2], auto.assign = F),
error = function(e){temp = NA})
tryCatch(temp <- temp[,4],
error = function(e){temp = data.frame(rep(NA, days))})
close <- cbind(close, temp)
}
close <- close[,2:1632]
dim(close)## [1] 2855 1631
We collected the closing prices of approxiately 11 years 7 months and 1631 currently listed stocks. If we check the data, there are NA values - the reason is the stock was not traded on that day. The rownames are dates and column names are the stock names. It took me around 30 minutes to collect the data, so I will save it, so that I can use it later.
write.csv(close, "ClosingPrices.csv")