AutoIt Requires Chilkat v11.0.0+
AutoIt
Schwab: Export account transactions to CSV
See more Schwab Examples
Exports the last seven days of trades, dividends, and interest for one account. Each UTF-8 CSV row represents an activity, including its signed net amount. The file is replaced on each run. For incremental imports, key records by account and activityId so overlapping date windows do not duplicate entries. If a window returns the 3,000-transaction maximum, split it before treating it as complete.
Use the access token saved by desktop authorization. First select the authorized account whose data you want to use. 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
; Read the hash saved by the authorized-account selection example.
$oEntry.UpdateString("service","SelectedAccount")
$oSelection = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oSelection)
Local $sAccountHash = $oSelection.StringOf("hashValue")
If (($bSuccess <> True) Or ($sAccountHash = "")) Then
ConsoleWrite("Select an authorized account first." & @CRLF)
Exit
EndIf
$oHttp.SetUrlVar("account",$sAccountHash)
; Use UTC endpoints for a rolling seven-day window.
$oClock = ObjCreate("Chilkat.CkDateTime")
$oClock.SetFromCurrentSystemTime()
$oEndTime = ObjCreate("Chilkat.StringBuilder")
$oEndTime.Append($oClock.GetAsTimestamp(False))
Local $iReplacements = $oEndTime.Replace("Z",".000Z")
$oClock.AddDays(-7)
$oStartTime = ObjCreate("Chilkat.StringBuilder")
$oStartTime.Append($oClock.GetAsTimestamp(False))
$iReplacements = $oStartTime.Replace("Z",".000Z")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "startDate",$oStartTime.GetAsString()
$oQuery.AddParam "endDate",$oEndTime.GetAsString()
$oQuery.AddParam "types","TRADE,DIVIDEND_OR_INTEREST"
$bSuccess = $oHttp.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}/transactions",$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
$oActivity = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oActivity.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Schwab returned unexpected JSON." & @CRLF)
Exit
EndIf
If ($oActivity.Size >= 3000) Then
ConsoleWrite("The response reached the transaction limit. Export smaller time windows." & @CRLF)
Exit
EndIf
$oCsv = ObjCreate("Chilkat.Csv")
$oCsv.SetColumnName(0,"activityId")
$oCsv.SetColumnName(1,"time")
$oCsv.SetColumnName(2,"type")
$oCsv.SetColumnName(3,"tradeDate")
$oCsv.SetColumnName(4,"settlementDate")
$oCsv.SetColumnName(5,"netAmount")
$oRow = ObjCreate("Chilkat.JsonObject")
Local $i = 0
While ($i < $oActivity.Size)
$oActivity.ObjectAt2($i,$oRow)
; Keep identifiers and decimal amounts as returned by Schwab.
$oCsv.SetCell($i,0,$oRow.StringOf("activityId"))
$oCsv.SetCell($i,1,$oRow.StringOf("time"))
$oCsv.SetCell($i,2,$oRow.StringOf("type"))
$oCsv.SetCell($i,3,$oRow.StringOf("tradeDate"))
$oCsv.SetCell($i,4,$oRow.StringOf("settlementDate"))
$oCsv.SetCell($i,5,$oRow.StringOf("netAmount"))
$i = $i + 1
Wend
$bSuccess = $oCsv.SaveFile2("schwab-transactions.csv","utf-8")
If ($bSuccess <> True) Then
ConsoleWrite("Could not write schwab-transactions.csv." & @CRLF)
Exit
EndIf
ConsoleWrite("Wrote " & $oCsv.NumRows & " rows to schwab-transactions.csv." & @CRLF)