Rust Requires Chilkat v11.0.0+
Rust
Schwab: Find an option contract by expiration and strike
See more Schwab Examples
Filters a chain by underlying, call or put, exact expiration, and strike. Choose a currently listed expiration and strike. Schwab groups contracts by expiration and strike and may return multiple deliverables. Compare the descriptions and use the exact returned contract symbol, including its spaces, for a later quote request.
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 underlying = "AAPL".to_string();
let expiration = "2027-01-15".to_string();
let strike = "250".to_string();
let side = "CALL".to_string();
let resp = chilkat::HttpResponse::new();
let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("symbol", &underlying);
query.add_param("contractType", &side);
query.add_param("strike", &strike);
query.add_param("fromDate", &expiration);
query.add_param("toDate", &expiration);
success = http.http_req("https://api.schwabapi.com/marketdata/v1/chains", &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 mut map_name = "callExpDateMap".to_string();
if side == "PUT" {
map_name = "putExpDateMap".to_string();
}
let expirations = chilkat::JsonObject::new();
if reply.object_of2(&map_name, &expirations).is_err() {
println!("No option map returned. Chain status: {}", reply.string_of("status").unwrap_or_default());
return;
}
// Expiration keys contain a day count. Strike keys may contain a decimal point.
let strikes = chilkat::JsonObject::new();
let contracts = chilkat::JsonArray::new();
let contract = chilkat::JsonObject::new();
let mut found = 0;
let mut i = 0;
while i < expirations.size() {
let _ = expirations.object_at2(i, &strikes);
let mut j = 0;
while j < strikes.size() {
let _ = strikes.array_at2(j, &contracts);
let mut k = 0;
while k < contracts.size() {
let _ = contracts.object_at2(k, &contract);
println!("{} strike {}", contract.string_of("description").unwrap_or_default(), contract.string_of("strikePrice").unwrap_or_default());
println!("Contract symbol: [{}]", contract.string_of("symbol").unwrap_or_default());
found = found + 1;
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
if found == 0 {
println!("No contracts matched the requested expiration, side and strike.");
}