Java Requires Chilkat v11.0.0+
Java
Schwab: Read account balances and positions
See more Schwab Examples
Requests fields=positions for the selected account. Reads current cash and liquidation value, prints buying power when supplied, and reports each holding’s symbol, long and short quantities, and market value. A cash-only portfolio can have balances and zero positions.
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.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);
// 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);
HttpResponse resp = new HttpResponse();
HttpRequest query = new HttpRequest();
query.setHttpVerb("GET");
query.addParam("fields", "positions");
try {
http.httpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}", 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 balances = new JsonObject();
try {
reply.objectOf2("securitiesAccount.currentBalances", balances);
} catch (ChilkatException e) {
System.out.println("The account response contained no current balances.");
return;
}
System.out.println("Cash balance: " + balances.stringOf("cashBalance"));
System.out.println("Liquidation value: " + balances.stringOf("liquidationValue"));
if (balances.hasMember("buyingPower")) {
System.out.println("Buying power: " + balances.stringOf("buyingPower"));
}
int count = reply.sizeOfArray("securitiesAccount.positions");
if (count <= 0) {
System.out.println("No positions.");
}
int i = 0;
while (i < count) {
reply.setI(i);
System.out.println(reply.stringOf("securitiesAccount.positions[i].instrument.symbol") + " long " + reply.stringOf("securitiesAccount.positions[i].longQuantity") + " short " + reply.stringOf("securitiesAccount.positions[i].shortQuantity") + " market value " + reply.stringOf("securitiesAccount.positions[i].marketValue"));
i = i + 1;
}
}
}