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

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
accessToken: PWideChar;
http: HCkHttp;
clock: HCkDateTime;
marketDate: PWideChar;
resp: HCkHttpResponse;
query: HCkHttpRequest;
reply: HCkJsonObject;
products: HCkJsonObject;
product: HCkJsonObject;
i: Integer;
j: Integer;
count: Integer;

begin
success := False;

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

vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','SchwabTrader');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','personal-desktop');
tokens := CkJsonObject_Create();
success := CkSecrets_GetSecretJson(vault,entry,tokens);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read the token record. Check the store and complete desktop authorization first.');
    Exit;
  end;
accessToken := CkJsonObject__stringOf(tokens,'access_token');
if (accessToken = '') then
  begin
    Memo1.Lines.Add('Run the Schwab authorization or refresh example to save an access token.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,accessToken);

//  Supply the calendar date you want to check, for example a US holiday.
clock := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(clock);
marketDate := CkDateTime__getAsIso8601(clock,'YYYY-MM-DD',False);
resp := CkHttpResponse_Create();
query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'date',marketDate);
success := CkHttp_HttpReq(http,'https://api.schwabapi.com/marketdata/v1/markets/equity',query,resp);
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    if (CkHttpResponse_getStatusCode(resp) = 0) then
      begin
        Memo1.Lines.Add('No HTTP response. Check connectivity.');
      end
    else
      begin
        Memo1.Lines.Add('Schwab returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
        if (CkHttpResponse_getStatusCode(resp) = 401) then
          begin
            Memo1.Lines.Add('Refresh the access token or repeat desktop authorization.');
          end;
      end;
    Exit;
  end;
reply := CkJsonObject_Create();
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Schwab returned unexpected JSON.');
    Exit;
  end;

products := CkJsonObject_Create();
success := CkJsonObject_ObjectOf2(reply,'equity',products);
if ((success <> True) or (CkJsonObject_getSize(products) = 0)) then
  begin
    Memo1.Lines.Add('No equity market schedule returned for ' + marketDate);
    Exit;
  end;
product := CkJsonObject_Create();
i := 0;
while (i < CkJsonObject_getSize(products)) do
  begin
    CkJsonObject_ObjectAt2(products,i,product);
    Memo1.Lines.Add(CkJsonObject__nameAt(products,i) + '  ' + CkJsonObject__stringOf(product,'date'));
    if (CkJsonObject_BoolOf(product,'isOpen') = True) then
      begin
        j := 0;
        count := CkJsonObject_SizeOfArray(product,'sessionHours.regularMarket');
        if (count <= 0) then
          begin
            Memo1.Lines.Add('Open date, but no regular-session times were supplied.');
          end;
        while (j < count) do
          begin
            CkJsonObject_putJ(product,j);
            Memo1.Lines.Add('Regular session: ' + CkJsonObject__stringOf(product,'sessionHours.regularMarket[j].start') + ' to ' + CkJsonObject__stringOf(product,'sessionHours.regularMarket[j].end'));
            j := j + 1;
          end;

      end
    else
      begin
        Memo1.Lines.Add('Closed for this date.');
      end;
    i := i + 1;
  end;

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkDateTime_Dispose(clock);
CkHttpResponse_Dispose(resp);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(products);
CkJsonObject_Dispose(product);