Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
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.
Chilkat Unicode C++ Downloads
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkDateTimeW.h>
#include <CkHttpResponseW.h>
#include <CkHttpRequestW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkSecretsW vault;
vault.put_Location(L"local_manager");
vault.put_AutoSplit(true);
CkJsonObjectW entry;
entry.UpdateString(L"appName",L"SchwabTrader");
entry.UpdateString(L"service",L"OAuth2");
entry.UpdateString(L"username",L"personal-desktop");
CkJsonObjectW tokens;
success = vault.GetSecretJson(entry,tokens);
if (success != true) {
wprintf(L"Could not read the token record. Check the store and complete desktop authorization first.\n");
return;
}
const wchar_t *accessToken = tokens.stringOf(L"access_token");
if (accessToken == L"") {
wprintf(L"Run the Schwab authorization or refresh example to save an access token.\n");
return;
}
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.put_AuthToken(accessToken);
// Supply the calendar date you want to check, for example a US holiday.
CkDateTimeW clock;
clock.SetFromCurrentSystemTime();
const wchar_t *marketDate = clock.getAsIso8601(L"YYYY-MM-DD",false);
CkHttpResponseW resp;
CkHttpRequestW query;
query.put_HttpVerb(L"GET");
query.AddParam(L"date",marketDate);
success = http.HttpReq(L"https://api.schwabapi.com/marketdata/v1/markets/equity",query,resp);
if ((success != true) || (resp.get_StatusCode() != 200)) {
if (resp.get_StatusCode() == 0) {
wprintf(L"No HTTP response. Check connectivity.\n");
}
else {
wprintf(L"Schwab returned HTTP %d\n",resp.get_StatusCode());
if (resp.get_StatusCode() == 401) {
wprintf(L"Refresh the access token or repeat desktop authorization.\n");
}
}
return;
}
CkJsonObjectW reply;
success = reply.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Schwab returned unexpected JSON.\n");
return;
}
CkJsonObjectW products;
success = reply.ObjectOf2(L"equity",products);
if ((success != true) || (products.get_Size() == 0)) {
wprintf(L"No equity market schedule returned for %s\n",marketDate);
return;
}
CkJsonObjectW product;
int i = 0;
while ((i < products.get_Size())) {
products.ObjectAt2(i,product);
wprintf(L"%s %s\n",products.nameAt(i),product.stringOf(L"date"));
if (product.BoolOf(L"isOpen") == true) {
int j = 0;
int count = product.SizeOfArray(L"sessionHours.regularMarket");
if (count <= 0) {
wprintf(L"Open date, but no regular-session times were supplied.\n");
}
while ((j < count)) {
product.put_J(j);
wprintf(L"Regular session: %s to %s\n",product.stringOf(L"sessionHours.regularMarket[j].start"),product.stringOf(L"sessionHours.regularMarket[j].end"));
j = j + 1;
}
}
else {
wprintf(L"Closed for this date.\n");
}
i = i + 1;
}
}