Vba code for data fetching from nseindia

asp123

New Member
Joined
May 11, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
hello
I am trying to fetch nifty option chain data from www.nseindia.com, with the help of vba code
but couldn't manage it,
Is there anyone who can help me out ..
iam attaching xlsm sheet with vba code which I tried ,could you please let me know what is wrong with the code. code is as below :-

Sub fetch_oc_data_from_nseindia()

Sheet1.Cells(1, 1).Value2 = "Fetching..."
Dim ocURL As String
Dim response As String
ocURL = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY&c=" & Int((900000) + Rnd) + 100000

With CreateObject("MSXML2.XMLHTTP")

.Open "GET", "https://www.nseindia.com/option-chain"
.setRequestHeader "User-Agent:", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
.send
Do Until .readyState = 4: DoEvents: Loop
response = .responseText
.Open "GET", ocURL
.setRequestHeader "User-Agent:", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
.setRequestHeader "Content-Type", "application/json; charset=utf-8"
.send
Do Until .readyState = 4: DoEvents: Loop
response = .responseText
End With

Data = VBA.Split(VBA.Split(VBA.Split(response, "[")(2), "]")(0), "},{")
If UBound(Data) > 0 Then
With Sheet1
.Range("A2").AutoFilter
.Range("A2").CurrentRegion.Offset(2).Clear
End With

For n = LBound(Data) To UBound(Data)
pe = VBA.Split(Data(n), "PE"":{")
If UBound(pe) > 0 Then
pe = VBA.Split(pe(1), "}")
If UBound(pe) > 0 Then
pe = VBA.Split(pe(0), ",")
If UBound(pe) > 0 Then
With Sheetl
For k = LBound(pe) To UBound(pe)
tmp = pe(k)
If tmp Like """expiryDate'" Then
.Cells(n + 3, 1).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """strikePrice'" Then
.Cells(n + 3, 14).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """totalBuyQuantity'" Then
.Cells(n + 3, 15).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """totalSellQuantity'" Then
.Cells(n + 3, 16).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """bidQty'" Then
.Cells(n + 3, 17).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """bidprice'" Then
.Cells(n + 3, 18).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """askprice'" Then
.Cells(n + 3, 19).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """askQty'" Then
.Cells(n + 3, 20).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """change""'" Then
.Cells(n + 3, 21).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """lastPrice'" Then
.Cells(n + 3, 22).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """impliedvolatity'" Then
.Cells(n + 3, 23).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """totalTradedVolume'" Then
.Cells(n + 3, 24).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """changeinOpenInterest'" Then
.Cells(n + 3, 25).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
ElseIf tmp Like """OpenInterest'" Then
.Cells(n + 3, 26).Value2 = VBA.Replace(VBA.Split(tmp, ":")(1), """", "")
End If
Next k
End With
End If
End If
End If
Next n
End If
End Sub





















VBA Code:
[CODE=vba][CODE=vba]
[/CODE][/CODE]
 

Attachments

  • Screenshot 2024-05-11 233319.png
    Screenshot 2024-05-11 233319.png
    29.9 KB · Views: 32

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,225,730
Messages
6,186,698
Members
453,369
Latest member
positivemind

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top