PureBasic Requires Chilkat v11.0.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonArray.pb"
IncludeFile "CkSecrets.pb"
IncludeFile "CkHttpRequest.pb"
IncludeFile "CkHttpResponse.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
vault.i = CkSecrets::ckCreate()
If vault.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSecrets::setCkLocation(vault, "local_manager")
CkSecrets::setCkAutoSplit(vault, 1)
entry.i = CkJsonObject::ckCreate()
If entry.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(entry,"appName","SchwabTrader")
CkJsonObject::ckUpdateString(entry,"service","OAuth2")
CkJsonObject::ckUpdateString(entry,"username","personal-desktop")
tokens.i = CkJsonObject::ckCreate()
If tokens.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkSecrets::ckGetSecretJson(vault,entry,tokens)
If success <> 1
Debug "Could not read the token record. Check the store and complete desktop authorization first."
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
ProcedureReturn
EndIf
accessToken.s = CkJsonObject::ckStringOf(tokens,"access_token")
If accessToken = ""
Debug "Run the Schwab authorization or refresh example to save an access token."
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
ProcedureReturn
EndIf
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkRequireSslCertVerify(http, 1)
CkHttp::setCkAuthToken(http, accessToken)
underlying.s = "AAPL"
expiration.s = "2027-01-15"
strike.s = "250"
side.s = "CALL"
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
query.i = CkHttpRequest::ckCreate()
If query.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpRequest::setCkHttpVerb(query, "GET")
CkHttpRequest::ckAddParam(query,"symbol",underlying)
CkHttpRequest::ckAddParam(query,"contractType",side)
CkHttpRequest::ckAddParam(query,"strike",strike)
CkHttpRequest::ckAddParam(query,"fromDate",expiration)
CkHttpRequest::ckAddParam(query,"toDate",expiration)
success = CkHttp::ckHttpReq(http,"https://api.schwabapi.com/marketdata/v1/chains",query,resp)
If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) <> 200)
If CkHttpResponse::ckStatusCode(resp) = 0
Debug "No HTTP response. Check connectivity."
Else
Debug "Schwab returned HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
If CkHttpResponse::ckStatusCode(resp) = 401
Debug "Refresh the access token or repeat desktop authorization."
EndIf
EndIf
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkHttpRequest::ckDispose(query)
ProcedureReturn
EndIf
reply.i = CkJsonObject::ckCreate()
If reply.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(reply,CkHttpResponse::ckBodyStr(resp))
If success <> 1
Debug "Schwab returned unexpected JSON."
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkHttpRequest::ckDispose(query)
CkJsonObject::ckDispose(reply)
ProcedureReturn
EndIf
mapName.s = "callExpDateMap"
If side = "PUT"
mapName = "putExpDateMap"
EndIf
expirations.i = CkJsonObject::ckCreate()
If expirations.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckObjectOf2(reply,mapName,expirations)
If success <> 1
Debug "No option map returned. Chain status: " + CkJsonObject::ckStringOf(reply,"status")
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkHttpRequest::ckDispose(query)
CkJsonObject::ckDispose(reply)
CkJsonObject::ckDispose(expirations)
ProcedureReturn
EndIf
; Expiration keys contain a day count. Strike keys may contain a decimal point.
strikes.i = CkJsonObject::ckCreate()
If strikes.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
contracts.i = CkJsonArray::ckCreate()
If contracts.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
contract.i = CkJsonObject::ckCreate()
If contract.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
found.i = 0
i.i = 0
While (i < CkJsonObject::ckSize(expirations))
CkJsonObject::ckObjectAt2(expirations,i,strikes)
j.i = 0
While (j < CkJsonObject::ckSize(strikes))
CkJsonObject::ckArrayAt2(strikes,j,contracts)
k.i = 0
While (k < CkJsonArray::ckSize(contracts))
CkJsonArray::ckObjectAt2(contracts,k,contract)
Debug CkJsonObject::ckStringOf(contract,"description") + " strike " + CkJsonObject::ckStringOf(contract,"strikePrice")
Debug "Contract symbol: [" + CkJsonObject::ckStringOf(contract,"symbol") + "]"
found = found + 1
k = k + 1
Wend
j = j + 1
Wend
i = i + 1
Wend
If found = 0
Debug "No contracts matched the requested expiration, side and strike."
EndIf
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkHttpRequest::ckDispose(query)
CkJsonObject::ckDispose(reply)
CkJsonObject::ckDispose(expirations)
CkJsonObject::ckDispose(strikes)
CkJsonArray::ckDispose(contracts)
CkJsonObject::ckDispose(contract)
ProcedureReturn
EndProcedure