You are on page 1of 2

#Define start and end dates, and convert them into date format startDate = as.

Date("2013-04-17", order="ymd") endDate = as.Date("2013-04-25", 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 = "") monthfilename=paste(as.character(myDate, "%y%m"),".csv", sep = "") downloadfilename=paste("cm", toupper(as.character(myDate, "%d%b%Y")), "bhav.csv", sep = "") temp ="" #Generate URL myURL = paste("http://nseindia.com/content/historical/EQUITIES/", as.character(myDate, "%Y"), "/", toupper(as.character(myDate, "%b")), "/", downloadfilename, ".zip", sep = "") #retrieve Zipped file tryCatch({ #Download Zipped File download.file(myURL,zippedFile, quiet=TRUE, mode="wb") #Unzip file and save it in temp temp <- read.csv(unzip(zippedFile), sep = ",") #Rename Columns Volume and Date colnames(temp)[9] <- "VOLUME" colnames(temp)[11] <- "DATE" #Define Date format temp$DATE <- as.Date(temp$DATE, format="%d-%b-%Y") #Reorder Columns and Select relevant columns temp<subset(temp,select=c("DATE","SYMBOL","OPEN","HIGH","LOW","CLOSE","L AST","VOLUME")) #Write the BHAVCOPY csv - datewise write.csv(temp,file=filenameDate,row.names = FALSE)

sep=". col. eol="\n". eol="\n".dir(pattern="cm") file.names = FALSE. "left")) }.#Write the csv in Monthly file if (file.sep=".table(temp.table(temp. append=TRUE) }else { write.Bhavcopy junk <. row. row.exists(monthfilename)) { write.names = FALSE. "-No Record")) } ) myDate <. append=FALSE) } #Write the file Symbol wise #Print Progress #print(paste (myDate.file=monthfilename.myDate+1 #print(paste(myDate. endDate-myDate.file=monthfilename.".remove(junk) . col. error=function(err){ #print(paste(myDate. "Next Record")) } #Delete temp file .names = FALSE.".names = TRUE. "-Done!".