AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","SchwabTrader")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","personal-desktop")
$oTokens = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bSuccess <> True) Then
ConsoleWrite("Could not read the token record. Check the store and complete desktop authorization first." & @CRLF)
Exit
EndIf
Local $sAccessToken = $oTokens.StringOf("access_token")
If ($sAccessToken = "") Then
ConsoleWrite("Run the Schwab authorization or refresh example to save an access token." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $sAccessToken
Local $sUnderlying = "AAPL"
Local $sExpiration = "2027-01-15"
Local $strike = "250"
Local $side = "CALL"
$oResp = ObjCreate("Chilkat.HttpResponse")
$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "symbol",$sUnderlying
$oQuery.AddParam "contractType",$side
$oQuery.AddParam "strike",$strike
$oQuery.AddParam "fromDate",$sExpiration
$oQuery.AddParam "toDate",$sExpiration
$bSuccess = $oHttp.HttpReq("https://api.schwabapi.com/marketdata/v1/chains",$oQuery,$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite("No HTTP response. Check connectivity." & @CRLF)
Else
ConsoleWrite("Schwab returned HTTP " & $oResp.StatusCode & @CRLF)
If ($oResp.StatusCode = 401) Then
ConsoleWrite("Refresh the access token or repeat desktop authorization." & @CRLF)
EndIf
EndIf
Exit
EndIf
$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Schwab returned unexpected JSON." & @CRLF)
Exit
EndIf
Local $sMapName = "callExpDateMap"
If ($side = "PUT") Then
$sMapName = "putExpDateMap"
EndIf
$oExpirations = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.ObjectOf2($sMapName,$oExpirations)
If ($bSuccess <> True) Then
ConsoleWrite("No option map returned. Chain status: " & $oReply.StringOf("status") & @CRLF)
Exit
EndIf
; Expiration keys contain a day count. Strike keys may contain a decimal point.
$oStrikes = ObjCreate("Chilkat.JsonObject")
$oContracts = ObjCreate("Chilkat.JsonArray")
$oContract = ObjCreate("Chilkat.JsonObject")
Local $iFound = 0
Local $i = 0
While ($i < $oExpirations.Size)
$oExpirations.ObjectAt2($i,$oStrikes)
Local $iJ = 0
While ($iJ < $oStrikes.Size)
$oStrikes.ArrayAt2($iJ,$oContracts)
Local $iK = 0
While ($iK < $oContracts.Size)
$oContracts.ObjectAt2($iK,$oContract)
ConsoleWrite($oContract.StringOf("description") & " strike " & $oContract.StringOf("strikePrice") & @CRLF)
ConsoleWrite("Contract symbol: [" & $oContract.StringOf("symbol") & "]" & @CRLF)
$iFound = $iFound + 1
$iK = $iK + 1
Wend
$iJ = $iJ + 1
Wend
$i = $i + 1
Wend
If ($iFound = 0) Then
ConsoleWrite("No contracts matched the requested expiration, side and strike." & @CRLF)
EndIf