Sample code for 30+ languages & platforms
Classic ASP 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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

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

set vault = Server.CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = Server.CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
set tokens = Server.CreateObject("Chilkat.JsonObject")
success = vault.GetSecretJson(entry,tokens)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read the token record. Check the store and complete desktop authorization first.") & "</pre>"
    Response.End
End If

accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
    Response.Write "<pre>" & Server.HTMLEncode( "Run the Schwab authorization or refresh example to save an access token.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = accessToken

'  Supply the calendar date you want to check, for example a US holiday.
set clock = Server.CreateObject("Chilkat.CkDateTime")
success = clock.SetFromCurrentSystemTime()
marketDate = clock.GetAsIso8601("YYYY-MM-DD",0)
set resp = Server.CreateObject("Chilkat.HttpResponse")
set query = Server.CreateObject("Chilkat.HttpRequest")
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
        Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response. Check connectivity.") & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned HTTP " & resp.StatusCode) & "</pre>"
        If (resp.StatusCode = 401) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Refresh the access token or repeat desktop authorization.") & "</pre>"
        End If

    End If

    Response.End
End If

set reply = Server.CreateObject("Chilkat.JsonObject")
success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned unexpected JSON.") & "</pre>"
    Response.End
End If

set products = Server.CreateObject("Chilkat.JsonObject")
success = reply.ObjectOf2("equity",products)
If ((success <> 1) Or (products.Size = 0)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No equity market schedule returned for " & marketDate) & "</pre>"
    Response.End
End If

set product = Server.CreateObject("Chilkat.JsonObject")
i = 0
Do While (i < products.Size)
    success = products.ObjectAt2(i,product)
    Response.Write "<pre>" & Server.HTMLEncode( products.NameAt(i) & "  " & product.StringOf("date")) & "</pre>"
    If (product.BoolOf("isOpen") = 1) Then
        j = 0
        count = product.SizeOfArray("sessionHours.regularMarket")
        If (count <= 0) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Open date, but no regular-session times were supplied.") & "</pre>"
        End If

        Do While (j < count)
            product.J = j
            Response.Write "<pre>" & Server.HTMLEncode( "Regular session: " & product.StringOf("sessionHours.regularMarket[j].start") & " to " & product.StringOf("sessionHours.regularMarket[j].end")) & "</pre>"
            j = j + 1
        Loop
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "Closed for this date.") & "</pre>"
    End If

    i = i + 1
Loop

%>
</body>
</html>