Sample code for 30+ languages & platforms
React Native

ETrade v1 Get Account Balances

See more HTTP Misc Examples

Get account balances using the ETrade v1 API.

Chilkat React Native Downloads

React Native
import { Http, JsonObject, Xml } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

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

  // Load the access token previously obtained via the OAuth1 3-Legged Authorization examples Step1 and Step2.
  const json = new JsonObject();
  try {
    json.loadFile('qa_data/tokens/etrade.json');
  } catch {
    console.log('Failed to load OAuth1 token');
    return;
  }

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

  // See the ETrade v1 API documentation HERE.

  http.setUrlVar('accountIdKey', 'vsnhtF7d9jXxBy6HyaAC4vQ');
  http.setUrlVar('instType', 'BROKERAGE');
  let respStr: string;
  try {
    respStr = await http.quickGetStrAsync('https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true');
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  // A 200 status code indicates success.
  const statusCode = http.lastStatus;
  console.log(`statusCode = ${statusCode}`);

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

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

  const xml = new Xml();
  xml.loadXml(respStr);

  const tagPath = '';

  const accountId = xml.getChildIntValue('accountId');
  const accountType = xml.getChildContent('accountType');
  const optionLevel = xml.getChildContent('optionLevel');
  const accountDescription = xml.getChildContent('accountDescription');
  const quoteMode = xml.getChildIntValue('quoteMode');
  const dayTraderStatus = xml.getChildContent('dayTraderStatus');
  const accountMode = xml.getChildContent('accountMode');
  const fundsForOpenOrdersCash = xml.getChildIntValue('Cash|fundsForOpenOrdersCash');
  const moneyMktBalance = xml.getChildIntValue('Cash|moneyMktBalance');
  const cashAvailableForInvestment = xml.getChildIntValue('Computed|cashAvailableForInvestment');
  const netCash = xml.getChildContent('Computed|netCash');
  const cashBalance = xml.getChildContent('Computed|cashBalance');
  const settledCashForInvestment = xml.getChildIntValue('Computed|settledCashForInvestment');
  const unSettledCashForInvestment = xml.getChildIntValue('Computed|unSettledCashForInvestment');
  const fundsWithheldFromPurchasePower = xml.getChildIntValue('Computed|fundsWithheldFromPurchasePower');
  const fundsWithheldFromWithdrawal = xml.getChildIntValue('Computed|fundsWithheldFromWithdrawal');
  const marginBuyingPower = xml.getChildIntValue('Computed|marginBuyingPower');
  const cashBuyingPower = xml.getChildContent('Computed|cashBuyingPower');
  const dtMarginBuyingPower = xml.getChildIntValue('Computed|dtMarginBuyingPower');
  const dtCashBuyingPower = xml.getChildIntValue('Computed|dtCashBuyingPower');
  const shortAdjustBalance = xml.getChildIntValue('Computed|shortAdjustBalance');
  const regtEquity = xml.getChildIntValue('Computed|regtEquity');
  const regtEquityPercent = xml.getChildIntValue('Computed|regtEquityPercent');
  const accountBalance = xml.getChildIntValue('Computed|accountBalance');
  const dtCashOpenOrderReserve = xml.getChildIntValue('Margin|dtCashOpenOrderReserve');
  const dtMarginOpenOrderReserve = xml.getChildIntValue('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>
}