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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

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

        Secrets vault = new Secrets();
        vault.setLocation("local_manager");
        vault.setAutoSplit(true);
        JsonObject entry = new JsonObject();
        entry.updateString("appName", "SchwabTrader");
        entry.updateString("service", "OAuth2");
        entry.updateString("username", "personal-desktop");
        JsonObject tokens = new JsonObject();
        try {
            vault.getSecretJson(entry, tokens);
        } catch (ChilkatException e) {
            System.out.println("Could not read the token record. Check the store and complete desktop authorization first.");
            return;
        }

        String accessToken = tokens.stringOf("access_token");
        if ("".equals(accessToken)) {
            System.out.println("Run the Schwab authorization or refresh example to save an access token.");
            return;
        }

        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setAuthToken(accessToken);

        // Read the hash saved by the authorized-account selection example.
        entry.updateString("service", "SelectedAccount");
        JsonObject selection = new JsonObject();
        try {
            vault.getSecretJson(entry, selection);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        String accountHash = selection.stringOf("hashValue");
        if ((!success) || ("".equals(accountHash))) {
            System.out.println("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);
        HttpResponse resp = new HttpResponse();
        try {
            http.httpNoBody("GET", "https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}", resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if ((!success) || (resp.getStatusCode() != 200)) {
            if (resp.getStatusCode() == 0) {
                System.out.println("No HTTP response. Check connectivity.");
            } else {
                System.out.println("Schwab returned HTTP " + resp.getStatusCode());
                if (resp.getStatusCode() == 401) {
                    System.out.println("Refresh the access token or repeat desktop authorization.");
                }
            }

            return;
        }

        JsonObject order = new JsonObject();
        try {
            order.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println("Schwab returned unexpected JSON.");
            return;
        }

        System.out.println("Order " + order.stringOf("orderId") + "  " + order.stringOf("status"));
        JsonObject symbols = new JsonObject();
        int i = 0;
        while (i < order.sizeOfArray("orderLegCollection")) {
            order.setI(i);
            String legId = order.stringOf("orderLegCollection[i].legId");
            String symbol = order.stringOf("orderLegCollection[i].instrument.symbol");
            symbols.updateString(legId, symbol);
            System.out.println("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.setI(i);
            if (order.stringOfEquals("orderActivityCollection[i].executionType", "FILL", true)) {
                int j = 0;
                while (j < order.sizeOfArray("orderActivityCollection[i].executionLegs")) {
                    order.setJ(j);
                    String filledLeg = order.stringOf("orderActivityCollection[i].executionLegs[j].legId");
                    System.out.println("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) {
            System.out.println("No fills reported for this order.");
        }
    }
}