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

Schwab: Read account balances and positions

See more Schwab Examples

Requests fields=positions for the selected account. Reads current cash and liquidation value, prints buying power when supplied, and reports each holding’s symbol, long and short quantities, and market value. A cash-only portfolio can have balances and zero positions.

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);

let resp = chilkat::HttpResponse::new();
let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("fields", "positions");
success = http.http_req("https://api.schwabapi.com/trader/v1/accounts/{$account}", &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 reply = chilkat::JsonObject::new();
if reply.load(&resp.body_str()).is_err() {
    println!("Schwab returned unexpected JSON.");
    return;
}

let balances = chilkat::JsonObject::new();
if reply.object_of2("securitiesAccount.currentBalances", &balances).is_err() {
    println!("The account response contained no current balances.");
    return;
}

println!("Cash balance: {}", balances.string_of("cashBalance").unwrap_or_default());
println!("Liquidation value: {}", balances.string_of("liquidationValue").unwrap_or_default());
if balances.has_member("buyingPower") {
    println!("Buying power: {}", balances.string_of("buyingPower").unwrap_or_default());
}

let count = reply.size_of_array("securitiesAccount.positions");
if count <= 0 {
    println!("No positions.");
}

let mut i = 0;
while i < count {
    reply.set_i(i);
    println!("{}  long {}  short {}  market value {}", reply.string_of("securitiesAccount.positions[i].instrument.symbol").unwrap_or_default(), reply.string_of("securitiesAccount.positions[i].longQuantity").unwrap_or_default(), reply.string_of("securitiesAccount.positions[i].shortQuantity").unwrap_or_default(), reply.string_of("securitiesAccount.positions[i].marketValue").unwrap_or_default());
    i = i + 1;
}