Sample code for 30+ languages & platforms
Rust

ETrade v1 Get Account Balances

See more HTTP Misc Examples

Get account balances using the ETrade v1 API.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

http.set_o_auth1(true);
http.set_o_auth_verifier("");
http.set_o_auth_consumer_key("ETRADE_CONSUMER_KEY");
http.set_o_auth_consumer_secret("ETRADE_CONSUMER_SECRET");

// Load the access token previously obtained via the OAuth1 3-Legged Authorization examples Step1 and Step2.
let json = chilkat::JsonObject::new();
if json.load_file("qa_data/tokens/etrade.json").is_err() {
    println!("Failed to load OAuth1 token");
    return;
}

http.set_o_auth_token(&json.string_of("oauth_token").unwrap_or_default());
http.set_o_auth_token_secret(&json.string_of("oauth_token_secret").unwrap_or_default());

// See the ETrade v1 API documentation HERE.

let _ = http.set_url_var("accountIdKey", "vsnhtF7d9jXxBy6HyaAC4vQ");
let _ = http.set_url_var("instType", "BROKERAGE");
let Ok(resp_str) = http.quick_get_str("https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true") else {
    println!("{}", http.last_error_text());
    return;
};

// A 200 status code indicates success.
let status_code = http.last_status();
println!("statusCode = {}", status_code);

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

// A sample XML response is shown below...

let xml = chilkat::Xml::new();
let _ = xml.load_xml(&resp_str);

let account_id = xml.get_child_int_value("accountId");
let account_type = xml.get_child_content("accountType").unwrap_or_default();
let option_level = xml.get_child_content("optionLevel").unwrap_or_default();
let account_description = xml.get_child_content("accountDescription").unwrap_or_default();
let quote_mode = xml.get_child_int_value("quoteMode");
let day_trader_status = xml.get_child_content("dayTraderStatus").unwrap_or_default();
let account_mode = xml.get_child_content("accountMode").unwrap_or_default();
let funds_for_open_orders_cash = xml.get_child_int_value("Cash|fundsForOpenOrdersCash");
let money_mkt_balance = xml.get_child_int_value("Cash|moneyMktBalance");
let cash_available_for_investment = xml.get_child_int_value("Computed|cashAvailableForInvestment");
let net_cash = xml.get_child_content("Computed|netCash").unwrap_or_default();
let cash_balance = xml.get_child_content("Computed|cashBalance").unwrap_or_default();
let settled_cash_for_investment = xml.get_child_int_value("Computed|settledCashForInvestment");
let un_settled_cash_for_investment = xml.get_child_int_value("Computed|unSettledCashForInvestment");
let funds_withheld_from_purchase_power = xml.get_child_int_value("Computed|fundsWithheldFromPurchasePower");
let funds_withheld_from_withdrawal = xml.get_child_int_value("Computed|fundsWithheldFromWithdrawal");
let margin_buying_power = xml.get_child_int_value("Computed|marginBuyingPower");
let cash_buying_power = xml.get_child_content("Computed|cashBuyingPower").unwrap_or_default();
let dt_margin_buying_power = xml.get_child_int_value("Computed|dtMarginBuyingPower");
let dt_cash_buying_power = xml.get_child_int_value("Computed|dtCashBuyingPower");
let short_adjust_balance = xml.get_child_int_value("Computed|shortAdjustBalance");
let regt_equity = xml.get_child_int_value("Computed|regtEquity");
let regt_equity_percent = xml.get_child_int_value("Computed|regtEquityPercent");
let account_balance = xml.get_child_int_value("Computed|accountBalance");
let dt_cash_open_order_reserve = xml.get_child_int_value("Margin|dtCashOpenOrderReserve");
let dt_margin_open_order_reserve = xml.get_child_int_value("Margin|dtMarginOpenOrderReserve");

// <?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>