BSE Bhavcopy with Delivery Quantity

[This article was first published on My Paper Trades, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

One of my TI forum members IV had a requirement for BSE Quotes along with Delivery Quantity. This made me implement “merge” function of R coding (thanks to the great work done by people behind various packages and guidance available on R Mailing lists).

The task involves downloading files from two separate links and merging it on the basis of Symbol on any given date.

The output is a single file for one trading day. With a slight tweak in the code, single file can be generated for a week, a month, a year , or from beginning to end. If anyone is interested, let me know, and will offer the solution for the same

Without much write-up here is the code for the same.

For those, who want to take a shortcut, here are historical quotes

You can download R Code here

#This code merges BHAVCOPY and Total Deliverable Quantity from BSE
#The output file contains "DATE","SC_CODE","SC_NAME","OPEN","HIGH",
# Start ---------------- User Defined Variables
#Define Working Directory, where files would be saved
setwd('D:/BSE Equity')
#Define start and end dates, and convert them into date format
startDate = as.Date("2010-01-01", order="ymd")
endDate = as.Date("2011-07-31", order="ymd")
#Define Download folder, use \\ instead of windows standard \
filepath="D:\\BSE Equity\\New\\"
# End ---------------- User Defined Variables
junkBHAV <- dir(pattern="EQ")
junkDELV <- dir(pattern="SCBSEALL")
#work with date, month, year for which data has to be extracted
myDate = startDate
#Define temporary files
zippedBhav <- tempfile()
zippedDelv <- tempfile()
while (myDate <= endDate){
  filenameDate = paste(as.character(myDate, "%Y%m%d"), sep = "")
 fileBHAV=paste("eq", as.character(myDate, "%d%m%y"), "", sep = "")
 fileDELV=paste("SCBSEALL", as.character(myDate, "%d%m"), ".zip", sep = "")
  #Generate URL in following format
  URLBhav = paste("", fileBHAV, sep = "")
 URLDelv = paste("", as.character(myDate, "%Y"),"/",fileDELV, sep = "")
  #retrieve Zipped file
  #Download Zipped File
  download.file(URLBhav,zippedBhav, quiet=TRUE, mode="wb")
  download.file(URLDelv,zippedDelv, quiet=TRUE, mode="wb")
  bhav <- read.csv(unzip(zippedBhav), header=TRUE, sep=",") 
  #Add Date Column
  bhav <- cbind(DATE=myDate,bhav)
  #Select only SY_Type="Q", or Equity Quotes
  bhav <- subset(bhav, SC_TYPE=="Q")
  delv <- read.csv(unzip(zippedDelv), header=TRUE, sep="|")
  colnames(delv)[2] <- "SC_CODE"
  colnames(delv)[3] <- "DLV_QTY"
  colnames(delv)[4] <- "DLV_VAL"
  colnames(delv)[5] <- "VOLUME"
  colnames(delv)[6] <- "TURNOVER"
  colnames(delv)[7] <- "DLV_PCT"
  temp<-merge(bhav,delv, by.x="SC_CODE", by.y="SC_CODE")
  colnames(temp)[2] <- "DATE"
  write.csv(temp.IV,file=paste(filepath,filenameDate, ".csv",sep=""),row.names = FALSE)
  #Print Progress
  print(paste (myDate, "-Done!", endDate-myDate, " days left"))
 }, error=function(err){
  print(paste(myDate, "-No Record", endDate-myDate, " days left"))
  myDate <- myDate+1
 #Delete temp file

To leave a comment for the author, please follow the link and comment on their blog: My Paper Trades. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)