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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False

$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","SchwabTrader")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","personal-desktop")
$oTokens = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read the token record. Check the store and complete desktop authorization first." & @CRLF)
    Exit
EndIf

Local $sAccessToken = $oTokens.StringOf("access_token")
If ($sAccessToken = "") Then
    ConsoleWrite("Run the Schwab authorization or refresh example to save an access token." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $sAccessToken

;  Supply the calendar date you want to check, for example a US holiday.
$oClock = ObjCreate("Chilkat.CkDateTime")
$oClock.SetFromCurrentSystemTime()
Local $sMarketDate = $oClock.GetAsIso8601("YYYY-MM-DD",False)
$oResp = ObjCreate("Chilkat.HttpResponse")
$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "date",$sMarketDate
$bSuccess = $oHttp.HttpReq("https://api.schwabapi.com/marketdata/v1/markets/equity",$oQuery,$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    If ($oResp.StatusCode = 0) Then
        ConsoleWrite("No HTTP response. Check connectivity." & @CRLF)
    Else
        ConsoleWrite("Schwab returned HTTP " & $oResp.StatusCode & @CRLF)
        If ($oResp.StatusCode = 401) Then
            ConsoleWrite("Refresh the access token or repeat desktop authorization." & @CRLF)
        EndIf

    EndIf

    Exit
EndIf

$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Schwab returned unexpected JSON." & @CRLF)
    Exit
EndIf

$oProducts = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.ObjectOf2("equity",$oProducts)
If (($bSuccess <> True) Or ($oProducts.Size = 0)) Then
    ConsoleWrite("No equity market schedule returned for " & $sMarketDate & @CRLF)
    Exit
EndIf

$oProduct = ObjCreate("Chilkat.JsonObject")
Local $i = 0
While ($i < $oProducts.Size)
    $oProducts.ObjectAt2($i,$oProduct)
    ConsoleWrite($oProducts.NameAt($i) & "  " & $oProduct.StringOf("date") & @CRLF)
    If ($oProduct.BoolOf("isOpen") = True) Then
Local $iJ = 0
Local $iCount = $oProduct.SizeOfArray("sessionHours.regularMarket")
        If ($iCount <= 0) Then
            ConsoleWrite("Open date, but no regular-session times were supplied." & @CRLF)
        EndIf

        While ($iJ < $iCount)
            $oProduct.J = $iJ
            ConsoleWrite("Regular session: " & $oProduct.StringOf("sessionHours.regularMarket[j].start") & " to " & $oProduct.StringOf("sessionHours.regularMarket[j].end") & @CRLF)
            $iJ = $iJ + 1
        Wend
    Else
        ConsoleWrite("Closed for this date." & @CRLF)
    EndIf

    $i = $i + 1
Wend