Rust Requires Chilkat v11.0.0+
Rust
Schwab: Get quotes for a watchlist
See more Schwab Examples
Requests several symbols in one call and prints the bid, ask, last price, quote timestamp in Unix milliseconds, and the real-time indicator when available. Missing quote fields can occur for individual instruments. The response uses symbol names as JSON keys and can report invalid symbols alongside valid quotes.
Use the access token saved by desktop authorization. Enable the Market Data API for the Schwab app. 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);
let resp = chilkat::HttpResponse::new();
let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("symbols", "AAPL,MSFT,BRK/B");
query.add_param("fields", "quote");
success = http.http_req("https://api.schwabapi.com/marketdata/v1/quotes", &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;
}
// Read members by index. A symbol may contain punctuation meaningful in a JSON path.
let instrument = chilkat::JsonObject::new();
let mut i = 0;
while i < reply.size() {
let key = reply.name_at(i).unwrap_or_default();
if key != "errors" {
if reply.object_at2(i, &instrument).is_ok() {
println!("{} bid {} ask {} last {}", key, instrument.string_of("quote.bidPrice").unwrap_or_default(), instrument.string_of("quote.askPrice").unwrap_or_default(), instrument.string_of("quote.lastPrice").unwrap_or_default());
println!(" quote time (Unix ms): {} real-time: {}", instrument.string_of("quote.quoteTime").unwrap_or_default(), instrument.string_of("realtime").unwrap_or_default());
}
}
i = i + 1;
}
let mut j = 0;
while j < reply.size_of_array("errors.invalidSymbols") {
reply.set_j(j);
println!("Invalid symbol: {}", reply.string_of("errors.invalidSymbols[j]").unwrap_or_default());
j = j + 1;
}