Jul 24, 2011

BSE Bhavcopy with Delivery Quantity

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 (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",
#"LOW","CLOSE","NO_OF_SHRS","DLV_QTY"
 
 
# 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"), "_csv.zip", sep = "")
  fileDELV=paste("SCBSEALL", as.character(myDate, "%d%m"), ".zip", sep = "")
 
  #Generate URL in following format
 #http://www.bseindia.com/Hisbhav/eq161210_csv.zip
 #http://www.bseindia.com/BSEDATA/gross/2010/SCBSEALL1612.TXT
  URLBhav = paste("http://www.bseindia.com/Hisbhav/", fileBHAV, sep = "")
 URLDelv = paste("http://www.bseindia.com/BSEDATA/gross/", as.character(myDate, "%Y"),"/",fileDELV, sep = "")
 
 
  #retrieve Zipped file
  tryCatch({
  #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"
  temp$DATE.y<-NULL
  temp.IV<-subset(temp,select=c("DATE","SC_CODE","SC_NAME","OPEN","HIGH","LOW","CLOSE","NO_OF_SHRS","DLV_QTY"))
 
  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
 file.remove(junkBHAV)
 file.remove(junkDELV)
Created by Pretty R at inside-R.org

4 comments:

  1. We offers stock equity tips, futures & options tips, stocks prices, share tips and stock market prices. All stock exchange, Investment advisory cum financial services Indore.

    Source: Intraday Stock Tips

    ReplyDelete
  2. best share tips, best share tips provider, free share tips, free intraday tips

    ReplyDelete
  3. Hi,
    I am new to R. I am interested to get data(cash/derivates, etc...) from NSE/BSE.
    Could you please send the code and process to do this.

    Thanks.

    ReplyDelete
  4. Its necessary to take guidance from the blogs to get the live market updates and if you are new in trading and wants to get live market news then use Stock Tips.

    ReplyDelete