Rust
Rust
ETrade Get Account Balances
See more ETrade Examples
Retrieves the current account balance and related details for a specified account.Chilkat Rust Downloads
// This 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 Authorization
let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/etrade.json").is_err() {
println!("Failed to load OAuth1 token");
return;
}
http.set_o_auth_token(&json_token.string_of("oauth_token").unwrap_or_default());
http.set_o_auth_token_secret(&json_token.string_of("oauth_token_secret").unwrap_or_default());
let sandbox_url = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true".to_string();
let live_url = "https://api.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true".to_string();
let _ = http.set_url_var("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A");
let _ = http.set_url_var("instType", "BROKERAGE");
let resp = chilkat::HttpResponse::new();
if http.http_no_body("GET", &sandbox_url, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
// Make sure a successful response was received.
if resp.status_code() > 200 {
println!("{}", resp.status_line());
println!("{}", resp.header());
println!("{}", resp.body_str());
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>
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&resp.body_str());
println!("{}", xml.get_xml().unwrap_or_default());
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");
println!("Success.");