Java Requires Chilkat v11.0.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Secrets vault = new Secrets();
vault.setLocation("local_manager");
vault.setAutoSplit(true);
JsonObject entry = new JsonObject();
entry.updateString("appName", "SchwabTrader");
entry.updateString("service", "OAuth2");
entry.updateString("username", "personal-desktop");
JsonObject tokens = new JsonObject();
try {
vault.getSecretJson(entry, tokens);
} catch (ChilkatException e) {
System.out.println("Could not read the token record. Check the store and complete desktop authorization first.");
return;
}
String accessToken = tokens.stringOf("access_token");
if ("".equals(accessToken)) {
System.out.println("Run the Schwab authorization or refresh example to save an access token.");
return;
}
Http http = new Http();
http.setRequireSslCertVerify(true);
http.setAuthToken(accessToken);
HttpResponse resp = new HttpResponse();
HttpRequest query = new HttpRequest();
query.setHttpVerb("GET");
query.addParam("symbols", "AAPL,MSFT,BRK/B");
query.addParam("fields", "quote");
try {
http.httpReq("https://api.schwabapi.com/marketdata/v1/quotes", query, resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (resp.getStatusCode() != 200)) {
if (resp.getStatusCode() == 0) {
System.out.println("No HTTP response. Check connectivity.");
} else {
System.out.println("Schwab returned HTTP " + resp.getStatusCode());
if (resp.getStatusCode() == 401) {
System.out.println("Refresh the access token or repeat desktop authorization.");
}
}
return;
}
JsonObject reply = new JsonObject();
try {
reply.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Schwab returned unexpected JSON.");
return;
}
// Read members by index. A symbol may contain punctuation meaningful in a JSON path.
JsonObject instrument = new JsonObject();
int i = 0;
while (i < reply.getSize()) {
String key = reply.nameAt(i);
if (!"errors".equals(key)) {
try {
reply.objectAt2(i, instrument);
success = true;
} catch (ChilkatException e) {
success = false;
}
if (success) {
System.out.println(key + " bid " + instrument.stringOf("quote.bidPrice") + " ask " + instrument.stringOf("quote.askPrice") + " last " + instrument.stringOf("quote.lastPrice"));
System.out.println(" quote time (Unix ms): " + instrument.stringOf("quote.quoteTime") + " real-time: " + instrument.stringOf("realtime"));
}
}
i = i + 1;
}
int j = 0;
while (j < reply.sizeOfArray("errors.invalidSymbols")) {
reply.setJ(j);
System.out.println("Invalid symbol: " + reply.stringOf("errors.invalidSymbols[j]"));
j = j + 1;
}
}
}