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

Schwab: Inspect an order, its legs, and fills

See more Schwab Examples

Use an order ID from the selected account’s recent orders. The example reads a single order and matches executions to order legs by legId. Each execution can have its own quantity, price, and timestamp. Working orders can have zero or partial fills. The loops cover the returned order’s own legs and activities.

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>

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);

    //  Supply the ID as text so large numeric identifiers keep all their digits.
    const wchar_t *orderId = L"ORDER_ID_FROM_LIST";
    http.SetUrlVar(L"orderId",orderId);
    CkHttpResponseW resp;
    success = http.HttpNoBody(L"GET",L"https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",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 order;
    success = order.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Schwab returned unexpected JSON.\n");
        return;
    }

    wprintf(L"Order %s  %s\n",order.stringOf(L"orderId"),order.stringOf(L"status"));
    CkJsonObjectW symbols;
    int i = 0;
    while ((i < order.SizeOfArray(L"orderLegCollection"))) {
        order.put_I(i);
        const wchar_t *legId = order.stringOf(L"orderLegCollection[i].legId");
        const wchar_t *symbol = order.stringOf(L"orderLegCollection[i].instrument.symbol");
        symbols.UpdateString(legId,symbol);
        wprintf(L"Leg %s  %s %s %s\n",legId,order.stringOf(L"orderLegCollection[i].instruction"),order.stringOf(L"orderLegCollection[i].quantity"),symbol);
        i = i + 1;
    }

    int fills = 0;
    i = 0;
    while ((i < order.SizeOfArray(L"orderActivityCollection"))) {
        order.put_I(i);
        if (order.StringOfEquals(L"orderActivityCollection[i].executionType",L"FILL",true) == true) {
            int j = 0;
            while ((j < order.SizeOfArray(L"orderActivityCollection[i].executionLegs"))) {
                order.put_J(j);
                const wchar_t *filledLeg = order.stringOf(L"orderActivityCollection[i].executionLegs[j].legId");
                wprintf(L"Fill on leg %s  %s  quantity %s  price %s  time %s\n",filledLeg,symbols.stringOf(filledLeg),order.stringOf(L"orderActivityCollection[i].executionLegs[j].quantity")
                    ,order.stringOf(L"orderActivityCollection[i].executionLegs[j].price"),order.stringOf(L"orderActivityCollection[i].executionLegs[j].time"));
                fills = fills + 1;
                j = j + 1;
            }

        }

        i = i + 1;
    }

    if (fills == 0) {
        wprintf(L"No fills reported for this order.\n");
    }
    }