Jul 19, 2011

Extracting EOD Data from BSE

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

1 comment:

  1. Awesome. Thank you for the code. Do u do algorithmic trading with R?