B4X Requires Chilkat v11.0.0+
B4X
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.
Chilkat B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim vault As ChilkatSecrets
vault.Initialize("vault")
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As ChilkatJsonObject
entry.Initialize
entry.UpdateString("appName", "SchwabTrader")
entry.UpdateString("service", "OAuth2")
entry.UpdateString("username", "personal-desktop")
Dim tokens As ChilkatJsonObject
tokens.Initialize
success = vault.GetSecretJson(entry, tokens)
If success <> True Then
Log("Could not read the token record. Check the store and complete desktop authorization first.")
Return
End If
Dim accessToken As String = tokens.StringOf("access_token")
If accessToken = "" Then
Log("Run the Schwab authorization or refresh example to save an access token.")
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.AuthToken = accessToken
' Supply the calendar date you want to check, for example a US holiday.
Dim clock As ChilkatDateTime
clock.Initialize
clock.SetFromCurrentSystemTime
Dim marketDate As String = clock.GetAsIso8601("YYYY-MM-DD", False)
Dim resp As ChilkatHttpResponse
resp.Initialize
Dim query As ChilkatHttpRequest
query.Initialize
query.HttpVerb = "GET"
query.AddParam("date", marketDate)
success = http.HttpReq("https://api.schwabapi.com/marketdata/v1/markets/equity", query, resp)
If (success <> True) Or (resp.StatusCode <> 200) Then
If resp.StatusCode = 0 Then
Log("No HTTP response. Check connectivity.")
Else
Log("Schwab returned HTTP " & resp.StatusCode)
If resp.StatusCode = 401 Then
Log("Refresh the access token or repeat desktop authorization.")
End If
End If
Return
End If
Dim reply As ChilkatJsonObject
reply.Initialize
success = reply.Load(resp.BodyStr)
If success <> True Then
Log("Schwab returned unexpected JSON.")
Return
End If
Dim products As ChilkatJsonObject
products.Initialize
success = reply.ObjectOf2("equity", products)
If (success <> True) Or (products.Size = 0) Then
Log("No equity market schedule returned for " & marketDate)
Return
End If
Dim product As ChilkatJsonObject
product.Initialize
Dim i As Int = 0
Do While (i < products.Size)
products.ObjectAt2(i, product)
Log(products.NameAt(i) & " " & product.StringOf("date"))
If product.BoolOf("isOpen") = True Then
Dim j As Int = 0
Dim count As Int = product.SizeOfArray("sessionHours.regularMarket")
If count <= 0 Then
Log("Open date, but no regular-session times were supplied.")
End If
Do While (j < count)
product.J = j
Log("Regular session: " & product.StringOf("sessionHours.regularMarket[j].start") & " to " & product.StringOf("sessionHours.regularMarket[j].end"))
j = j + 1
Loop
Else
Log("Closed for this date.")
End If
i = i + 1
Loop