Xbase++ Requires Chilkat v11.0.0+
Xbase++
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.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL cAccessToken
LOCAL oHttp
LOCAL cUnderlying
LOCAL cExpiration
LOCAL cStrike
LOCAL cSide
LOCAL oResp
LOCAL oQuery
LOCAL oReply
LOCAL cMapName
LOCAL oExpirations
LOCAL oStrikes
LOCAL oContracts
LOCAL oContract
LOCAL nFound
LOCAL i
LOCAL j
LOCAL k
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "SchwabTrader")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "personal-desktop")
oTokens := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oTokens)
IF (nSuccess != 1)
? "Could not read the token record. Check the store and complete desktop authorization first."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
RETURN
ENDIF
cAccessToken := oTokens:StringOf("access_token")
IF (cAccessToken == "")
? "Run the Schwab authorization or refresh example to save an access token."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
RETURN
ENDIF
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := cAccessToken
cUnderlying := "AAPL"
cExpiration := "2027-01-15"
cStrike := "250"
cSide := "CALL"
oResp := CreateObject("Chilkat.HttpResponse")
oQuery := CreateObject("Chilkat.HttpRequest")
oQuery:HttpVerb := "GET"
oQuery:AddParam("symbol", cUnderlying)
oQuery:AddParam("contractType", cSide)
oQuery:AddParam("strike", cStrike)
oQuery:AddParam("fromDate", cExpiration)
oQuery:AddParam("toDate", cExpiration)
nSuccess := oHttp:HttpReq("https://api.schwabapi.com/marketdata/v1/chains", oQuery, oResp)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
IF (oResp:StatusCode == 0)
? "No HTTP response. Check connectivity."
ELSE
? "Schwab returned HTTP " + Str(oResp:StatusCode)
IF (oResp:StatusCode == 401)
? "Refresh the access token or repeat desktop authorization."
ENDIF
ENDIF
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oQuery:destroy()
RETURN
ENDIF
oReply := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:Load(oResp:BodyStr)
IF (nSuccess != 1)
? "Schwab returned unexpected JSON."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oQuery:destroy()
oReply:destroy()
RETURN
ENDIF
cMapName := "callExpDateMap"
IF (cSide == "PUT")
cMapName := "putExpDateMap"
ENDIF
oExpirations := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:ObjectOf2(cMapName, oExpirations)
IF (nSuccess != 1)
? "No option map returned. Chain status: " + oReply:StringOf("status")
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oQuery:destroy()
oReply:destroy()
oExpirations:destroy()
RETURN
ENDIF
// Expiration keys contain a day count. Strike keys may contain a decimal point.
oStrikes := CreateObject("Chilkat.JsonObject")
oContracts := CreateObject("Chilkat.JsonArray")
oContract := CreateObject("Chilkat.JsonObject")
nFound := 0
i := 0
DO WHILE (i < oExpirations:Size)
oExpirations:ObjectAt2(i, oStrikes)
j := 0
DO WHILE (j < oStrikes:Size)
oStrikes:ArrayAt2(j, oContracts)
k := 0
DO WHILE (k < oContracts:Size)
oContracts:ObjectAt2(k, oContract)
? oContract:StringOf("description") + " strike " + oContract:StringOf("strikePrice")
? "Contract symbol: [" + oContract:StringOf("symbol") + "]"
nFound := nFound + 1
k := k + 1
ENDDO
j := j + 1
ENDDO
i := i + 1
ENDDO
IF (nFound == 0)
? "No contracts matched the requested expiration, side and strike."
ENDIF
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oQuery:destroy()
oReply:destroy()
oExpirations:destroy()
oStrikes:destroy()
oContracts:destroy()
oContract:destroy()