Sample code for 30+ languages & platforms
Dart

ETrade Get Account Balances

See more ETrade Examples

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

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

  http.oAuth1 = true;
  http.oAuthVerifier = '';
  http.oAuthConsumerKey = 'ETRADE_CONSUMER_KEY';
  http.oAuthConsumerSecret = 'ETRADE_CONSUMER_SECRET';

  // Load the access token previously obtained via the OAuth1 Authorization
  final jsonToken = CkJsonObject();
  try {
    jsonToken.loadFile('qa_data/tokens/etrade.json');
  } on ChilkatException {
    print('Failed to load OAuth1 token');
    return;
  }

  http.oAuthToken = jsonToken.stringOf('oauth_token');
  http.oAuthTokenSecret = jsonToken.stringOf('oauth_token_secret');

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

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

  final resp = CkHttpResponse();
  try {
    http.httpNoBody('GET', sandboxUrl, resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Make sure a successful response was received.
  if (resp.statusCode > 200) {
    print(resp.statusLine);
    print(resp.header);
    print(resp.bodyStr);
    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>

  final xml = CkXml();
  xml.loadXml(resp.bodyStr);
  print(xml.getXml());

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

  print('Success.');
}