B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim vault As ChilkatSecrets
vault.Initialize("vault")
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As ChilkatJsonObject
entry.Initialize
entry.UpdateString("appName", "SchwabTrader")
entry.UpdateString("service", "OAuth2")
entry.UpdateString("username", "personal-desktop")
Dim tokens As ChilkatJsonObject
tokens.Initialize
success = vault.GetSecretJson(entry, tokens)
If success <> True Then
Log("Could not read the token record. Check the store and complete desktop authorization first.")
Return
End If
Dim accessToken As String = tokens.StringOf("access_token")
If accessToken = "" Then
Log("Run the Schwab authorization or refresh example to save an access token.")
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.AuthToken = accessToken
' Read the hash saved by the authorized-account selection example.
entry.UpdateString("service", "SelectedAccount")
Dim selection As ChilkatJsonObject
selection.Initialize
success = vault.GetSecretJson(entry, selection)
Dim accountHash As String = selection.StringOf("hashValue")
If (success <> True) Or (accountHash = "") Then
Log("Select an authorized account first.")
Return
End If
http.SetUrlVar("account", accountHash)
' Use UTC endpoints for a rolling seven-day window.
Dim clock As ChilkatDateTime
clock.Initialize
clock.SetFromCurrentSystemTime
Dim endTime As ChilkatStringBuilder
endTime.Initialize
endTime.Append(clock.GetAsTimestamp(False))
Dim replacements As Int = endTime.Replace("Z", ".000Z")
clock.AddDays(-7)
Dim startTime As ChilkatStringBuilder
startTime.Initialize
startTime.Append(clock.GetAsTimestamp(False))
replacements = startTime.Replace("Z", ".000Z")
Dim resp As ChilkatHttpResponse
resp.Initialize
Dim query As ChilkatHttpRequest
query.Initialize
query.HttpVerb = "GET"
query.AddParam("startDate", startTime.GetAsString)
query.AddParam("endDate", endTime.GetAsString)
query.AddParam("types", "TRADE,DIVIDEND_OR_INTEREST")
success = http.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}/transactions", query, resp)
If (success <> True) Or (resp.StatusCode <> 200) Then
If resp.StatusCode = 0 Then
Log("No HTTP response. Check connectivity.")
Else
Log("Schwab returned HTTP " & resp.StatusCode)
If resp.StatusCode = 401 Then
Log("Refresh the access token or repeat desktop authorization.")
End If
End If
Return
End If
Dim activity As ChilkatJsonArray
activity.Initialize
success = activity.Load(resp.BodyStr)
If success <> True Then
Log("Schwab returned unexpected JSON.")
Return
End If
If activity.Size >= 3000 Then
Log("The response reached the transaction limit. Export smaller time windows.")
Return
End If
Dim csv As ChilkatCsv
csv.Initialize
csv.SetColumnName(0, "activityId")
csv.SetColumnName(1, "time")
csv.SetColumnName(2, "type")
csv.SetColumnName(3, "tradeDate")
csv.SetColumnName(4, "settlementDate")
csv.SetColumnName(5, "netAmount")
Dim row As ChilkatJsonObject
row.Initialize
Dim i As Int = 0
Do While (i < activity.Size)
activity.ObjectAt2(i, row)
' Keep identifiers and decimal amounts as returned by Schwab.
csv.SetCell(i, 0, row.StringOf("activityId"))
csv.SetCell(i, 1, row.StringOf("time"))
csv.SetCell(i, 2, row.StringOf("type"))
csv.SetCell(i, 3, row.StringOf("tradeDate"))
csv.SetCell(i, 4, row.StringOf("settlementDate"))
csv.SetCell(i, 5, row.StringOf("netAmount"))
i = i + 1
Loop
success = csv.SaveFile2("schwab-transactions.csv", "utf-8")
If success <> True Then
Log("Could not write schwab-transactions.csv.")
Return
End If
Log("Wrote " & csv.NumRows & " rows to schwab-transactions.csv.")