Java Requires Chilkat v11.0.0+
Java
Schwab: Find an option contract by expiration and strike
See more Schwab Examples
Filters a chain by underlying, call or put, exact expiration, and strike. Choose a currently listed expiration and strike. Schwab groups contracts by expiration and strike and may return multiple deliverables. Compare the descriptions and use the exact returned contract symbol, including its spaces, for a later quote request.
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.JsonArray;
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);
String underlying = "AAPL";
String expiration = "2027-01-15";
String strike = "250";
String side = "CALL";
HttpResponse resp = new HttpResponse();
HttpRequest query = new HttpRequest();
query.setHttpVerb("GET");
query.addParam("symbol", underlying);
query.addParam("contractType", side);
query.addParam("strike", strike);
query.addParam("fromDate", expiration);
query.addParam("toDate", expiration);
try {
http.httpReq("https://api.schwabapi.com/marketdata/v1/chains", 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;
}
String mapName = "callExpDateMap";
if ("PUT".equals(side)) {
mapName = "putExpDateMap";
}
JsonObject expirations = new JsonObject();
try {
reply.objectOf2(mapName, expirations);
} catch (ChilkatException e) {
System.out.println("No option map returned. Chain status: " + reply.stringOf("status"));
return;
}
// Expiration keys contain a day count. Strike keys may contain a decimal point.
JsonObject strikes = new JsonObject();
JsonArray contracts = new JsonArray();
JsonObject contract = new JsonObject();
int found = 0;
int i = 0;
while (i < expirations.getSize()) {
expirations.objectAt2(i, strikes);
int j = 0;
while (j < strikes.getSize()) {
strikes.arrayAt2(j, contracts);
int k = 0;
while (k < contracts.getSize()) {
contracts.objectAt2(k, contract);
System.out.println(contract.stringOf("description") + " strike " + contract.stringOf("strikePrice"));
System.out.println("Contract symbol: [" + contract.stringOf("symbol") + "]");
found = found + 1;
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
if (found == 0) {
System.out.println("No contracts matched the requested expiration, side and strike.");
}
}
}