Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.CkDateTime,
Chilkat.HttpResponse,
Chilkat.HttpRequest,
Chilkat.JsonObject,
Chilkat.Secrets,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
vault: TSecrets;
entry: TJsonObject;
tokens: TJsonObject;
accessToken: string;
http: THttp;
clock: TCkDateTime;
marketDate: string;
resp: THttpResponse;
query: THttpRequest;
reply: TJsonObject;
products: TJsonObject;
product: TJsonObject;
i: Integer;
j: Integer;
count: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
vault := TSecrets.Create;
vault.Location := 'local_manager';
vault.AutoSplit := True;
entry := TJsonObject.Create;
entry.UpdateString('appName','SchwabTrader');
entry.UpdateString('service','OAuth2');
entry.UpdateString('username','personal-desktop');
tokens := TJsonObject.Create;
success := vault.GetSecretJson(entry,tokens);
if (success <> True) then
begin
WriteLn('Could not read the token record. Check the store and complete desktop authorization first.');
Exit;
end;
accessToken := tokens.StringOf('access_token');
if (accessToken = '') then
begin
WriteLn('Run the Schwab authorization or refresh example to save an access token.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := accessToken;
// Supply the calendar date you want to check, for example a US holiday.
clock := TCkDateTime.Create;
clock.SetFromCurrentSystemTime();
marketDate := clock.GetAsIso8601('YYYY-MM-DD',False);
resp := THttpResponse.Create;
query := THttpRequest.Create;
query.HttpVerb := 'GET';
query.AddParam('date',marketDate);
success := http.HttpReq('https://api.schwabapi.com/marketdata/v1/markets/equity',query,resp);
if ((success <> True) or (resp.StatusCode <> 200)) then
begin
if (resp.StatusCode = 0) then
begin
WriteLn('No HTTP response. Check connectivity.');
end
else
begin
WriteLn('Schwab returned HTTP ' + resp.StatusCode);
if (resp.StatusCode = 401) then
begin
WriteLn('Refresh the access token or repeat desktop authorization.');
end;
end;
Exit;
end;
reply := TJsonObject.Create;
success := reply.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Schwab returned unexpected JSON.');
Exit;
end;
products := TJsonObject.Create;
success := reply.ObjectOf2('equity',products);
if ((success <> True) or (products.Size = 0)) then
begin
WriteLn('No equity market schedule returned for ' + marketDate);
Exit;
end;
product := TJsonObject.Create;
i := 0;
while (i < products.Size) do
begin
products.ObjectAt2(i,product);
WriteLn(products.NameAt(i) + ' ' + product.StringOf('date'));
if (product.BoolOf('isOpen') = True) then
begin
j := 0;
count := product.SizeOfArray('sessionHours.regularMarket');
if (count <= 0) then
begin
WriteLn('Open date, but no regular-session times were supplied.');
end;
while (j < count) do
begin
product.J := j;
WriteLn('Regular session: ' + product.StringOf('sessionHours.regularMarket[j].start') + ' to ' + product.StringOf('sessionHours.regularMarket[j].end'));
j := j + 1;
end;
end
else
begin
WriteLn('Closed for this date.');
end;
i := i + 1;
end;
vault.Free;
entry.Free;
tokens.Free;
http.Free;
clock.Free;
resp.Free;
query.Free;
reply.Free;
products.Free;
product.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.