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

.NET Core C#
bool success = false;

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

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

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

Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.AuthToken = accessToken;

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

http.SetUrlVar("account",accountHash);

//  Supply the ID as text so large numeric identifiers keep all their digits.
string orderId = "ORDER_ID_FROM_LIST";
http.SetUrlVar("orderId",orderId);
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
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) {
        Debug.WriteLine("No HTTP response. Check connectivity.");
    }
    else {
        Debug.WriteLine("Schwab returned HTTP " + Convert.ToString(resp.StatusCode));
        if (resp.StatusCode == 401) {
            Debug.WriteLine("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

Chilkat.JsonObject order = new Chilkat.JsonObject();
success = order.Load(resp.BodyStr);
if (success != true) {
    Debug.WriteLine("Schwab returned unexpected JSON.");
    return;
}

Debug.WriteLine("Order " + order.StringOf("orderId") + "  " + order.StringOf("status"));
Chilkat.JsonObject symbols = new Chilkat.JsonObject();
int i = 0;
while ((i < order.SizeOfArray("orderLegCollection"))) {
    order.I = i;
    string legId = order.StringOf("orderLegCollection[i].legId");
    string symbol = order.StringOf("orderLegCollection[i].instrument.symbol");
    symbols.UpdateString(legId,symbol);
    Debug.WriteLine("Leg " + legId + "  " + order.StringOf("orderLegCollection[i].instruction") + " " + order.StringOf("orderLegCollection[i].quantity") + " " + symbol);
    i = i + 1;
}

int fills = 0;
i = 0;
while ((i < order.SizeOfArray("orderActivityCollection"))) {
    order.I = i;
    if (order.StringOfEquals("orderActivityCollection[i].executionType","FILL",true) == true) {
        int j = 0;
        while ((j < order.SizeOfArray("orderActivityCollection[i].executionLegs"))) {
            order.J = j;
            string filledLeg = order.StringOf("orderActivityCollection[i].executionLegs[j].legId");
            Debug.WriteLine("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) {
    Debug.WriteLine("No fills reported for this order.");
}