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

16 comments:

  1. BigProfitbuzz is an Indian stock market advisory firm. BigProfitbuzz proven month after month that trading and investing in stock market can be profitable whether market overall is bullish or bearish. BSE and NSE
    BigProfitbuzz facilitates its clients in their wealth creation process. BigProfitbuzz provides recommendations for Stocks- Cash and F&O traded in NSE and Commodity MCX and NCDEX. We at BigProfitbuzz are a group of professionals with expertise in Technical and Fundamental analysis.
    stock market tips ,share tips, stock tips, share market tips, equity tips india, bse nse tips, free stock market tips
    Regards
    BigProfitbuzz Team

    ReplyDelete
  2. Commodity trading is done in MCX and NCDEX. Commodity traders dealing in GOLD, SILVER, COPPER etc should look for some commodity tips from the reliable sources so as to make their trading sessions as profitable sessions. Commodity market is picking pace at very rapid speed and offers very good returns.

    ReplyDelete
  3. I was very encouraged to find this site. I wanted to thank you for this special read.
    Commodity tips
    Stock Market
    Share Market
    Nse Tips
    Bse Tips

    ReplyDelete
  4. Bigprofitbuzz, stock tips, share tips, technical analysis of stocks, share market tips, share, stock SHARE MARKET NSE BSE FREE TIPSmarket, share trading tips, stock market tips, stock trading tips, stocks, shares, investing, free tips, free stock tips, share market trading, stock market trading, Nse and Bse.
    Regards
    BIGPROFITBUZZ TEAM

    ReplyDelete
  5. 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
  6. Share Market Investment made profitable by SHRISTOCKTIPS- Get NSE/BSE Tips via SMS and Yahoo Messenger. We give daily stock market tips for future and cash segments. As we said to make sell position at a higher level of 5450-5500 who sold, made a very handsome profit in NIFTY. Now again we proved that whatever the marketSHARE MARKET, STOCK TIPSwe will get very handsome profit. Now for the coming week traders can make a buy position in NIFTY around 5500-5550 for the target of 5750-5850 with keeping the stoploss of 5450. Because now Govt. is trying to make some arrangements to make the rupee stronger. So our strategy is to buy all NIFTY 50 stocks with the stoploss of 5450 in NIFTY. Last Thursday & Friday there is not any kind of sell signal in the market. This shows there is strong buying at the lower level. So keep a plus position and book profit when you think it is up to your expectation. To get our trial you can visit our website.
    Regards
    SHRISTOCKTIPS TEAM

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

    ReplyDelete
  8. BigProfitbuzz is an Indian stock market advisory firm. BigProfitbuzz proven month after month that trading and investing in stock market can be profitable whether market is bull or bear. Those who made a buy at lower level of 5300-5250 are counting their handsome profits. Always keep long position at the bottom. By following this strategy youSHARE TIPS, INTRADAY SURE TIPSwill definitely earn good profit. For the present market you can make a buy position around 5650-5700 with stoploss of 5500 for target of 5900-6000. Always trade with stoploss. NIFTY is showing the consolidation you can buy till the last level & wait with the stoploss for book profits. We advise not to make any sell position in NIFTY otherwise you cannot stop your losses. Always trade with the trend because the trend is your only friend in the market. If want stock & other calls then you can fill our trial form & earn some good profit.
    Regards
    BIGPROFITBUZZ TEAM

    ReplyDelete
  9. Share Market Investment made profitable by SHRISTOCKTIPS- Get NSE/BSE Tips via SMS and Yahoo Messenger. We give daily stock market tips for future and cash segments. NIFTY is trading unexpectedly in positive direction in last week. Though their are negative near form the US also still trading with positive biasMCX NCDEX, INTRADAY TIPSwe advise to trade with an intraday view only not with the positional view. Traders can make a intraday position as per their capital with the current view of the market. For proper advice you can fill our trail form & get the calls accurately. Otherwise trade with the trend of the market but do not make any big position or wait for NIFTY to clear the trend. Shristocktips is here for maximize your capital with the appropriate risk.
    Regards
    SHRISTOCKTIPS TEAM

    ReplyDelete
  10. BigProfitbuzz is an Indian stock market advisory firm. BigProfitbuzz proven month after month that trading and investing in stock market can be profitable whether market is bull or bear. Those who made a buy at Again and again we are giving greatSHARE TIPS, COMMODITY TIPScalls with good profit. For the coming week we suggest not to overconfident with the buy position .Trade with the stop losses with the view of intraday only. You can take our two days trial also to get proper advise. If you think we can make some profit for you then you can join our paid services to increase your capital with low risk. In current scenario NIFTY is showing positive sigh still negative news from the US. So stay away from the market Or take our trial for proper support and service.
    Regards
    BIGPROFITBUZZ TEAM

    ReplyDelete
  11. BigProfitbuzz is an Indian stock market advisory firm. BigProfitbuzz proven month after month that trading and investing in stock market can be profitable whether market is bull or bear. For the coming week Trader can make a buy position in the NIFTYINTRADAY STOCK TIPS, NSE BSE TIPSOne can buy NIFTY at 6000-6050 with the stoploss of 5900 for the target of 6200-6300 .Trader can also make a buy position in NIFTY stocks with stoploss to their capacity but try to buy at the prescribed NIFTY level. Always keep stoploss with your trade. You can take our two days trial to check our accuracy. For further update visit our website.
    Regards
    BIGPROFITBUZZ TEAM

    ReplyDelete
  12. BigProfitbuzz is an Indian stock market advisory firm. BigProfitbuzz proven month after month that trading and investing in stock market can be profitable whether market is bull or bear. For the coming week Trader can make a buy position in the NIFTY. One can buy NIFTY at 6200-6250 with the stoploss of 6100 for the target of 6500-6600. Trader SHARE TIPS, COMMODITY TIPScan also make a buy position in NIFTY stocks with stoploss to their capacity but try to buy at the prescribed NIFTY level. Always keep stoploss with your trade. Always keep in my mind you risk taking capacity. You can take our two days trial to check our accuracy. For further update visit our website.
    Regards
    BIGPROFITBUZZ TEAM

    ReplyDelete
  13. 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
  14. 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
  15. Commodity market is a place where many new stock market traders join the share trading league daily and trade daily in Commodities. Intraday traders, stock market investors and market beginners should understand the meaning of the stock market for beginners cutting the learning curve but they should have proper knowledge of share market so you can find best tips from here...
    Commodity Tips Provider In India
    genuine mcx tips provider

    ReplyDelete
  16. SUPER POWER TIP BUY PC JEWELLERS FUTURE ABOVE 323 TARGET 325.50 WITH SL 320 Stock Advisory Company Earn Money in Share Market Miss Call 7879881122.
    SUPER POWER FOLLOWUP: PC JEWELLER FUT ROCKSSSSSSSSS HIT ULTIMATE TG 325.50 BOOK FULL PROFIT

    ReplyDelete