Download Historical Data from NSE in Excel VBA

#21
The bhav copy in zipped format has a size ~250-450KB. Downloading such a file once a day is no big deal. NSE doesn't allow download of such files without using the browser. Therefore I fake both the user-agent and referer to get around. In case you are downloading this data since 01-01-2010 since when NSE started publishing the bhav copy in zip format, this should take less than 5 mins. I download the zip file every day, unzip it and upload it in Amibroker. That way I have daily OHLC & Volume data for all NSE stocks & indices from 01-01-2010.
-- Hi, I was looking an option to download the historical data from NSE India , and I came across this site. this post gave me answer which I am looking for . Can you please share the process to download the on daily basis as you have mentioned above.
It would be a great help.
 
#22
Hello All,
NSE has recently change the method of retrieving historical data from "GET" method to "POST" method. Hence, the historical data can no longer be downloaded by sending scrip code and date in the link. Based on various programs, I have tried to develop a VBA code which worked for me.

The VBA code below can be automated to suit once need.

Regards,
Amod
----------------------------------------
Public Sub downloadData()
'Open an excel sheet and rename one of the sheets as "DailyData". The data will be
'copied to that sheet

Dim frmDate As String, toDate As String, scrip As String
frmDate = DateSerial(2017, 5, 22)
frmDate = Format(frmDate, "dd-mm-yyyy")
toDate = DateSerial(2017, 5, 26)
toDate = Format(toDate, "dd-mm-yyyy")
scrip = "LUPIN"
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("DailyData")

'Call subroutine to download the data
getNSE_post frmDate, toDate, 1, 1, scrip, ws

End Sub


Sub getNSE_post(frmDate As String, toDate As String, nRow As Integer, _
nCol As Integer, scrip As String, ws As Worksheet)

Dim ie As Object
Dim frm As Variant
Dim element, submitInput As Variant
Dim rowCollection, htmlRow As Variant
Dim rowSubContent, rowSubData As Variant
Dim i, j, k, pauseTime As Integer
Dim anchorRange As Range, cellRng As Range
Dim start

Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://www.nseindia.com/products/content/equities/equities/eq_security.htm"
While ie.readyState <> 4: DoEvents: Wend

'try to get form by ID
Set frm = ie.document.getElementById("histForm")

ie.Visible = True
ie.document.getElementById("dataType").Value = "priceVolumeDeliverable"
ie.document.getElementById("symbol").Value = scrip
ie.document.getElementById("segmentLink").Value = 3
ie.document.getElementById("symbolCount").Value = 1
ie.document.getElementById("series").Value = "EQ"
'ie.document.getElementById("dateRange").Value = "day"
ie.document.getElementById("rdPeriod").Checked = True
ie.document.getElementById("fromDate").Value = frmDate
ie.document.getElementById("toDate").Value = toDate

'Pause For User To See Entry
pauseTime = 2 ' Set duration in seconds
start = Timer ' Set start time.
Do While Timer < start + pauseTime
DoEvents ' Yield to other processes.
Loop

For Each submitInput In ie.document.getElementsByTagName("INPUT")
If InStr(submitInput.getAttribute("onclick"), "submitData") Then
submitInput.Click
Exit For
End If
Next

Set anchorRange = ws.Cells(1, 1)
i = -1 'The header row needs to be omitted
j = 0
Set rowCollection = ie.document.getElementsByTagName("tr")
For Each htmlRow In rowCollection
Set rowSubContent = htmlRow.getElementsByTagName("td")
k = 0
For Each rowSubData In rowSubContent
If j = 2 Or j = 8 Or j = 14 Then
anchorRange.Offset(i, k).Value = rowSubData.innerText
k = k + 1
ElseIf j >= 4 And j <= 6 Then
anchorRange.Offset(i, k).Value = rowSubData.innerText
k = k + 1
ElseIf j = 10 Then
anchorRange.Offset(i, k).Value = rowSubData.innerText
anchorRange.Offset(i, k).Value = Replace(anchorRange.Offset(i, k).Value, ",", "")
k = k + 1
End If
j = j + 1
Next rowSubData
j = 0
i = i + 1
Next htmlRow
Exit Sub

errHandler:
Application.Calculation = xlCalculationAutomatic
MsgBox Err.Description, vbExclamation, "Download Error: the remote server did not respond!"
Err.Clear
Exit Sub

End Sub
your code is working only in debug mode but not in run mode . Please can you help me??