DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoVault
Variant vEntry
Handle hoEntry
Variant vTokens
Handle hoTokens
String sAccessToken
Handle hoHttp
Variant vResp
Handle hoResp
Variant vQuery
Handle hoQuery
Handle hoReply
Integer iCount
Handle hoCsv
Handle hoMillis
Handle hoUnixTime
Variant vCandleTime
Handle hoCandleTime
Integer i
String sTimestamp
Integer iDigits
String sTemp1
Integer iTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
Get Create (RefClass(cComChilkatSecrets)) To hoVault
If (Not(IsComObjectCreated(hoVault))) Begin
Send CreateComObject of hoVault
End
Set ComLocation Of hoVault To "local_manager"
Set ComAutoSplit Of hoVault To True
Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
If (Not(IsComObjectCreated(hoEntry))) Begin
Send CreateComObject of hoEntry
End
Get ComUpdateString Of hoEntry "appName" "SchwabTrader" To iSuccess
Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
Get ComUpdateString Of hoEntry "username" "personal-desktop" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
If (Not(IsComObjectCreated(hoTokens))) Begin
Send CreateComObject of hoTokens
End
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoTokens to vTokens
Get ComGetSecretJson Of hoVault vEntry vTokens To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read the token record. Check the store and complete desktop authorization first."
Procedure_Return
End
Get ComStringOf Of hoTokens "access_token" To sAccessToken
If (sAccessToken = "") Begin
Showln "Run the Schwab authorization or refresh example to save an access token."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComAuthToken Of hoHttp To sAccessToken
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatHttpRequest)) To hoQuery
If (Not(IsComObjectCreated(hoQuery))) Begin
Send CreateComObject of hoQuery
End
Set ComHttpVerb Of hoQuery To "GET"
Send ComAddParam To hoQuery "symbol" "AAPL"
Send ComAddParam To hoQuery "periodType" "year"
Send ComAddParam To hoQuery "period" "1"
Send ComAddParam To hoQuery "frequencyType" "daily"
Send ComAddParam To hoQuery "frequency" "1"
Send ComAddParam To hoQuery "needExtendedHoursData" "false"
Get pvComObject of hoQuery to vQuery
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp "https://api.schwabapi.com/marketdata/v1/pricehistory" vQuery vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response. Check connectivity."
End
Else Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Schwab returned HTTP " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 401) Begin
Showln "Refresh the access token or repeat desktop authorization."
End
End
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Schwab returned unexpected JSON."
Procedure_Return
End
Get ComSizeOfArray Of hoReply "candles" To iCount
If (iCount <= 0) Begin
Showln "No candles for this symbol and period."
Procedure_Return
End
Get Create (RefClass(cComChilkatCsv)) To hoCsv
If (Not(IsComObjectCreated(hoCsv))) Begin
Send CreateComObject of hoCsv
End
Get ComSetColumnName Of hoCsv 0 "utcTime" To iSuccess
Get ComSetColumnName Of hoCsv 1 "unixMilliseconds" To iSuccess
Get ComSetColumnName Of hoCsv 2 "open" To iSuccess
Get ComSetColumnName Of hoCsv 3 "high" To iSuccess
Get ComSetColumnName Of hoCsv 4 "low" To iSuccess
Get ComSetColumnName Of hoCsv 5 "close" To iSuccess
Get ComSetColumnName Of hoCsv 6 "volume" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoMillis
If (Not(IsComObjectCreated(hoMillis))) Begin
Send CreateComObject of hoMillis
End
Get Create (RefClass(cComChilkatJsonObject)) To hoUnixTime
If (Not(IsComObjectCreated(hoUnixTime))) Begin
Send CreateComObject of hoUnixTime
End
Get Create (RefClass(cComCkDateTime)) To hoCandleTime
If (Not(IsComObjectCreated(hoCandleTime))) Begin
Send CreateComObject of hoCandleTime
End
Move 0 To i
While (i < iCount)
Set ComI Of hoReply To i
Get ComStringOf Of hoReply "candles[i].datetime" To sTimestamp
// DateOf reads Unix seconds. Drop the three millisecond digits as text.
Get ComSetString Of hoMillis sTimestamp To iSuccess
Get ComLength Of hoMillis To iTemp1
Move (iTemp1 - 3) To iDigits
If (iDigits <= 0) Begin
Showln "Missing candle timestamp."
Procedure_Return
End
Get ComGetRange Of hoMillis 0 iDigits False To sTemp1
Get ComUpdateNumber Of hoUnixTime "seconds" sTemp1 To iSuccess
Get pvComObject of hoCandleTime to vCandleTime
Get ComDateOf Of hoUnixTime "seconds" vCandleTime To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid candle timestamp."
Procedure_Return
End
Get ComGetAsTimestamp Of hoCandleTime False To sTemp1
Get ComSetCell Of hoCsv i 0 sTemp1 To iSuccess
Get ComSetCell Of hoCsv i 1 sTimestamp To iSuccess
Get ComStringOf Of hoReply "candles[i].open" To sTemp1
Get ComSetCell Of hoCsv i 2 sTemp1 To iSuccess
Get ComStringOf Of hoReply "candles[i].high" To sTemp1
Get ComSetCell Of hoCsv i 3 sTemp1 To iSuccess
Get ComStringOf Of hoReply "candles[i].low" To sTemp1
Get ComSetCell Of hoCsv i 4 sTemp1 To iSuccess
Get ComStringOf Of hoReply "candles[i].close" To sTemp1
Get ComSetCell Of hoCsv i 5 sTemp1 To iSuccess
Get ComStringOf Of hoReply "candles[i].volume" To sTemp1
Get ComSetCell Of hoCsv i 6 sTemp1 To iSuccess
Move (i + 1) To i
Loop
Get ComSaveFile2 Of hoCsv "schwab-daily-prices.csv" "utf-8" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not write schwab-daily-prices.csv."
Procedure_Return
End
Get ComNumRows Of hoCsv To iTemp1
Showln "Wrote " iTemp1 " rows to schwab-daily-prices.csv."
End_Procedure