Sample code for 30+ languages & platforms
Java

ETrade Get Account Balances

See more ETrade Examples

Retrieves the current account balance and related details for a specified account.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // This requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        http.setOAuth1(true);
        http.setOAuthVerifier("");
        http.setOAuthConsumerKey("ETRADE_CONSUMER_KEY");
        http.setOAuthConsumerSecret("ETRADE_CONSUMER_SECRET");

        // Load the access token previously obtained via the OAuth1 Authorization
        JsonObject jsonToken = new JsonObject();
        try {
            jsonToken.loadFile("qa_data/tokens/etrade.json");
        } catch (ChilkatException e) {
            System.out.println("Failed to load OAuth1 token");
            return;
        }

        http.setOAuthToken(jsonToken.stringOf("oauth_token"));
        http.setOAuthTokenSecret(jsonToken.stringOf("oauth_token_secret"));

        String sandboxUrl = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true";
        String liveUrl = "https://api.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true";

        http.setUrlVar("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A");
        http.setUrlVar("instType", "BROKERAGE");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpNoBody("GET", sandboxUrl, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Make sure a successful response was received.
        if (resp.getStatusCode() > 200) {
            System.out.println(resp.getStatusLine());
            System.out.println(resp.getHeader());
            System.out.println(resp.getBodyStr());
            return;
        }

        // Sample XML response:

        // Use this online tool to generate parsing code from sample XML:
        // Generate Parsing Code from XML

        // <?xml version="1.0" encoding="UTF-8"?>
        // <BalanceResponse>
        //    <accountId>83564979</accountId>
        //    <accountType>PDT_ACCOUNT</accountType>
        //    <optionLevel>LEVEL_4</optionLevel>
        //    <accountDescription>KRITHH TT</accountDescription>
        //    <quoteMode>6</quoteMode>
        //    <dayTraderStatus>PDT_MIN_EQUITY_RES_1XK</dayTraderStatus>
        //    <accountMode>PDT ACCOUNT</accountMode>
        //    <Cash>
        //       <fundsForOpenOrdersCash>0</fundsForOpenOrdersCash>
        //       <moneyMktBalance>0</moneyMktBalance>
        //    </Cash>
        //    <Computed>
        //       <cashAvailableForInvestment>0</cashAvailableForInvestment>
        //       <netCash>93921.44</netCash>
        //       <cashBalance>93921.44</cashBalance>
        //       <settledCashForInvestment>0</settledCashForInvestment>
        //       <unSettledCashForInvestment>0</unSettledCashForInvestment>
        //       <fundsWithheldFromPurchasePower>0</fundsWithheldFromPurchasePower>
        //       <fundsWithheldFromWithdrawal>0</fundsWithheldFromWithdrawal>
        //       <marginBuyingPower>0</marginBuyingPower>
        //       <cashBuyingPower>93921.44</cashBuyingPower>
        //       <dtMarginBuyingPower>0</dtMarginBuyingPower>
        //       <dtCashBuyingPower>0</dtCashBuyingPower>
        //       <shortAdjustBalance>0</shortAdjustBalance>
        //       <regtEquity>0</regtEquity>
        //       <regtEquityPercent>0</regtEquityPercent>
        //       <accountBalance>0</accountBalance>
        //    </Computed>
        //    <Margin>
        //       <dtCashOpenOrderReserve>0</dtCashOpenOrderReserve>
        //       <dtMarginOpenOrderReserve>0</dtMarginOpenOrderReserve>
        //    </Margin>
        // </BalanceResponse>

        Xml xml = new Xml();
        xml.loadXml(resp.getBodyStr());
        System.out.println(xml.getXml());

        int accountId = xml.getChildIntValue("accountId");
        String accountType = xml.getChildContent("accountType");
        String optionLevel = xml.getChildContent("optionLevel");
        String accountDescription = xml.getChildContent("accountDescription");
        int quoteMode = xml.getChildIntValue("quoteMode");
        String dayTraderStatus = xml.getChildContent("dayTraderStatus");
        String accountMode = xml.getChildContent("accountMode");
        int fundsForOpenOrdersCash = xml.getChildIntValue("Cash|fundsForOpenOrdersCash");
        int moneyMktBalance = xml.getChildIntValue("Cash|moneyMktBalance");
        int cashAvailableForInvestment = xml.getChildIntValue("Computed|cashAvailableForInvestment");
        String netCash = xml.getChildContent("Computed|netCash");
        String cashBalance = xml.getChildContent("Computed|cashBalance");
        int settledCashForInvestment = xml.getChildIntValue("Computed|settledCashForInvestment");
        int unSettledCashForInvestment = xml.getChildIntValue("Computed|unSettledCashForInvestment");
        int fundsWithheldFromPurchasePower = xml.getChildIntValue("Computed|fundsWithheldFromPurchasePower");
        int fundsWithheldFromWithdrawal = xml.getChildIntValue("Computed|fundsWithheldFromWithdrawal");
        int marginBuyingPower = xml.getChildIntValue("Computed|marginBuyingPower");
        String cashBuyingPower = xml.getChildContent("Computed|cashBuyingPower");
        int dtMarginBuyingPower = xml.getChildIntValue("Computed|dtMarginBuyingPower");
        int dtCashBuyingPower = xml.getChildIntValue("Computed|dtCashBuyingPower");
        int shortAdjustBalance = xml.getChildIntValue("Computed|shortAdjustBalance");
        int regtEquity = xml.getChildIntValue("Computed|regtEquity");
        int regtEquityPercent = xml.getChildIntValue("Computed|regtEquityPercent");
        int accountBalance = xml.getChildIntValue("Computed|accountBalance");
        int dtCashOpenOrderReserve = xml.getChildIntValue("Margin|dtCashOpenOrderReserve");
        int dtMarginOpenOrderReserve = xml.getChildIntValue("Margin|dtMarginOpenOrderReserve");

        System.out.println("Success.");
    }
}