Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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.

Schwab API specification

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL cAccessToken
LOCAL oHttp
LOCAL oSelection
LOCAL cAccountHash
LOCAL oClock
LOCAL oEndTime
LOCAL nReplacements
LOCAL oStartTime
LOCAL oResp
LOCAL oQuery
LOCAL oActivity
LOCAL oCsv
LOCAL oRow
LOCAL i

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "SchwabTrader")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "personal-desktop")
oTokens := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oTokens)
IF (nSuccess != 1)
    ? "Could not read the token record. Check the store and complete desktop authorization first."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

cAccessToken := oTokens:StringOf("access_token")
IF (cAccessToken == "")
    ? "Run the Schwab authorization or refresh example to save an access token."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := cAccessToken

//  Read the hash saved by the authorized-account selection example.
oEntry:UpdateString("service", "SelectedAccount")
oSelection := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oSelection)
cAccountHash := oSelection:StringOf("hashValue")
IF ((nSuccess != 1) .OR. (cAccountHash == ""))
    ? "Select an authorized account first."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    RETURN
ENDIF

oHttp:SetUrlVar("account", cAccountHash)

//  Use UTC endpoints for a rolling seven-day window.
oClock := CreateObject("Chilkat.CkDateTime")
oClock:SetFromCurrentSystemTime()
oEndTime := CreateObject("Chilkat.StringBuilder")
oEndTime:Append(oClock:GetAsTimestamp(0))
nReplacements := oEndTime:Replace("Z", ".000Z")
oClock:AddDays(-7)
oStartTime := CreateObject("Chilkat.StringBuilder")
oStartTime:Append(oClock:GetAsTimestamp(0))
nReplacements := oStartTime:Replace("Z", ".000Z")
oResp := CreateObject("Chilkat.HttpResponse")
oQuery := CreateObject("Chilkat.HttpRequest")
oQuery:HttpVerb := "GET"
oQuery:AddParam("startDate", oStartTime:GetAsString())
oQuery:AddParam("endDate", oEndTime:GetAsString())
oQuery:AddParam("types", "TRADE,DIVIDEND_OR_INTEREST")
nSuccess := oHttp:HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}/transactions", oQuery, oResp)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
    IF (oResp:StatusCode == 0)
        ? "No HTTP response. Check connectivity."
    ELSE
        ? "Schwab returned HTTP " + Str(oResp:StatusCode)
        IF (oResp:StatusCode == 401)
            ? "Refresh the access token or repeat desktop authorization."
        ENDIF

    ENDIF

    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oClock:destroy()
    oEndTime:destroy()
    oStartTime:destroy()
    oResp:destroy()
    oQuery:destroy()
    RETURN
ENDIF

oActivity := CreateObject("Chilkat.JsonArray")
nSuccess := oActivity:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Schwab returned unexpected JSON."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oClock:destroy()
    oEndTime:destroy()
    oStartTime:destroy()
    oResp:destroy()
    oQuery:destroy()
    oActivity:destroy()
    RETURN
ENDIF

IF (oActivity:Size >= 3000)
    ? "The response reached the transaction limit. Export smaller time windows."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oClock:destroy()
    oEndTime:destroy()
    oStartTime:destroy()
    oResp:destroy()
    oQuery:destroy()
    oActivity:destroy()
    RETURN
ENDIF

oCsv := CreateObject("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 := CreateObject("Chilkat.JsonObject")
i := 0
DO 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
ENDDO
nSuccess := oCsv:SaveFile2("schwab-transactions.csv", "utf-8")
IF (nSuccess != 1)
    ? "Could not write schwab-transactions.csv."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oClock:destroy()
    oEndTime:destroy()
    oStartTime:destroy()
    oResp:destroy()
    oQuery:destroy()
    oActivity:destroy()
    oCsv:destroy()
    oRow:destroy()
    RETURN
ENDIF

? "Wrote " + Str(oCsv:NumRows) + " rows to schwab-transactions.csv."

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oSelection:destroy()
oClock:destroy()
oEndTime:destroy()
oStartTime:destroy()
oResp:destroy()
oQuery:destroy()
oActivity:destroy()
oCsv:destroy()
oRow:destroy()