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

Schwab: Check equity market hours for a date

See more Schwab Examples

Looks up the US equity calendar for a selected trading date. Reads each product’s open flag and the actual regular-session start and end, including their offsets. This distinguishes a holiday closure from a shortened session. The open flag describes the requested date, not whether the market is open at this instant.

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.

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

// Supply the calendar date you want to check, for example a US holiday.
let clock = chilkat::DateTime::new();
let _ = clock.set_from_current_system_time();
let market_date = clock.get_as_iso8601("YYYY-MM-DD", false).unwrap_or_default();
let resp = chilkat::HttpResponse::new();
let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("date", &market_date);
success = http.http_req("https://api.schwabapi.com/marketdata/v1/markets/equity", &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();
success = reply.load(&resp.body_str()).is_ok();
if !success {
    println!("Schwab returned unexpected JSON.");
    return;
}

let products = chilkat::JsonObject::new();
success = reply.object_of2("equity", &products).is_ok();
if (!success) || (products.size() == 0) {
    println!("No equity market schedule returned for {}", market_date);
    return;
}

let product = chilkat::JsonObject::new();
let mut i = 0;
while i < products.size() {
    let _ = products.object_at2(i, &product);
    println!("{}  {}", products.name_at(i).unwrap_or_default(), product.string_of("date").unwrap_or_default());
    if product.bool_of("isOpen") {
        let mut j = 0;
        let count = product.size_of_array("sessionHours.regularMarket");
        if count <= 0 {
            println!("Open date, but no regular-session times were supplied.");
        }

        while j < count {
            product.set_j(j);
            println!("Regular session: {} to {}", product.string_of("sessionHours.regularMarket[j].start").unwrap_or_default(), product.string_of("sessionHours.regularMarket[j].end").unwrap_or_default());
            j = j + 1;
        }

    } else {
        println!("Closed for this date.");
    }

    i = i + 1;
}