1

I am a day trader based in INDIA. I am using R to do my research. I want to download the End of the Day(EOD) stock prices for different stocks. I was using Quandl and quantmod but was not satisfied with them ( they are OK for historical data but not for EOD quotes). After much research I found out that the EOD for NSE(national stock exchange of india) can be found in the so called "bhav copy" that can be downloaded daily from its website. The URL, for 30th APRIL, is:

https://www.nseindia.com/content/historical/EQUITIES/2018/APR/cm30APR2018bhav.csv.zip

I have two questions:

1) If I type this in the address box of google chrome and execute, it throws a pop up window that asks where to store the csv file. How do I automate this in R? If I just enter the URL as an argument for read.csv, will it suffice?

2) The bhav copy is updated daily. I want to write a function in R that automates this downloading daily. But the URL changes daily( the above URL is only for 30th APRIL 2018). The function will take the current date as an argument. How can I create a one one map to the date and the URL for that particular date? In other words, the URL for date dt is:

https://www.nseindia.com/content/historical/EQUITIES/2018/APR/cmdtAPR2018bhav.csv.zip

the R function f(dt) should create the URL for that particular date and download the csv file.

Very many thanks for your time and effort....

2 Answers2

0

download.file(url, destfile) should be what you need to download the data from the URL in R. Then you can use read.csv. You may need to use unzip() before processing it, judging by the URL you provided.

If you feel like it, you can use fread from the data.table library to pass the url directly, but if it's a zip file then the first option is probably better for you.

As for the URL and processing dates, the lubridate library will be handy for parsing dates.

Oliver Frost
  • 827
  • 5
  • 18
0

Package nser solves your problem.

To download and read today's bhavcopy use bhavtoday

library(nser)
bhavtoday

To download and read historical bhavcopy of Equity segment

bhav("30042018")

bhavcopy of F&O segment

fobhav("30042018")

You can also use RSelenium to download bhavcopy zip file using function bhavs.

Package link https://cloud.r-project.org/web/packages/nser/index.html

Nad Pat
  • 3,129
  • 3
  • 10
  • 20