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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var vault = new CkSecrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
var entry = new CkJsonObject();
entry.UpdateString("appName","SchwabTrader");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","personal-desktop");
var tokens = new CkJsonObject();
success = vault.GetSecretJson(entry,tokens);
if (success !== true) {
    console.log("Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

var accessToken = tokens.StringOf("access_token");
if (accessToken == "") {
    console.log("Run the Schwab authorization or refresh example to save an access token.");
    return;
}

var http = new CkHttp();
http.RequireSslCertVerify = true;
http.AuthToken = accessToken;

//  Read the hash saved by the authorized-account selection example.
entry.UpdateString("service","SelectedAccount");
var selection = new CkJsonObject();
success = vault.GetSecretJson(entry,selection);
var accountHash = selection.StringOf("hashValue");
if ((success !== true) || (accountHash == "")) {
    console.log("Select an authorized account first.");
    return;
}

http.SetUrlVar("account",accountHash);

//  Supply the ID as text so large numeric identifiers keep all their digits.
var orderId = "ORDER_ID_FROM_LIST";
http.SetUrlVar("orderId",orderId);
var resp = new CkHttpResponse();
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",resp);
if ((success !== true) || (resp.StatusCode !== 200)) {
    if (resp.StatusCode == 0) {
        console.log("No HTTP response. Check connectivity.");
    }
    else {
        console.log("Schwab returned HTTP " + resp.StatusCode);
        if (resp.StatusCode == 401) {
            console.log("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

var order = new CkJsonObject();
success = order.Load(resp.BodyStr);
if (success !== true) {
    console.log("Schwab returned unexpected JSON.");
    return;
}

console.log("Order " + order.StringOf("orderId") + "  " + order.StringOf("status"));
var symbols = new CkJsonObject();
var i = 0;
while ((i < order.SizeOfArray("orderLegCollection"))) {
    order.I = i;
    var legId = order.StringOf("orderLegCollection[i].legId");
    var symbol = order.StringOf("orderLegCollection[i].instrument.symbol");
    symbols.UpdateString(legId,symbol);
    console.log("Leg " + legId + "  " + order.StringOf("orderLegCollection[i].instruction") + " " + order.StringOf("orderLegCollection[i].quantity") + " " + symbol);
    i = i+1;
}

var fills = 0;
i = 0;
while ((i < order.SizeOfArray("orderActivityCollection"))) {
    order.I = i;
    if (order.StringOfEquals("orderActivityCollection[i].executionType","FILL",true) == true) {
        var j = 0;
        while ((j < order.SizeOfArray("orderActivityCollection[i].executionLegs"))) {
            order.J = j;
            var filledLeg = order.StringOf("orderActivityCollection[i].executionLegs[j].legId");
            console.log("Fill on leg " + filledLeg + "  " + symbols.StringOf(filledLeg) + "  quantity " + order.StringOf("orderActivityCollection[i].executionLegs[j].quantity")
                 + "  price " + order.StringOf("orderActivityCollection[i].executionLegs[j].price") + "  time " + order.StringOf("orderActivityCollection[i].executionLegs[j].time"));
            fills = fills+1;
            j = j+1;
        }

    }

    i = i+1;
}

if (fills == 0) {
    console.log("No fills reported for this order.");
}