Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

Schwab: Find an option contract by expiration and strike

See more Schwab Examples

Filters a chain by underlying, call or put, exact expiration, and strike. Choose a currently listed expiration and strike. Schwab groups contracts by expiration and strike and may return multiple deliverables. Compare the descriptions and use the exact returned contract symbol, including its spaces, for a later quote request.

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
    String sUnderlying
    String sExpiration
    String sStrike
    String sSide
    Variant vResp
    Handle hoResp
    Variant vQuery
    Handle hoQuery
    Handle hoReply
    String sMapName
    Variant vExpirations
    Handle hoExpirations
    Variant vStrikes
    Handle hoStrikes
    Variant vContracts
    Handle hoContracts
    Variant vContract
    Handle hoContract
    Integer iFound
    Integer i
    Integer j
    Integer k
    String sTemp1
    String sTemp2
    Integer iTemp1

    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

    Move "AAPL" To sUnderlying
    Move "2027-01-15" To sExpiration
    Move "250" To sStrike
    Move "CALL" To sSide
    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 "symbol" sUnderlying
    Send ComAddParam To hoQuery "contractType" sSide
    Send ComAddParam To hoQuery "strike" sStrike
    Send ComAddParam To hoQuery "fromDate" sExpiration
    Send ComAddParam To hoQuery "toDate" sExpiration
    Get pvComObject of hoQuery to vQuery
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp "https://api.schwabapi.com/marketdata/v1/chains" 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

    Move "callExpDateMap" To sMapName
    If (sSide = "PUT") Begin
        Move "putExpDateMap" To sMapName
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoExpirations
    If (Not(IsComObjectCreated(hoExpirations))) Begin
        Send CreateComObject of hoExpirations
    End
    Get pvComObject of hoExpirations to vExpirations
    Get ComObjectOf2 Of hoReply sMapName vExpirations To iSuccess
    If (iSuccess <> True) Begin
        Get ComStringOf Of hoReply "status" To sTemp1
        Showln "No option map returned. Chain status: " sTemp1
        Procedure_Return
    End

    //  Expiration keys contain a day count. Strike keys may contain a decimal point.
    Get Create (RefClass(cComChilkatJsonObject)) To hoStrikes
    If (Not(IsComObjectCreated(hoStrikes))) Begin
        Send CreateComObject of hoStrikes
    End
    Get Create (RefClass(cComChilkatJsonArray)) To hoContracts
    If (Not(IsComObjectCreated(hoContracts))) Begin
        Send CreateComObject of hoContracts
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoContract
    If (Not(IsComObjectCreated(hoContract))) Begin
        Send CreateComObject of hoContract
    End
    Move 0 To iFound
    Move 0 To i
    While (i < (ComSize(hoExpirations)))
        Get pvComObject of hoStrikes to vStrikes
        Get ComObjectAt2 Of hoExpirations i vStrikes To iSuccess
        Move 0 To j
        While (j < (ComSize(hoStrikes)))
            Get pvComObject of hoContracts to vContracts
            Get ComArrayAt2 Of hoStrikes j vContracts To iSuccess
            Move 0 To k
            While (k < (ComSize(hoContracts)))
                Get pvComObject of hoContract to vContract
                Get ComObjectAt2 Of hoContracts k vContract To iSuccess
                Get ComStringOf Of hoContract "description" To sTemp1
                Get ComStringOf Of hoContract "strikePrice" To sTemp2
                Showln sTemp1 "  strike " sTemp2
                Get ComStringOf Of hoContract "symbol" To sTemp1
                Showln "Contract symbol: [" sTemp1 "]"
                Move (iFound + 1) To iFound
                Move (k + 1) To k
            Loop

            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop

    If (iFound = 0) Begin
        Showln "No contracts matched the requested expiration, side and strike."
    End



End_Procedure