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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
string ls_AccessToken
oleobject loo_Http
string ls_Underlying
string ls_Expiration
string ls_Strike
string ls_Side
oleobject loo_Resp
oleobject loo_Query
oleobject loo_Reply
string ls_MapName
oleobject loo_Expirations
oleobject loo_Strikes
oleobject loo_Contracts
oleobject loo_Contract
long li_Found
long i
long j
long k

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0

loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
    destroy loo_Vault
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")

loo_Entry.UpdateString("appName","SchwabTrader")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","personal-desktop")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Success <> 1 then
    Write-Debug "Could not read the token record. Check the store and complete desktop authorization first."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

ls_AccessToken = loo_Tokens.StringOf("access_token")
if ls_AccessToken = "" then
    Write-Debug "Run the Schwab authorization or refresh example to save an access token."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = ls_AccessToken

ls_Underlying = "AAPL"
ls_Expiration = "2027-01-15"
ls_Strike = "250"
ls_Side = "CALL"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Query = create oleobject
li_rc = loo_Query.ConnectToNewObject("Chilkat.HttpRequest")

loo_Query.HttpVerb = "GET"
loo_Query.AddParam("symbol",ls_Underlying)
loo_Query.AddParam("contractType",ls_Side)
loo_Query.AddParam("strike",ls_Strike)
loo_Query.AddParam("fromDate",ls_Expiration)
loo_Query.AddParam("toDate",ls_Expiration)
li_Success = loo_Http.HttpReq("https://api.schwabapi.com/marketdata/v1/chains",loo_Query,loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
    if loo_Resp.StatusCode = 0 then
        Write-Debug "No HTTP response. Check connectivity."
    else
        Write-Debug "Schwab returned HTTP " + string(loo_Resp.StatusCode)
        if loo_Resp.StatusCode = 401 then
            Write-Debug "Refresh the access token or repeat desktop authorization."
        end if

    end if

    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    return
end if

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Schwab returned unexpected JSON."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Reply
    return
end if

ls_MapName = "callExpDateMap"
if ls_Side = "PUT" then
    ls_MapName = "putExpDateMap"
end if

loo_Expirations = create oleobject
li_rc = loo_Expirations.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reply.ObjectOf2(ls_MapName,loo_Expirations)
if li_Success <> 1 then
    Write-Debug "No option map returned. Chain status: " + loo_Reply.StringOf("status")
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Reply
    destroy loo_Expirations
    return
end if

//  Expiration keys contain a day count. Strike keys may contain a decimal point.
loo_Strikes = create oleobject
li_rc = loo_Strikes.ConnectToNewObject("Chilkat.JsonObject")

loo_Contracts = create oleobject
li_rc = loo_Contracts.ConnectToNewObject("Chilkat.JsonArray")

loo_Contract = create oleobject
li_rc = loo_Contract.ConnectToNewObject("Chilkat.JsonObject")

li_Found = 0
i = 0
do while (i < loo_Expirations.Size)
    loo_Expirations.ObjectAt2(i,loo_Strikes)
    j = 0
    do while (j < loo_Strikes.Size)
        loo_Strikes.ArrayAt2(j,loo_Contracts)
        k = 0
        do while (k < loo_Contracts.Size)
            loo_Contracts.ObjectAt2(k,loo_Contract)
            Write-Debug loo_Contract.StringOf("description") + "  strike " + loo_Contract.StringOf("strikePrice")
            Write-Debug "Contract symbol: [" + loo_Contract.StringOf("symbol") + "]"
            li_Found = li_Found + 1
            k = k + 1
        loop
        j = j + 1
    loop
    i = i + 1
loop
if li_Found = 0 then
    Write-Debug "No contracts matched the requested expiration, side and strike."
end if



destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Query
destroy loo_Reply
destroy loo_Expirations
destroy loo_Strikes
destroy loo_Contracts
destroy loo_Contract