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

Schwab: Read account balances and positions

See more Schwab Examples

Requests fields=positions for the selected account. Reads current cash and liquidation value, prints buying power when supplied, and reports each holding’s symbol, long and short quantities, and market value. A cash-only portfolio can have balances and zero positions.

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 <CkHttpResponseW.h>
#include <CkHttpRequestW.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);

    CkHttpResponseW resp;
    CkHttpRequestW query;
    query.put_HttpVerb(L"GET");
    query.AddParam(L"fields",L"positions");
    success = http.HttpReq(L"https://api.schwabapi.com/trader/v1/accounts/{$account}",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;
    }

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

    CkJsonObjectW balances;
    success = reply.ObjectOf2(L"securitiesAccount.currentBalances",balances);
    if (success != true) {
        wprintf(L"The account response contained no current balances.\n");
        return;
    }

    wprintf(L"Cash balance: %s\n",balances.stringOf(L"cashBalance"));
    wprintf(L"Liquidation value: %s\n",balances.stringOf(L"liquidationValue"));
    if (balances.HasMember(L"buyingPower") == true) {
        wprintf(L"Buying power: %s\n",balances.stringOf(L"buyingPower"));
    }

    int count = reply.SizeOfArray(L"securitiesAccount.positions");
    if (count <= 0) {
        wprintf(L"No positions.\n");
    }

    int i = 0;
    while ((i < count)) {
        reply.put_I(i);
        wprintf(L"%s  long %s  short %s  market value %s\n",reply.stringOf(L"securitiesAccount.positions[i].instrument.symbol"),reply.stringOf(L"securitiesAccount.positions[i].longQuantity")
            ,reply.stringOf(L"securitiesAccount.positions[i].shortQuantity"),reply.stringOf(L"securitiesAccount.positions[i].marketValue"));
        i = i + 1;
    }
    }