Download Historical Data from NSE in Excel VBA

#1
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
 
#5
Hello,
NSE does not allow to download data for more than 365 days at a time.

Hence, you cannot download from 01-Jan-14 to 31-Dec-16. Rather, try in two steps, 01-Jan-2014 to 31-Dec-15 and then 01-Jan-15 to 31-Dec-16.

I am trying to implement a loop. Will upload excel when I have added few more features.

Regards,
Amod
 
#7
Hello All,

I have been developing this excel version. The codes are not commented though, though anyone familiar with VBA will be able to follow.

I think I cannot upload the Excel file - only option available are IMAGE files.

Regards,
Amod
 
#8
Hello,
NSE does not allow to download data for more than 365 days at a time.

Hence, you cannot download from 01-Jan-14 to 31-Dec-16. Rather, try in two steps, 01-Jan-2014 to 31-Dec-15 and then 01-Jan-15 to 31-Dec-16.

I am trying to implement a loop. Will upload excel when I have added few more features.

Regards,
Amod
sorry bro
you can not download this much data (from 01-Jan-14 to 31-Dec-16) in 2 steps (as you said) but in 3 steps :D
 
#9
The problem with we traders/people is that we choose a difficult/tedious way to do a simple work. Just like here in this thread, the member is choosing a difficult/tedious way to download historical eod data of various NSE stocks.

I somewhere read a quote from Bill Gates — 'I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.'

I am a lazy person, but, I am looking for a person, who is lazy like me or lazier than me, to do above job :lol: :D
 

papa

New Member
#10
The problem with we traders/people is that we choose a difficult/tedious way to do a simple work. Just like here in this thread, the member is choosing a difficult/tedious way to download historical eod data of various NSE stocks.

I somewhere read a quote from Bill Gates — 'I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.'

I am a lazy person, but, I am looking for a person, who is lazy like me or lazier than me, to do above job :lol: :D
If you are not ready to help then please do not give an excuse.