AutoIt Requires Chilkat v11.0.0+
AutoIt
Schwab: Get quotes for a watchlist
See more Schwab Examples
Requests several symbols in one call and prints the bid, ask, last price, quote timestamp in Unix milliseconds, and the real-time indicator when available. Missing quote fields can occur for individual instruments. The response uses symbol names as JSON keys and can report invalid symbols alongside valid quotes.
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
$oResp = ObjCreate("Chilkat.HttpResponse")
$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "symbols","AAPL,MSFT,BRK/B"
$oQuery.AddParam "fields","quote"
$bSuccess = $oHttp.HttpReq("https://api.schwabapi.com/marketdata/v1/quotes",$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
; Read members by index. A symbol may contain punctuation meaningful in a JSON path.
$oInstrument = ObjCreate("Chilkat.JsonObject")
Local $i = 0
While ($i < $oReply.Size)
Local $sKey = $oReply.NameAt($i)
If ($sKey <> "errors") Then
$bSuccess = $oReply.ObjectAt2($i,$oInstrument)
If ($bSuccess = True) Then
ConsoleWrite($sKey & " bid " & $oInstrument.StringOf("quote.bidPrice") & " ask " & $oInstrument.StringOf("quote.askPrice") & " last " & $oInstrument.StringOf("quote.lastPrice") & @CRLF)
ConsoleWrite(" quote time (Unix ms): " & $oInstrument.StringOf("quote.quoteTime") & " real-time: " & $oInstrument.StringOf("realtime") & @CRLF)
EndIf
EndIf
$i = $i + 1
Wend
Local $iJ = 0
While ($iJ < $oReply.SizeOfArray("errors.invalidSymbols"))
$oReply.J = $iJ
ConsoleWrite("Invalid symbol: " & $oReply.StringOf("errors.invalidSymbols[j]") & @CRLF)
$iJ = $iJ + 1
Wend