Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoVault
    Variant vEntry
    Handle hoEntry
    Variant vTokens
    Handle hoTokens
    String sAccessToken
    Handle hoHttp
    Variant vSelection
    Handle hoSelection
    String sAccountHash
    Handle hoClock
    Handle hoEndTime
    Integer iReplacements
    Handle hoStartTime
    Variant vResp
    Handle hoResp
    Variant vQuery
    Handle hoQuery
    Handle hoActivity
    Handle hoCsv
    Variant vRow
    Handle hoRow
    Integer i
    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

    //  Read the hash saved by the authorized-account selection example.
    Get ComUpdateString Of hoEntry "service" "SelectedAccount" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoSelection
    If (Not(IsComObjectCreated(hoSelection))) Begin
        Send CreateComObject of hoSelection
    End
    Get pvComObject of hoEntry to vEntry
    Get pvComObject of hoSelection to vSelection
    Get ComGetSecretJson Of hoVault vEntry vSelection To iSuccess
    Get ComStringOf Of hoSelection "hashValue" To sAccountHash
    If ((iSuccess <> True) Or (sAccountHash = "")) Begin
        Showln "Select an authorized account first."
        Procedure_Return
    End

    Get ComSetUrlVar Of hoHttp "account" sAccountHash To iSuccess

    //  Use UTC endpoints for a rolling seven-day window.
    Get Create (RefClass(cComCkDateTime)) To hoClock
    If (Not(IsComObjectCreated(hoClock))) Begin
        Send CreateComObject of hoClock
    End
    Get ComSetFromCurrentSystemTime Of hoClock To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEndTime
    If (Not(IsComObjectCreated(hoEndTime))) Begin
        Send CreateComObject of hoEndTime
    End
    Get ComGetAsTimestamp Of hoClock False To sTemp1
    Get ComAppend Of hoEndTime sTemp1 To iSuccess
    Get ComReplace Of hoEndTime "Z" ".000Z" To iReplacements
    Get ComAddDays Of hoClock -7 To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoStartTime
    If (Not(IsComObjectCreated(hoStartTime))) Begin
        Send CreateComObject of hoStartTime
    End
    Get ComGetAsTimestamp Of hoClock False To sTemp1
    Get ComAppend Of hoStartTime sTemp1 To iSuccess
    Get ComReplace Of hoStartTime "Z" ".000Z" To iReplacements
    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"
    Get ComGetAsString Of hoStartTime To sTemp1
    Send ComAddParam To hoQuery "startDate" sTemp1
    Get ComGetAsString Of hoEndTime To sTemp1
    Send ComAddParam To hoQuery "endDate" sTemp1
    Send ComAddParam To hoQuery "types" "TRADE,DIVIDEND_OR_INTEREST"
    Get pvComObject of hoQuery to vQuery
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp "https://api.schwabapi.com/trader/v1/accounts/{$account}/transactions" 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(cComChilkatJsonArray)) To hoActivity
    If (Not(IsComObjectCreated(hoActivity))) Begin
        Send CreateComObject of hoActivity
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoActivity sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Schwab returned unexpected JSON."
        Procedure_Return
    End

    Get ComSize Of hoActivity To iTemp1
    If (iTemp1 >= 3000) Begin
        Showln "The response reached the transaction limit. Export smaller time windows."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCsv)) To hoCsv
    If (Not(IsComObjectCreated(hoCsv))) Begin
        Send CreateComObject of hoCsv
    End
    Get ComSetColumnName Of hoCsv 0 "activityId" To iSuccess
    Get ComSetColumnName Of hoCsv 1 "time" To iSuccess
    Get ComSetColumnName Of hoCsv 2 "type" To iSuccess
    Get ComSetColumnName Of hoCsv 3 "tradeDate" To iSuccess
    Get ComSetColumnName Of hoCsv 4 "settlementDate" To iSuccess
    Get ComSetColumnName Of hoCsv 5 "netAmount" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoRow
    If (Not(IsComObjectCreated(hoRow))) Begin
        Send CreateComObject of hoRow
    End
    Move 0 To i
    While (i < (ComSize(hoActivity)))
        Get pvComObject of hoRow to vRow
        Get ComObjectAt2 Of hoActivity i vRow To iSuccess
        //  Keep identifiers and decimal amounts as returned by Schwab.
        Get ComStringOf Of hoRow "activityId" To sTemp1
        Get ComSetCell Of hoCsv i 0 sTemp1 To iSuccess
        Get ComStringOf Of hoRow "time" To sTemp1
        Get ComSetCell Of hoCsv i 1 sTemp1 To iSuccess
        Get ComStringOf Of hoRow "type" To sTemp1
        Get ComSetCell Of hoCsv i 2 sTemp1 To iSuccess
        Get ComStringOf Of hoRow "tradeDate" To sTemp1
        Get ComSetCell Of hoCsv i 3 sTemp1 To iSuccess
        Get ComStringOf Of hoRow "settlementDate" To sTemp1
        Get ComSetCell Of hoCsv i 4 sTemp1 To iSuccess
        Get ComStringOf Of hoRow "netAmount" To sTemp1
        Get ComSetCell Of hoCsv i 5 sTemp1 To iSuccess
        Move (i + 1) To i
    Loop

    Get ComSaveFile2 Of hoCsv "schwab-transactions.csv" "utf-8" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not write schwab-transactions.csv."
        Procedure_Return
    End

    Get ComNumRows Of hoCsv To iTemp1
    Showln "Wrote " iTemp1 " rows to schwab-transactions.csv."


End_Procedure