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

Unicode C++
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>
#include <CkHttpRequestW.h>
#include <CkJsonArrayW.h>
#include <CkCsvW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;

    CkSecretsW vault;
    vault.put_Location(L"local_manager");
    vault.put_AutoSplit(true);
    CkJsonObjectW entry;
    entry.UpdateString(L"appName",L"SchwabTrader");
    entry.UpdateString(L"service",L"OAuth2");
    entry.UpdateString(L"username",L"personal-desktop");
    CkJsonObjectW tokens;
    success = vault.GetSecretJson(entry,tokens);
    if (success != true) {
        wprintf(L"Could not read the token record. Check the store and complete desktop authorization first.\n");
        return;
    }

    const wchar_t *accessToken = tokens.stringOf(L"access_token");
    if (accessToken == L"") {
        wprintf(L"Run the Schwab authorization or refresh example to save an access token.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(accessToken);

    //  Read the hash saved by the authorized-account selection example.
    entry.UpdateString(L"service",L"SelectedAccount");
    CkJsonObjectW selection;
    success = vault.GetSecretJson(entry,selection);
    const wchar_t *accountHash = selection.stringOf(L"hashValue");
    if ((success != true) || (accountHash == L"")) {
        wprintf(L"Select an authorized account first.\n");
        return;
    }

    http.SetUrlVar(L"account",accountHash);

    //  Use UTC endpoints for a rolling seven-day window.
    CkDateTimeW clock;
    clock.SetFromCurrentSystemTime();
    CkStringBuilderW endTime;
    endTime.Append(clock.getAsTimestamp(false));
    int replacements = endTime.Replace(L"Z",L".000Z");
    clock.AddDays(-7);
    CkStringBuilderW startTime;
    startTime.Append(clock.getAsTimestamp(false));
    replacements = startTime.Replace(L"Z",L".000Z");
    CkHttpResponseW resp;
    CkHttpRequestW query;
    query.put_HttpVerb(L"GET");
    query.AddParam(L"startDate",startTime.getAsString());
    query.AddParam(L"endDate",endTime.getAsString());
    query.AddParam(L"types",L"TRADE,DIVIDEND_OR_INTEREST");
    success = http.HttpReq(L"https://api.schwabapi.com/trader/v1/accounts/{$account}/transactions",query,resp);
    if ((success != true) || (resp.get_StatusCode() != 200)) {
        if (resp.get_StatusCode() == 0) {
            wprintf(L"No HTTP response. Check connectivity.\n");
        }
        else {
            wprintf(L"Schwab returned HTTP %d\n",resp.get_StatusCode());
            if (resp.get_StatusCode() == 401) {
                wprintf(L"Refresh the access token or repeat desktop authorization.\n");
            }

        }

        return;
    }

    CkJsonArrayW activity;
    success = activity.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Schwab returned unexpected JSON.\n");
        return;
    }

    if (activity.get_Size() >= 3000) {
        wprintf(L"The response reached the transaction limit. Export smaller time windows.\n");
        return;
    }

    CkCsvW csv;
    csv.SetColumnName(0,L"activityId");
    csv.SetColumnName(1,L"time");
    csv.SetColumnName(2,L"type");
    csv.SetColumnName(3,L"tradeDate");
    csv.SetColumnName(4,L"settlementDate");
    csv.SetColumnName(5,L"netAmount");
    CkJsonObjectW row;
    int i = 0;
    while ((i < activity.get_Size())) {
        activity.ObjectAt2(i,row);
        //  Keep identifiers and decimal amounts as returned by Schwab.
        csv.SetCell(i,0,row.stringOf(L"activityId"));
        csv.SetCell(i,1,row.stringOf(L"time"));
        csv.SetCell(i,2,row.stringOf(L"type"));
        csv.SetCell(i,3,row.stringOf(L"tradeDate"));
        csv.SetCell(i,4,row.stringOf(L"settlementDate"));
        csv.SetCell(i,5,row.stringOf(L"netAmount"));
        i = i + 1;
    }

    success = csv.SaveFile2(L"schwab-transactions.csv",L"utf-8");
    if (success != true) {
        wprintf(L"Could not write schwab-transactions.csv.\n");
        return;
    }

    wprintf(L"Wrote %d rows to schwab-transactions.csv.\n",csv.get_NumRows());
    }