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

C
#include <C_CkSecrets.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSecrets vault;
    HCkJsonObject entry;
    HCkJsonObject tokens;
    const char *accessToken;
    HCkHttp http;
    HCkJsonObject selection;
    const char *accountHash;
    const char *orderId;
    HCkHttpResponse resp;
    HCkJsonObject order;
    HCkJsonObject symbols;
    int i;
    const char *legId;
    const char *symbol;
    int fills;
    int j;
    const char *filledLeg;

    success = FALSE;

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

    vault = CkSecrets_Create();
    CkSecrets_putLocation(vault,"local_manager");
    CkSecrets_putAutoSplit(vault,TRUE);
    entry = CkJsonObject_Create();
    CkJsonObject_UpdateString(entry,"appName","SchwabTrader");
    CkJsonObject_UpdateString(entry,"service","OAuth2");
    CkJsonObject_UpdateString(entry,"username","personal-desktop");
    tokens = CkJsonObject_Create();
    success = CkSecrets_GetSecretJson(vault,entry,tokens);
    if (success != TRUE) {
        printf("Could not read the token record. Check the store and complete desktop authorization first.\n");
        CkSecrets_Dispose(vault);
        CkJsonObject_Dispose(entry);
        CkJsonObject_Dispose(tokens);
        return;
    }

    accessToken = CkJsonObject_stringOf(tokens,"access_token");
    if (accessToken == "") {
        printf("Run the Schwab authorization or refresh example to save an access token.\n");
        CkSecrets_Dispose(vault);
        CkJsonObject_Dispose(entry);
        CkJsonObject_Dispose(tokens);
        return;
    }

    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    CkHttp_putAuthToken(http,accessToken);

    //  Read the hash saved by the authorized-account selection example.
    CkJsonObject_UpdateString(entry,"service","SelectedAccount");
    selection = CkJsonObject_Create();
    success = CkSecrets_GetSecretJson(vault,entry,selection);
    accountHash = CkJsonObject_stringOf(selection,"hashValue");
    if ((success != TRUE) || (accountHash == "")) {
        printf("Select an authorized account first.\n");
        CkSecrets_Dispose(vault);
        CkJsonObject_Dispose(entry);
        CkJsonObject_Dispose(tokens);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(selection);
        return;
    }

    CkHttp_SetUrlVar(http,"account",accountHash);

    //  Supply the ID as text so large numeric identifiers keep all their digits.
    orderId = "ORDER_ID_FROM_LIST";
    CkHttp_SetUrlVar(http,"orderId",orderId);
    resp = CkHttpResponse_Create();
    success = CkHttp_HttpNoBody(http,"GET","https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",resp);
    if ((success != TRUE) || (CkHttpResponse_getStatusCode(resp) != 200)) {
        if (CkHttpResponse_getStatusCode(resp) == 0) {
            printf("No HTTP response. Check connectivity.\n");
        }
        else {
            printf("Schwab returned HTTP %d\n",CkHttpResponse_getStatusCode(resp));
            if (CkHttpResponse_getStatusCode(resp) == 401) {
                printf("Refresh the access token or repeat desktop authorization.\n");
            }

        }

        CkSecrets_Dispose(vault);
        CkJsonObject_Dispose(entry);
        CkJsonObject_Dispose(tokens);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(selection);
        CkHttpResponse_Dispose(resp);
        return;
    }

    order = CkJsonObject_Create();
    success = CkJsonObject_Load(order,CkHttpResponse_bodyStr(resp));
    if (success != TRUE) {
        printf("Schwab returned unexpected JSON.\n");
        CkSecrets_Dispose(vault);
        CkJsonObject_Dispose(entry);
        CkJsonObject_Dispose(tokens);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(selection);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(order);
        return;
    }

    printf("Order %s  %s\n",CkJsonObject_stringOf(order,"orderId"),CkJsonObject_stringOf(order,"status"));
    symbols = CkJsonObject_Create();
    i = 0;
    while ((i < CkJsonObject_SizeOfArray(order,"orderLegCollection"))) {
        CkJsonObject_putI(order,i);
        legId = CkJsonObject_stringOf(order,"orderLegCollection[i].legId");
        symbol = CkJsonObject_stringOf(order,"orderLegCollection[i].instrument.symbol");
        CkJsonObject_UpdateString(symbols,legId,symbol);
        printf("Leg %s  %s %s %s\n",legId,CkJsonObject_stringOf(order,"orderLegCollection[i].instruction"),CkJsonObject_stringOf(order,"orderLegCollection[i].quantity")
            ,symbol);
        i = i + 1;
    }

    fills = 0;
    i = 0;
    while ((i < CkJsonObject_SizeOfArray(order,"orderActivityCollection"))) {
        CkJsonObject_putI(order,i);
        if (CkJsonObject_StringOfEquals(order,"orderActivityCollection[i].executionType","FILL",TRUE) == TRUE) {
            j = 0;
            while ((j < CkJsonObject_SizeOfArray(order,"orderActivityCollection[i].executionLegs"))) {
                CkJsonObject_putJ(order,j);
                filledLeg = CkJsonObject_stringOf(order,"orderActivityCollection[i].executionLegs[j].legId");
                printf("Fill on leg %s  %s  quantity %s  price %s  time %s\n",filledLeg,CkJsonObject_stringOf(symbols,filledLeg),CkJsonObject_stringOf(order,"orderActivityCollection[i].executionLegs[j].quantity")
                    ,CkJsonObject_stringOf(order,"orderActivityCollection[i].executionLegs[j].price"),CkJsonObject_stringOf(order,"orderActivityCollection[i].executionLegs[j].time"));
                fills = fills + 1;
                j = j + 1;
            }

        }

        i = i + 1;
    }

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



    CkSecrets_Dispose(vault);
    CkJsonObject_Dispose(entry);
    CkJsonObject_Dispose(tokens);
    CkHttp_Dispose(http);
    CkJsonObject_Dispose(selection);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(order);
    CkJsonObject_Dispose(symbols);

    }