Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
accessToken: PWideChar;
http: HCkHttp;
resp: HCkHttpResponse;
query: HCkHttpRequest;
reply: HCkJsonObject;
instrument: HCkJsonObject;
i: Integer;
key: PWideChar;
j: 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);
resp := CkHttpResponse_Create();
query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'symbols','AAPL,MSFT,BRK/B');
CkHttpRequest_AddParam(query,'fields','quote');
success := CkHttp_HttpReq(http,'https://api.schwabapi.com/marketdata/v1/quotes',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;
// Read members by index. A symbol may contain punctuation meaningful in a JSON path.
instrument := CkJsonObject_Create();
i := 0;
while (i < CkJsonObject_getSize(reply)) do
begin
key := CkJsonObject__nameAt(reply,i);
if (key <> 'errors') then
begin
success := CkJsonObject_ObjectAt2(reply,i,instrument);
if (success = True) then
begin
Memo1.Lines.Add(key + ' bid ' + CkJsonObject__stringOf(instrument,'quote.bidPrice') + ' ask ' + CkJsonObject__stringOf(instrument,'quote.askPrice')
+ ' last ' + CkJsonObject__stringOf(instrument,'quote.lastPrice'));
Memo1.Lines.Add(' quote time (Unix ms): ' + CkJsonObject__stringOf(instrument,'quote.quoteTime') + ' real-time: ' + CkJsonObject__stringOf(instrument,'realtime'));
end;
end;
i := i + 1;
end;
j := 0;
while (j < CkJsonObject_SizeOfArray(reply,'errors.invalidSymbols')) do
begin
CkJsonObject_putJ(reply,j);
Memo1.Lines.Add('Invalid symbol: ' + CkJsonObject__stringOf(reply,'errors.invalidSymbols[j]'));
j := j + 1;
end;
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(instrument);