Rust Requires Chilkat v11.0.0+
Rust
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.
Chilkat Rust Downloads
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);
// Supply the ID as text so large numeric identifiers keep all their digits.
let order_id = "ORDER_ID_FROM_LIST".to_string();
let _ = http.set_url_var("orderId", &order_id);
let resp = chilkat::HttpResponse::new();
success = http.http_no_body("GET", "https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}", &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 order = chilkat::JsonObject::new();
if order.load(&resp.body_str()).is_err() {
println!("Schwab returned unexpected JSON.");
return;
}
println!("Order {} {}", order.string_of("orderId").unwrap_or_default(), order.string_of("status").unwrap_or_default());
let symbols = chilkat::JsonObject::new();
let mut i = 0;
while i < order.size_of_array("orderLegCollection") {
order.set_i(i);
let leg_id = order.string_of("orderLegCollection[i].legId").unwrap_or_default();
let symbol = order.string_of("orderLegCollection[i].instrument.symbol").unwrap_or_default();
let _ = symbols.update_string(&leg_id, &symbol);
println!("Leg {} {} {} {}", leg_id, order.string_of("orderLegCollection[i].instruction").unwrap_or_default(), order.string_of("orderLegCollection[i].quantity").unwrap_or_default(), symbol);
i = i + 1;
}
let mut fills = 0;
i = 0;
while i < order.size_of_array("orderActivityCollection") {
order.set_i(i);
if order.string_of_equals("orderActivityCollection[i].executionType", "FILL", true) {
let mut j = 0;
while j < order.size_of_array("orderActivityCollection[i].executionLegs") {
order.set_j(j);
let filled_leg = order.string_of("orderActivityCollection[i].executionLegs[j].legId").unwrap_or_default();
println!("Fill on leg {} {} quantity {} price {} time {}", filled_leg, symbols.string_of(&filled_leg).unwrap_or_default(), order.string_of("orderActivityCollection[i].executionLegs[j].quantity").unwrap_or_default(), order.string_of("orderActivityCollection[i].executionLegs[j].price").unwrap_or_default(), order.string_of("orderActivityCollection[i].executionLegs[j].time").unwrap_or_default());
fills = fills + 1;
j = j + 1;
}
}
i = i + 1;
}
if fills == 0 {
println!("No fills reported for this order.");
}