Sample code for 30+ languages & platforms
Unicode C++

ETrade Get Account Balances

See more ETrade Examples

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

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkXmlW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkHttpW http;

    http.put_OAuth1(true);
    http.put_OAuthVerifier(L"");
    http.put_OAuthConsumerKey(L"ETRADE_CONSUMER_KEY");
    http.put_OAuthConsumerSecret(L"ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 Authorization
    CkJsonObjectW jsonToken;
    success = jsonToken.LoadFile(L"qa_data/tokens/etrade.json");
    if (success != true) {
        wprintf(L"Failed to load OAuth1 token\n");
        return;
    }

    http.put_OAuthToken(jsonToken.stringOf(L"oauth_token"));
    http.put_OAuthTokenSecret(jsonToken.stringOf(L"oauth_token_secret"));

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

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

    CkHttpResponseW resp;
    success = http.HttpNoBody(L"GET",sandboxUrl,resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    // Make sure a successful response was received.
    if (resp.get_StatusCode() > 200) {
        wprintf(L"%s\n",resp.statusLine());
        wprintf(L"%s\n",resp.header());
        wprintf(L"%s\n",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>

    CkXmlW xml;
    xml.LoadXml(resp.bodyStr());
    wprintf(L"%s\n",xml.getXml());

    int accountId;
    const wchar_t *accountType = 0;
    const wchar_t *optionLevel = 0;
    const wchar_t *accountDescription = 0;
    int quoteMode;
    const wchar_t *dayTraderStatus = 0;
    const wchar_t *accountMode = 0;
    int fundsForOpenOrdersCash;
    int moneyMktBalance;
    int cashAvailableForInvestment;
    const wchar_t *netCash = 0;
    const wchar_t *cashBalance = 0;
    int settledCashForInvestment;
    int unSettledCashForInvestment;
    int fundsWithheldFromPurchasePower;
    int fundsWithheldFromWithdrawal;
    int marginBuyingPower;
    const wchar_t *cashBuyingPower = 0;
    int dtMarginBuyingPower;
    int dtCashBuyingPower;
    int shortAdjustBalance;
    int regtEquity;
    int regtEquityPercent;
    int accountBalance;
    int dtCashOpenOrderReserve;
    int dtMarginOpenOrderReserve;

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

    wprintf(L"Success.\n");
    }