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

ETrade v1 Get Account Balances

See more HTTP Misc Examples

Get account balances using the ETrade v1 API.

Chilkat Unicode C++ Downloads

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

void ChilkatSample(void)
    {
    bool success = false;

    // This example 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 3-Legged Authorization examples Step1 and Step2.
    CkJsonObjectW json;
    success = json.LoadFile(L"qa_data/tokens/etrade.json");
    if (success != true) {
        wprintf(L"Failed to load OAuth1 token\n");
        return;
    }

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

    // See the ETrade v1 API documentation HERE.

    http.SetUrlVar(L"accountIdKey",L"vsnhtF7d9jXxBy6HyaAC4vQ");
    http.SetUrlVar(L"instType",L"BROKERAGE");
    const wchar_t *respStr = http.quickGetStr(L"https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true");
    if (http.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    // A 200 status code indicates success.
    int statusCode = http.get_LastStatus();
    wprintf(L"statusCode = %d\n",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...

    CkXmlW xml;
    xml.LoadXml(respStr);

    const wchar_t *tagPath = 0;
    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");

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