Sample code for 30+ languages & platforms
.NET Core C# 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 .NET Core C# Downloads

.NET Core C#
bool success = false;

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

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

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

Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.AuthToken = accessToken;

//  Supply the calendar date you want to check, for example a US holiday.
Chilkat.CkDateTime clock = new Chilkat.CkDateTime();
clock.SetFromCurrentSystemTime();
string marketDate = clock.GetAsIso8601("YYYY-MM-DD",false);
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
Chilkat.HttpRequest query = new Chilkat.HttpRequest();
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) {
        Debug.WriteLine("No HTTP response. Check connectivity.");
    }
    else {
        Debug.WriteLine("Schwab returned HTTP " + Convert.ToString(resp.StatusCode));
        if (resp.StatusCode == 401) {
            Debug.WriteLine("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

Chilkat.JsonObject reply = new Chilkat.JsonObject();
success = reply.Load(resp.BodyStr);
if (success != true) {
    Debug.WriteLine("Schwab returned unexpected JSON.");
    return;
}

Chilkat.JsonObject products = new Chilkat.JsonObject();
success = reply.ObjectOf2("equity",products);
if ((success != true) || (products.Size == 0)) {
    Debug.WriteLine("No equity market schedule returned for " + marketDate);
    return;
}

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

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

    }
    else {
        Debug.WriteLine("Closed for this date.");
    }

    i = i + 1;
}