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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoVault
    Variant vEntry
    Handle hoEntry
    Variant vTokens
    Handle hoTokens
    String sAccessToken
    Handle hoHttp
    Handle hoClock
    String sMarketDate
    Variant vResp
    Handle hoResp
    Variant vQuery
    Handle hoQuery
    Handle hoReply
    Variant vProducts
    Handle hoProducts
    Variant vProduct
    Handle hoProduct
    Integer i
    Integer j
    Integer iCount
    String sTemp1
    String sTemp2
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Move False To iSuccess

    Get Create (RefClass(cComChilkatSecrets)) To hoVault
    If (Not(IsComObjectCreated(hoVault))) Begin
        Send CreateComObject of hoVault
    End
    Set ComLocation Of hoVault To "local_manager"
    Set ComAutoSplit Of hoVault To True
    Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
    If (Not(IsComObjectCreated(hoEntry))) Begin
        Send CreateComObject of hoEntry
    End
    Get ComUpdateString Of hoEntry "appName" "SchwabTrader" To iSuccess
    Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
    Get ComUpdateString Of hoEntry "username" "personal-desktop" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
    If (Not(IsComObjectCreated(hoTokens))) Begin
        Send CreateComObject of hoTokens
    End
    Get pvComObject of hoEntry to vEntry
    Get pvComObject of hoTokens to vTokens
    Get ComGetSecretJson Of hoVault vEntry vTokens To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read the token record. Check the store and complete desktop authorization first."
        Procedure_Return
    End

    Get ComStringOf Of hoTokens "access_token" To sAccessToken
    If (sAccessToken = "") Begin
        Showln "Run the Schwab authorization or refresh example to save an access token."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComAuthToken Of hoHttp To sAccessToken

    //  Supply the calendar date you want to check, for example a US holiday.
    Get Create (RefClass(cComCkDateTime)) To hoClock
    If (Not(IsComObjectCreated(hoClock))) Begin
        Send CreateComObject of hoClock
    End
    Get ComSetFromCurrentSystemTime Of hoClock To iSuccess
    Get ComGetAsIso8601 Of hoClock "YYYY-MM-DD" False To sMarketDate
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get Create (RefClass(cComChilkatHttpRequest)) To hoQuery
    If (Not(IsComObjectCreated(hoQuery))) Begin
        Send CreateComObject of hoQuery
    End
    Set ComHttpVerb Of hoQuery To "GET"
    Send ComAddParam To hoQuery "date" sMarketDate
    Get pvComObject of hoQuery to vQuery
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp "https://api.schwabapi.com/marketdata/v1/markets/equity" vQuery vResp To iSuccess
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 0) Begin
            Showln "No HTTP response. Check connectivity."
        End
        Else Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "Schwab returned HTTP " iTemp1
            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 401) Begin
                Showln "Refresh the access token or repeat desktop authorization."
            End

        End

        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoReply sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Schwab returned unexpected JSON."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoProducts
    If (Not(IsComObjectCreated(hoProducts))) Begin
        Send CreateComObject of hoProducts
    End
    Get pvComObject of hoProducts to vProducts
    Get ComObjectOf2 Of hoReply "equity" vProducts To iSuccess
    Get ComSize Of hoProducts To iTemp1
    If ((iSuccess <> True) Or (iTemp1 = 0)) Begin
        Showln "No equity market schedule returned for " sMarketDate
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoProduct
    If (Not(IsComObjectCreated(hoProduct))) Begin
        Send CreateComObject of hoProduct
    End
    Move 0 To i
    While (i < (ComSize(hoProducts)))
        Get pvComObject of hoProduct to vProduct
        Get ComObjectAt2 Of hoProducts i vProduct To iSuccess
        Get ComNameAt Of hoProducts i To sTemp1
        Get ComStringOf Of hoProduct "date" To sTemp2
        Showln sTemp1 "  " sTemp2
        Get ComBoolOf Of hoProduct "isOpen" To bTemp1
        If (bTemp1 = True) Begin
            Move 0 To j
            Get ComSizeOfArray Of hoProduct "sessionHours.regularMarket" To iCount
            If (iCount <= 0) Begin
                Showln "Open date, but no regular-session times were supplied."
            End

            While (j < iCount)
                Set ComJ Of hoProduct To j
                Get ComStringOf Of hoProduct "sessionHours.regularMarket[j].start" To sTemp1
                Get ComStringOf Of hoProduct "sessionHours.regularMarket[j].end" To sTemp2
                Showln "Regular session: " sTemp1 " to " sTemp2
                Move (j + 1) To j
            Loop

        End
        Else Begin
            Showln "Closed for this date."
        End

        Move (i + 1) To i
    Loop



End_Procedure