Rust Requires Chilkat v11.0.0+
Rust
Schwab: Export daily OHLCV price history
See more Schwab Examples
Downloads one year of daily open, high, low, close, and volume for one equity. The CSV records each candle’s UTC timestamp and original Unix milliseconds. Volume and prices retain their JSON number text. An empty series leaves any previous export in place.
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("symbol", "AAPL");
query.add_param("periodType", "year");
query.add_param("period", "1");
query.add_param("frequencyType", "daily");
query.add_param("frequency", "1");
query.add_param("needExtendedHoursData", "false");
success = http.http_req("https://api.schwabapi.com/marketdata/v1/pricehistory", &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 count = reply.size_of_array("candles");
if count <= 0 {
println!("No candles for this symbol and period.");
return;
}
let csv = chilkat::Csv::new();
let _ = csv.set_column_name(0, "utcTime");
let _ = csv.set_column_name(1, "unixMilliseconds");
let _ = csv.set_column_name(2, "open");
let _ = csv.set_column_name(3, "high");
let _ = csv.set_column_name(4, "low");
let _ = csv.set_column_name(5, "close");
let _ = csv.set_column_name(6, "volume");
let millis = chilkat::StringBuilder::new();
let unix_time = chilkat::JsonObject::new();
let candle_time = chilkat::DateTime::new();
let mut i = 0;
while i < count {
reply.set_i(i);
let timestamp = reply.string_of("candles[i].datetime").unwrap_or_default();
// DateOf reads Unix seconds. Drop the three millisecond digits as text.
let _ = millis.set_string(×tamp);
let digits = millis.length() - 3;
if digits <= 0 {
println!("Missing candle timestamp.");
return;
}
let _ = unix_time.update_number("seconds", &millis.get_range(0, digits, false).unwrap_or_default());
if unix_time.date_of("seconds", &candle_time).is_err() {
println!("Invalid candle timestamp.");
return;
}
let _ = csv.set_cell(i, 0, &candle_time.get_as_timestamp(false).unwrap_or_default());
let _ = csv.set_cell(i, 1, ×tamp);
let _ = csv.set_cell(i, 2, &reply.string_of("candles[i].open").unwrap_or_default());
let _ = csv.set_cell(i, 3, &reply.string_of("candles[i].high").unwrap_or_default());
let _ = csv.set_cell(i, 4, &reply.string_of("candles[i].low").unwrap_or_default());
let _ = csv.set_cell(i, 5, &reply.string_of("candles[i].close").unwrap_or_default());
let _ = csv.set_cell(i, 6, &reply.string_of("candles[i].volume").unwrap_or_default());
i = i + 1;
}
if csv.save_file2("schwab-daily-prices.csv", "utf-8").is_err() {
println!("Could not write schwab-daily-prices.csv.");
return;
}
println!("Wrote {} rows to schwab-daily-prices.csv.", csv.num_rows());