Sample code for 30+ languages & platforms
Visual Basic 6.0 Requires Chilkat v11.0.0+

Schwab: Check equity market hours for a date

See more Schwab Examples

Looks up the US equity calendar for a selected trading date. Reads each product’s open flag and the actual regular-session start and end, including their offsets. This distinguishes a holiday closure from a shortened session. The open flag describes the requested date, not whether the market is open at this instant.

Use the access token saved by desktop authorization. Enable the Market Data API for the Schwab app. Use the same Chilkat Secrets store as the OAuth example.

Schwab API specification

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0

Dim vault As New ChilkatSecrets
vault.Location = "local_manager"
vault.AutoSplit = 1
Dim entry As New ChilkatJsonObject
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
Dim tokens As New ChilkatJsonObject
success = vault.GetSecretJson(entry,tokens)
If (success <> 1) Then
    Debug.Print "Could not read the token record. Check the store and complete desktop authorization first."
    Exit Sub
End If

Dim accessToken As String
accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
    Debug.Print "Run the Schwab authorization or refresh example to save an access token."
    Exit Sub
End If

Dim http As New ChilkatHttp
http.RequireSslCertVerify = 1
http.AuthToken = accessToken

'  Supply the calendar date you want to check, for example a US holiday.
Dim clock As New CkDateTime
success = clock.SetFromCurrentSystemTime()
Dim marketDate As String
marketDate = clock.GetAsIso8601("YYYY-MM-DD",0)
Dim resp As New ChilkatHttpResponse
Dim query As New ChilkatHttpRequest
query.HttpVerb = "GET"
query.AddParam "date",marketDate
success = http.HttpReq("https://api.schwabapi.com/marketdata/v1/markets/equity",query,resp)
If ((success <> 1) Or (resp.StatusCode <> 200)) Then
    If (resp.StatusCode = 0) Then
        Debug.Print "No HTTP response. Check connectivity."
    Else
        Debug.Print "Schwab returned HTTP " & resp.StatusCode
        If (resp.StatusCode = 401) Then
            Debug.Print "Refresh the access token or repeat desktop authorization."
        End If

    End If

    Exit Sub
End If

Dim reply As New ChilkatJsonObject
success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    Debug.Print "Schwab returned unexpected JSON."
    Exit Sub
End If

Dim products As New ChilkatJsonObject
success = reply.ObjectOf2("equity",products)
If ((success <> 1) Or (products.Size = 0)) Then
    Debug.Print "No equity market schedule returned for " & marketDate
    Exit Sub
End If

Dim product As New ChilkatJsonObject
Dim i As Long
i = 0
Do While (i < products.Size)
    success = products.ObjectAt2(i,product)
    Debug.Print products.NameAt(i) & "  " & product.StringOf("date")
    If (product.BoolOf("isOpen") = 1) Then
        Dim j As Long
        j = 0
        Dim count As Long
        count = product.SizeOfArray("sessionHours.regularMarket")
        If (count <= 0) Then
            Debug.Print "Open date, but no regular-session times were supplied."
        End If

        Do While (j < count)
            product.J = j
            Debug.Print "Regular session: " & product.StringOf("sessionHours.regularMarket[j].start") & " to " & product.StringOf("sessionHours.regularMarket[j].end")
            j = j + 1
        Loop
    Else
        Debug.Print "Closed for this date."
    End If

    i = i + 1
Loop