Java Requires Chilkat v11.0.0+
Java
Schwab: Preview a single-leg equity limit order
See more Schwab Examples
Builds a day limit order for one share and posts it to previewOrder. Reads the projected order and the validation messages, including warnings and rejection reasons. A successful HTTP response means the preview completed. A preview does not submit an order, reserve buying power, or guarantee later acceptance.
Use the access token saved by desktop authorization. First select the authorized account whose data you want to use. 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.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);
// Read the hash saved by the authorized-account selection example.
entry.updateString("service", "SelectedAccount");
JsonObject selection = new JsonObject();
try {
vault.getSecretJson(entry, selection);
success = true;
} catch (ChilkatException e) {
success = false;
}
String accountHash = selection.stringOf("hashValue");
if ((!success) || ("".equals(accountHash))) {
System.out.println("Select an authorized account first.");
return;
}
http.setUrlVar("account", accountHash);
// These are illustrative preview inputs. Set the symbol, quantity and limit price.
JsonObject order = new JsonObject();
// The PreviewOrder schema puts orderLegs inside orderStrategy.
order.updateString("orderStrategy.session", "NORMAL");
order.updateString("orderStrategy.duration", "DAY");
order.updateString("orderStrategy.orderType", "LIMIT");
order.updateString("orderStrategy.orderStrategyType", "SINGLE");
order.updateNumber("orderStrategy.price", "100.00");
order.updateInt("orderStrategy.quantity", 1);
order.updateString("orderStrategy.orderLegs[0].instruction", "BUY");
order.updateInt("orderStrategy.orderLegs[0].quantity", 1);
order.updateString("orderStrategy.orderLegs[0].finalSymbol", "AAPL");
order.updateString("orderStrategy.orderLegs[0].assetType", "EQUITY");
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", "https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder", order, "application/json", 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;
}
System.out.println("Preview only. No order was submitted.");
if (reply.hasMember("orderStrategy.orderType")) {
System.out.println("Projected order: " + reply.stringOf("orderStrategy.orderType") + " price " + reply.stringOf("orderStrategy.price"));
}
if (reply.hasMember("orderStrategy.orderBalance.projectedBuyingPower")) {
System.out.println("Projected buying power: " + reply.stringOf("orderStrategy.orderBalance.projectedBuyingPower"));
}
JsonObject validation = new JsonObject();
try {
reply.objectOf2("orderValidationResult", validation);
} catch (ChilkatException e) {
System.out.println("Schwab supplied no validation result.");
return;
}
// Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
JsonArray rules = new JsonArray();
JsonObject detail = new JsonObject();
int messages = 0;
int i = 0;
while (i < validation.getSize()) {
String category = validation.nameAt(i);
try {
validation.arrayAt2(i, rules);
success = true;
} catch (ChilkatException e) {
success = false;
}
if (success) {
int j = 0;
while (j < rules.getSize()) {
rules.objectAt2(j, detail);
System.out.println(category + " " + detail.stringOf("validationRuleName") + " " + detail.stringOf("message") + " " + detail.stringOf("activityMessage"));
messages = messages + 1;
j = j + 1;
}
}
i = i + 1;
}
if (messages == 0) {
System.out.println("No validation messages returned.");
}
}
}