AutoIt Requires Chilkat v11.0.0+
AutoIt
Schwab: Export daily OHLCV price history
See more Schwab Examples
Downloads one year of daily open, high, low, close, and volume for one equity. The CSV records each candle’s UTC timestamp and original Unix milliseconds. Volume and prices retain their JSON number text. An empty series leaves any previous export in place.
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 "symbol","AAPL"
$oQuery.AddParam "periodType","year"
$oQuery.AddParam "period","1"
$oQuery.AddParam "frequencyType","daily"
$oQuery.AddParam "frequency","1"
$oQuery.AddParam "needExtendedHoursData","false"
$bSuccess = $oHttp.HttpReq("https://api.schwabapi.com/marketdata/v1/pricehistory",$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 $iCount = $oReply.SizeOfArray("candles")
If ($iCount <= 0) Then
ConsoleWrite("No candles for this symbol and period." & @CRLF)
Exit
EndIf
$oCsv = ObjCreate("Chilkat.Csv")
$oCsv.SetColumnName(0,"utcTime")
$oCsv.SetColumnName(1,"unixMilliseconds")
$oCsv.SetColumnName(2,"open")
$oCsv.SetColumnName(3,"high")
$oCsv.SetColumnName(4,"low")
$oCsv.SetColumnName(5,"close")
$oCsv.SetColumnName(6,"volume")
$oMillis = ObjCreate("Chilkat.StringBuilder")
$oUnixTime = ObjCreate("Chilkat.JsonObject")
$oCandleTime = ObjCreate("Chilkat.CkDateTime")
Local $i = 0
While ($i < $iCount)
$oReply.I = $i
Local $sTimestamp = $oReply.StringOf("candles[i].datetime")
; DateOf reads Unix seconds. Drop the three millisecond digits as text.
$oMillis.SetString($sTimestamp)
Local $iDigits = $oMillis.Length - 3
If ($iDigits <= 0) Then
ConsoleWrite("Missing candle timestamp." & @CRLF)
Exit
EndIf
$oUnixTime.UpdateNumber("seconds",$oMillis.GetRange(0,$iDigits,False))
$bSuccess = $oUnixTime.DateOf("seconds",$oCandleTime)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid candle timestamp." & @CRLF)
Exit
EndIf
$oCsv.SetCell($i,0,$oCandleTime.GetAsTimestamp(False))
$oCsv.SetCell($i,1,$sTimestamp)
$oCsv.SetCell($i,2,$oReply.StringOf("candles[i].open"))
$oCsv.SetCell($i,3,$oReply.StringOf("candles[i].high"))
$oCsv.SetCell($i,4,$oReply.StringOf("candles[i].low"))
$oCsv.SetCell($i,5,$oReply.StringOf("candles[i].close"))
$oCsv.SetCell($i,6,$oReply.StringOf("candles[i].volume"))
$i = $i + 1
Wend
$bSuccess = $oCsv.SaveFile2("schwab-daily-prices.csv","utf-8")
If ($bSuccess <> True) Then
ConsoleWrite("Could not write schwab-daily-prices.csv." & @CRLF)
Exit
EndIf
ConsoleWrite("Wrote " & $oCsv.NumRows & " rows to schwab-daily-prices.csv." & @CRLF)