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, "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
[/CODE][/CODE]
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, "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]