Java Requires Chilkat v11.0.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
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);
// Supply the calendar date you want to check, for example a US holiday.
DateTime clock = new DateTime();
clock.setFromCurrentSystemTime();
String marketDate = clock.getAsIso8601("YYYY-MM-DD", false);
HttpResponse resp = new HttpResponse();
HttpRequest query = new HttpRequest();
query.setHttpVerb("GET");
query.addParam("date", marketDate);
try {
http.httpReq("https://api.schwabapi.com/marketdata/v1/markets/equity", 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;
}
JsonObject products = new JsonObject();
try {
reply.objectOf2("equity", products);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (products.getSize() == 0)) {
System.out.println("No equity market schedule returned for " + marketDate);
return;
}
JsonObject product = new JsonObject();
int i = 0;
while (i < products.getSize()) {
products.objectAt2(i, product);
System.out.println(products.nameAt(i) + " " + product.stringOf("date"));
if (product.boolOf("isOpen")) {
int j = 0;
int count = product.sizeOfArray("sessionHours.regularMarket");
if (count <= 0) {
System.out.println("Open date, but no regular-session times were supplied.");
}
while (j < count) {
product.setJ(j);
System.out.println("Regular session: " + product.stringOf("sessionHours.regularMarket[j].start") + " to " + product.stringOf("sessionHours.regularMarket[j].end"));
j = j + 1;
}
} else {
System.out.println("Closed for this date.");
}
i = i + 1;
}
}
}