Using VBA and JSON fetch data from multiple url's simultaneously

dlptestx

New Member
Joined
Jul 23, 2021
Messages
1
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hello,

I am trying to get live stock data from two url's into excel.
I am able to do this for 1 url however I would like to do it for two url's simultaneously, so that there is no/ minimal time difference
Below is my code for fetching data for 1 url "webURL_Nifty" and then plot into excel sheet.

can someone please help here.

VBA Code:
Public Sub pullNIFTYData()

    Dim Json_N As Object
    Dim Json_BN As Object
    Dim webURL_Nifty As String, mainString, subString, webURL_BN
    
    webURL_Nifty = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
    webURL_BN = "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY"
    subString = "Resource not found"
    
FetchAgain:
    
    With CreateObject("msxml2.xmlhttp")
        .Open "GET", webURL_Nifty, False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
        
        On Error Resume Next
        .send
        
        mainString = .responsetext
    
        If InStr(mainString, subString) <> 0 Then
            Application.Wait (Now + TimeValue("00:00:03"))
            GoTo FetchAgain
    
        Else    
            Set Json_N = JsonConverter.ParseJson(mainString)
    
        End If
    End With
    
    Call Nifty_Data(Json_N)

End Sub


Public Sub Nifty_Data(JN As Object)

    Dim dtArr() As String
    Dim ws As Worksheet
    
    Set ws = Worksheets("Sheet1")

    ws.Range("B1") = JN("records")("underlyingValue")

    dtArr = Split(JN("records")("timestamp"), " ")

    ws.Range("E1") = FormatDateTime(dtArr(0), 2)
    ws.Range("H1") = FormatDateTime(dtArr(1), 4)
    ws.Range("K1") = JN("records")("expiryDates")(1)
    ws.Range("N1") = Abs(JN("filtered")("PE")("totOI") / JN("filtered")("CE")("totOI"))
    ws.Range("Q1") = Abs(JN("filtered")("PE")("totVol") / JN("filtered")("CE")("totVol"))
    
    j = 4
    
    For i = 1 To JN("records")("data").Count


        If JN("records")("data")(i)("expiryDate") = JN("records")("expiryDates")(1) Or JN("records")("data")(i)("expiryDate") = JN("records")("expiryDates")(2) Or JN("records")("data")(i)("expiryDate") = JN("records")("expiryDates")(3) Or JN("records")("data")(i)("expiryDate") = JN("records")("expiryDates")(4) Or JN("records")("data")(i)("expiryDate") = JN("records")("expiryDates")(5) Then

'            If Abs(Json("records")("data")(i)("strikePrice") - Json("records")("underlyingValue")) < 400 Then

                k = 1
            
                        If IsObject(JN("records")("data")(i)("CE")) Then
                            
                            ws.Cells(j, k + 6) = JN("records")("data")(i)("CE")("change")
                            ws.Cells(j, k + 5) = JN("records")("data")(i)("CE")("lastPrice")
                            ws.Cells(j, k + 4) = JN("records")("data")(i)("CE")("impliedVolatility")
                            ws.Cells(j, k + 3) = JN("records")("data")(i)("CE")("totalTradedVolume")
                            ws.Cells(j, k + 2) = JN("records")("data")(i)("CE")("changeinOpenInterest")
                            ws.Cells(j, k + 1) = JN("records")("data")(i)("CE")("openInterest")
                            
                        Else
                            ws.Cells(j, k + 6) = ""
                            ws.Cells(j, k + 5) = ""
                            ws.Cells(j, k + 4) = ""
                            ws.Cells(j, k + 3) = ""
                            ws.Cells(j, k + 2) = ""
                            ws.Cells(j, k + 1) = ""
                        End If
                            
                        If IsObject(JN("records")("data")(i)("PE")) Then
                            ws.Cells(j, k + 13) = JN("records")("data")(i)("PE")("expiryDate")
                            ws.Cells(j, k + 12) = JN("records")("data")(i)("PE")("openInterest")
                            ws.Cells(j, k + 11) = JN("records")("data")(i)("PE")("changeinOpenInterest")
                            ws.Cells(j, k + 10) = JN("records")("data")(i)("PE")("totalTradedVolume")
                            ws.Cells(j, k + 9) = JN("records")("data")(i)("PE")("impliedVolatility")
                            ws.Cells(j, k + 8) = JN("records")("data")(i)("PE")("lastPrice")
                            ws.Cells(j, k + 7) = JN("records")("data")(i)("PE")("change")
                            ws.Cells(j, k) = JN("records")("data")(i)("strikePrice")
                            
                        Else
                            ws.Cells(j, k + 12) = ""
                            ws.Cells(j, k + 11) = ""
                            ws.Cells(j, k + 10) = ""
                            ws.Cells(j, k + 9) = ""
                            ws.Cells(j, k + 8) = ""
                            ws.Cells(j, k + 7) = ""
                            ws.Cells(j, k) = ""
                        End If
                j = j + 1
'            End If

        End If

    Next i
    
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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