Extracting EOD Data from BSE

July 19, 2011

(This article was first published on My Paper Trades, and kindly contributed to R-bloggers)

Earlier, I had worked around to download Bhavcopy from NSE. Now, I would make a similar attempt to download the BSE Bhavcopy

Objective: Download Bhavcopy (Equity) from http://www.bseindia.com and save only relevant columns Date, Symbol, Name, Open, High, Low, Close, Last and Volume.

Here is the R Code for the same

#Define Working Directory, where files would be saved
#Define start and end dates, and convert them into date format
startDate = as.Date("2011-05-26", order="ymd")
endDate = as.Date("2011-06-06", order="ymd")
#work with date, month, year for which data has to be extracted
myDate = startDate
zippedFile <- tempfile()
while (myDate <= endDate){
filenameDate = paste(as.character(myDate, "%y%m%d"), ".csv", sep = "")
downloadfilename=paste("eq", as.character(myDate, "%d%m%y"), "_csv.zip", sep = "")
monthfilename=paste(as.character(myDate, "%y%m"),".csv", sep = "")
temp =""
#Generate URL
myURL = paste("http://www.bseindia.com/bhavcopy/", downloadfilename, sep = "")
#retrieve Zipped file
#Download Zipped File
download.file(myURL,zippedFile, quiet=TRUE, mode="wb")
#Unzip file and save it in temp
temp <- read.csv(unzip(zippedFile))
#Rename Columns Volume and Date
colnames(temp)[1] <- "SYMBOL"
colnames(temp)[2] <- "NAME"
colnames(temp)[12] <- "VOLUME"
#Define Date format
temp$DATE <- as.Date(myDate, format="%d-%b-%Y")
#Reorder Columns and Select relevant columns
#Write the BHAVCOPY csv - datewise
write.csv(temp,file=filenameDate,row.names = FALSE)
#Write the csv in Monthly file
if (file.exists(monthfilename))
write.table(temp,file=monthfilename,sep=",", eol="\n", row.names = FALSE, col.names = FALSE, append=TRUE)
write.table(temp,file=monthfilename,sep=",", eol="\n", row.names = FALSE, col.names = TRUE, append=FALSE)
#Write the file Symbol wise
#Print Progress
#print(paste (myDate, "-Done!", endDate-myDate, "left"))
}, error=function(err){
#print(paste(myDate, "-No Record"))
myDate <- myDate+1
#print(paste(myDate, "Next Record"))
#Delete temp file - Bhavcopy
junk <- dir(pattern="EQ")

Created by Pretty R at inside-R.org

To leave a comment for the author, please follow the link and comment on their blog: My Paper Trades.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.