Sample code for 30+ languages & platforms
JavaScript 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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;

var vault = new CkSecrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
var entry = new CkJsonObject();
entry.UpdateString("appName","SchwabTrader");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","personal-desktop");
var tokens = new CkJsonObject();
success = vault.GetSecretJson(entry,tokens);
if (success !== true) {
    console.log("Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

var accessToken = tokens.StringOf("access_token");
if (accessToken == "") {
    console.log("Run the Schwab authorization or refresh example to save an access token.");
    return;
}

var http = new CkHttp();
http.RequireSslCertVerify = true;
http.AuthToken = accessToken;

//  Supply the calendar date you want to check, for example a US holiday.
var clock = new CkDateTime();
clock.SetFromCurrentSystemTime();
var marketDate = clock.GetAsIso8601("YYYY-MM-DD",false);
var resp = new CkHttpResponse();
var query = new CkHttpRequest();
query.HttpVerb = "GET";
query.AddParam("date",marketDate);
success = http.HttpReq("https://api.schwabapi.com/marketdata/v1/markets/equity",query,resp);
if ((success !== true) || (resp.StatusCode !== 200)) {
    if (resp.StatusCode == 0) {
        console.log("No HTTP response. Check connectivity.");
    }
    else {
        console.log("Schwab returned HTTP " + resp.StatusCode);
        if (resp.StatusCode == 401) {
            console.log("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

var reply = new CkJsonObject();
success = reply.Load(resp.BodyStr);
if (success !== true) {
    console.log("Schwab returned unexpected JSON.");
    return;
}

var products = new CkJsonObject();
success = reply.ObjectOf2("equity",products);
if ((success !== true) || (products.Size == 0)) {
    console.log("No equity market schedule returned for " + marketDate);
    return;
}

var product = new CkJsonObject();
var i = 0;
while ((i < products.Size)) {
    products.ObjectAt2(i,product);
    console.log(products.NameAt(i) + "  " + product.StringOf("date"));
    if (product.BoolOf("isOpen") == true) {
        var j = 0;
        var count = product.SizeOfArray("sessionHours.regularMarket");
        if (count <= 0) {
            console.log("Open date, but no regular-session times were supplied.");
        }

        while ((j < count)) {
            product.J = j;
            console.log("Regular session: " + product.StringOf("sessionHours.regularMarket[j].start") + " to " + product.StringOf("sessionHours.regularMarket[j].end"));
            j = j+1;
        }

    }
    else {
        console.log("Closed for this date.");
    }

    i = i+1;
}