Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
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 Unicode C++ Downloads
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.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);
CkHttpResponseW resp;
CkHttpRequestW query;
query.put_HttpVerb(L"GET");
query.AddParam(L"symbols",L"AAPL,MSFT,BRK/B");
query.AddParam(L"fields",L"quote");
success = http.HttpReq(L"https://api.schwabapi.com/marketdata/v1/quotes",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;
}
// Read members by index. A symbol may contain punctuation meaningful in a JSON path.
CkJsonObjectW instrument;
int i = 0;
while ((i < reply.get_Size())) {
const wchar_t *key = reply.nameAt(i);
if (key != L"errors") {
success = reply.ObjectAt2(i,instrument);
if (success == true) {
wprintf(L"%s bid %s ask %s last %s\n",key,instrument.stringOf(L"quote.bidPrice"),instrument.stringOf(L"quote.askPrice"),instrument.stringOf(L"quote.lastPrice"));
wprintf(L" quote time (Unix ms): %s real-time: %s\n",instrument.stringOf(L"quote.quoteTime"),instrument.stringOf(L"realtime"));
}
}
i = i + 1;
}
int j = 0;
while ((j < reply.SizeOfArray(L"errors.invalidSymbols"))) {
reply.put_J(j);
wprintf(L"Invalid symbol: %s\n",reply.stringOf(L"errors.invalidSymbols[j]"));
j = j + 1;
}
}