Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Schwab: Get quotes for a watchlist
See more Schwab Examples
Requests several symbols in one call and prints the bid, ask, last price, quote timestamp in Unix milliseconds, and the real-time indicator when available. Missing quote fields can occur for individual instruments. The response uses symbol names as JSON keys and can report invalid symbols alongside valid quotes.
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.Http,
Chilkat.Secrets,
Chilkat.HttpRequest,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
vault: TSecrets;
entry: TJsonObject;
tokens: TJsonObject;
accessToken: string;
http: THttp;
resp: THttpResponse;
query: THttpRequest;
reply: TJsonObject;
instrument: TJsonObject;
i: Integer;
key: string;
j: 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;
resp := THttpResponse.Create;
query := THttpRequest.Create;
query.HttpVerb := 'GET';
query.AddParam('symbols','AAPL,MSFT,BRK/B');
query.AddParam('fields','quote');
success := http.HttpReq('https://api.schwabapi.com/marketdata/v1/quotes',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;
// Read members by index. A symbol may contain punctuation meaningful in a JSON path.
instrument := TJsonObject.Create;
i := 0;
while (i < reply.Size) do
begin
key := reply.NameAt(i);
if (key <> 'errors') then
begin
success := reply.ObjectAt2(i,instrument);
if (success = True) then
begin
WriteLn(key + ' bid ' + instrument.StringOf('quote.bidPrice') + ' ask ' + instrument.StringOf('quote.askPrice') + ' last ' + instrument.StringOf('quote.lastPrice'));
WriteLn(' quote time (Unix ms): ' + instrument.StringOf('quote.quoteTime') + ' real-time: ' + instrument.StringOf('realtime'));
end;
end;
i := i + 1;
end;
j := 0;
while (j < reply.SizeOfArray('errors.invalidSymbols')) do
begin
reply.J := j;
WriteLn('Invalid symbol: ' + reply.StringOf('errors.invalidSymbols[j]'));
j := j + 1;
end;
vault.Free;
entry.Free;
tokens.Free;
http.Free;
resp.Free;
query.Free;
reply.Free;
instrument.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.