Pages

Saturday, September 2, 2017

R code to do Basic Stock Analysis

library(quantmod)
require(quantmod)

#! to disable warnings
options("getSymbols.yahoo.warning"=FALSE)

#! add list of stocks which you want to review
stocklist <- c("SBIN.NS","ITC.NS","MARUTI.NS")
j=1
RTable= array(c(stocklist,1:10),c(length(stocklist),2))

#! get the data for all the stocks and perform the basic calculations
for (i in stocklist)
{
test<-as.data.frame(getSymbols(Symbols = i,from ='2015-01-01',to = Sys.Date(), env = NULL))
ptest <- test[,6]
RTable[j,2] = as.numeric(ptest[length(ptest)] / ptest[1])
j = j+1
}

#! plot the output
print (RTable)
barplot(type.convert(RTable[,2]),names.arg = RTable[,1])