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

Schwab: List recent orders for one account

See more Schwab Examples

Lists orders entered in the last seven days, with an optional status filter such as WORKING. Shows the order ID, entry time, status, filled quantity, and remaining quantity. Select an ID from the results for the order detail and fills example. An older order can still be working outside this entry-time window.

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 Rust Downloads

Rust
let mut success = false;

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

let vault = chilkat::Secrets::new();
vault.set_location("local_manager");
vault.set_auto_split(true);
let entry = chilkat::JsonObject::new();
let _ = entry.update_string("appName", "SchwabTrader");
let _ = entry.update_string("service", "OAuth2");
let _ = entry.update_string("username", "personal-desktop");
let tokens = chilkat::JsonObject::new();
success = vault.get_secret_json(&entry, &tokens).is_ok();
if !success {
    println!("Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

let access_token = tokens.string_of("access_token").unwrap_or_default();
if access_token == "" {
    println!("Run the Schwab authorization or refresh example to save an access token.");
    return;
}

let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&access_token);

// Read the hash saved by the authorized-account selection example.
let _ = entry.update_string("service", "SelectedAccount");
let selection = chilkat::JsonObject::new();
success = vault.get_secret_json(&entry, &selection).is_ok();
let account_hash = selection.string_of("hashValue").unwrap_or_default();
if (!success) || (account_hash == "") {
    println!("Select an authorized account first.");
    return;
}

let _ = http.set_url_var("account", &account_hash);

// Use UTC endpoints for a rolling seven-day window.
let clock = chilkat::DateTime::new();
let _ = clock.set_from_current_system_time();
let end_time = chilkat::StringBuilder::new();
let _ = end_time.append(&clock.get_as_timestamp(false).unwrap_or_default());
let mut replacements = end_time.replace("Z", ".000Z");
let _ = clock.add_days(-7);
let start_time = chilkat::StringBuilder::new();
let _ = start_time.append(&clock.get_as_timestamp(false).unwrap_or_default());
replacements = start_time.replace("Z", ".000Z");
let status_filter = String::new();
let resp = chilkat::HttpResponse::new();
let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("fromEnteredTime", &start_time.get_as_string().unwrap_or_default());
query.add_param("toEnteredTime", &end_time.get_as_string().unwrap_or_default());
query.add_param("maxResults", "100");
if status_filter != "" {
    query.add_param("status", &status_filter);
}

success = http.http_req("https://api.schwabapi.com/trader/v1/accounts/{$account}/orders", &query, &resp).is_ok();
if (!success) || (resp.status_code() != 200) {
    if resp.status_code() == 0 {
        println!("No HTTP response. Check connectivity.");
    } else {
        println!("Schwab returned HTTP {}", resp.status_code());
        if resp.status_code() == 401 {
            println!("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

let orders = chilkat::JsonArray::new();
if orders.load(&resp.body_str()).is_err() {
    println!("Schwab returned unexpected JSON.");
    return;
}

if orders.size() == 0 {
    println!("No matching orders entered during this interval.");
}

if orders.size() == 100 {
    println!("Reached maxResults. Narrow the interval to check for additional orders.");
}

let order = chilkat::JsonObject::new();
let mut i = 0;
while i < orders.size() {
    let _ = orders.object_at2(i, &order);
    println!("Order {}  {}  {}  {}", order.string_of("orderId").unwrap_or_default(), order.string_of("enteredTime").unwrap_or_default(), order.string_of("orderType").unwrap_or_default(), order.string_of("status").unwrap_or_default());
    println!("  filled {}  remaining {}", order.string_of("filledQuantity").unwrap_or_default(), order.string_of("remainingQuantity").unwrap_or_default());
    i = i + 1;
}