Automating the download of share price data from NSE
Share prices on NSE are available from this page. The challenge is to collect share price data for a given index over a period of time. We can do it using two methods.
Method One - Filling the form on the web page, and then submitting the data, and then downloading the CSV.
This is a standard and logical technique. It comprises the following steps.
1. Open NSE India on Internet Explorer and navigate to this page. (Why Internet Explorer? That's because VBA has readymade methods for manipulating elements of a web page that are opened in Internet Explorer. You can automatically fill forms and hit the submit button of web pages, provided they are opened using IE).
2. Fill up the relevant dates in the form fields (Start Date and End Date) and hit the submit (Get Data) button.
3. Once the Page is loaded, hit the Download file in csv format link using VBA, and you will have downloaded the data on your machine.
I will expand upon this method in the next article.
Method Two - Hacking the process and using a Web Query from Excel
Excel has a built-in feature for importing data from the web directly. This is the Web Query feature, to be found under the Data Tab, Import data from Web option. This feature allows you to directly import data from a web page if you specify the url correctly. It has a few options, including letting you select specific tables from the page, letting you choose the format in which you want to import the data - No Format means that the data will be downloaded without the formatting, Rich Text Formating means the fonts from the web page will be preserved on the excel, and full HTML formatting means the web page will be almost exactly reproduced as it is on the excel sheet. I would recommend choosing 'Full HTML formatting'. However, as of now we can't use this method because we don't know what url to target.
The problem broken down - Which URL should I target to get index data between a specific date range from NSE ?
Assuming that you want to download index data for the CNX Nifty between 1st January 2014 and 1st October 2014, and you are going to use a web query to download the final data from the url, the problem now reduces to guessing the correct url on NSE for a particular index's data (in this case NIFTY) for a particular date range (start date: 1st January 2014 to 1st October 2014).
Hacking the URL from Javascript for getting the dates right
1. Have a look at this page and click on view source. You will see that the function "Load Indices Data " is loading the results from the database on the webpage (<input type="image" onclick="loadindicesdata()" class="getdata-button" src="/common/images/btn-get-data.gif" id="get"/>) .
2. Search for this function in the remaining javascript files. You will find it defined in this file (http://www.nseindia.com/products/resources/js/pepb.js)
3. The loadIndicesData() function prefixes a url at the beginning that goes something like (http://www.nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?) and then appends three parameters, and produces a result based on your inputs for those three parameters. A little bit of trial and error will let you figure out the correct way of passing these parameters.
(a) indexType -> This is the name of the Index (CNX NIFTY, CNX 100, CNX 200 etc etc)
(b) fromDate -> This is the start date. The format is mm-dd-yyyy
(c) toDate -> This is the end date. The format is again mm-dd-yyyy
The final url, for example, if you are searching for CNX Nifty data between 1st January 2014 and 1st October 2014, will be (http://www.nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?indexType=CNX%20100&fromDate=01-01-2014&toDate=01-10-2014)
Use this url in your web query, and download the data from the web page to the excel sheet. Make life easier.
The sample file can be accessed by clicking on the link below.
InsideIIM NSE Data Download Demo
I will leave it to you to make the dates and index names configurable.
Read this article by Anup Kumar Agarwal on Excel Tips and Tricks
Read this article on how to send an email automatically through excel
Comments