Sample code for 30+ languages & platforms
Xbase++

ETrade Get Account Balances

See more ETrade Examples

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

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oJsonToken
LOCAL cSandboxUrl
LOCAL cLiveUrl
LOCAL oResp
LOCAL oXml
LOCAL nAccountId
LOCAL cAccountType
LOCAL cOptionLevel
LOCAL cAccountDescription
LOCAL nQuoteMode
LOCAL cDayTraderStatus
LOCAL cAccountMode
LOCAL nFundsForOpenOrdersCash
LOCAL nMoneyMktBalance
LOCAL nCashAvailableForInvestment
LOCAL cNetCash
LOCAL cCashBalance
LOCAL nSettledCashForInvestment
LOCAL nUnSettledCashForInvestment
LOCAL nFundsWithheldFromPurchasePower
LOCAL nFundsWithheldFromWithdrawal
LOCAL nMarginBuyingPower
LOCAL cCashBuyingPower
LOCAL nDtMarginBuyingPower
LOCAL nDtCashBuyingPower
LOCAL nShortAdjustBalance
LOCAL nRegtEquity
LOCAL nRegtEquityPercent
LOCAL nAccountBalance
LOCAL nDtCashOpenOrderReserve
LOCAL nDtMarginOpenOrderReserve

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

oHttp:OAuth1 := 1
oHttp:OAuthVerifier := ""
oHttp:OAuthConsumerKey := "ETRADE_CONSUMER_KEY"
oHttp:OAuthConsumerSecret := "ETRADE_CONSUMER_SECRET"

//  Load the access token previously obtained via the OAuth1 Authorization
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/etrade.json")
IF (nSuccess != 1)
    ? "Failed to load OAuth1 token"
    oHttp:destroy()
    oJsonToken:destroy()
    RETURN
ENDIF

oHttp:OAuthToken := oJsonToken:StringOf("oauth_token")
oHttp:OAuthTokenSecret := oJsonToken:StringOf("oauth_token_secret")

cSandboxUrl := "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true"
cLiveUrl := "https://api.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true"

oHttp:SetUrlVar("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A")
oHttp:SetUrlVar("instType", "BROKERAGE")

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", cSandboxUrl, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJsonToken:destroy()
    oResp:destroy()
    RETURN
ENDIF

//  Make sure a successful response was received.
IF (oResp:StatusCode > 200)
    ? oResp:StatusLine
    ? oResp:Header
    ? oResp:BodyStr
    oHttp:destroy()
    oJsonToken:destroy()
    oResp:destroy()
    RETURN
ENDIF

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

oXml := CreateObject("Chilkat.Xml")
oXml:LoadXml(oResp:BodyStr)
? oXml:GetXml()

nAccountId := oXml:GetChildIntValue("accountId")
cAccountType := oXml:GetChildContent("accountType")
cOptionLevel := oXml:GetChildContent("optionLevel")
cAccountDescription := oXml:GetChildContent("accountDescription")
nQuoteMode := oXml:GetChildIntValue("quoteMode")
cDayTraderStatus := oXml:GetChildContent("dayTraderStatus")
cAccountMode := oXml:GetChildContent("accountMode")
nFundsForOpenOrdersCash := oXml:GetChildIntValue("Cash|fundsForOpenOrdersCash")
nMoneyMktBalance := oXml:GetChildIntValue("Cash|moneyMktBalance")
nCashAvailableForInvestment := oXml:GetChildIntValue("Computed|cashAvailableForInvestment")
cNetCash := oXml:GetChildContent("Computed|netCash")
cCashBalance := oXml:GetChildContent("Computed|cashBalance")
nSettledCashForInvestment := oXml:GetChildIntValue("Computed|settledCashForInvestment")
nUnSettledCashForInvestment := oXml:GetChildIntValue("Computed|unSettledCashForInvestment")
nFundsWithheldFromPurchasePower := oXml:GetChildIntValue("Computed|fundsWithheldFromPurchasePower")
nFundsWithheldFromWithdrawal := oXml:GetChildIntValue("Computed|fundsWithheldFromWithdrawal")
nMarginBuyingPower := oXml:GetChildIntValue("Computed|marginBuyingPower")
cCashBuyingPower := oXml:GetChildContent("Computed|cashBuyingPower")
nDtMarginBuyingPower := oXml:GetChildIntValue("Computed|dtMarginBuyingPower")
nDtCashBuyingPower := oXml:GetChildIntValue("Computed|dtCashBuyingPower")
nShortAdjustBalance := oXml:GetChildIntValue("Computed|shortAdjustBalance")
nRegtEquity := oXml:GetChildIntValue("Computed|regtEquity")
nRegtEquityPercent := oXml:GetChildIntValue("Computed|regtEquityPercent")
nAccountBalance := oXml:GetChildIntValue("Computed|accountBalance")
nDtCashOpenOrderReserve := oXml:GetChildIntValue("Margin|dtCashOpenOrderReserve")
nDtMarginOpenOrderReserve := oXml:GetChildIntValue("Margin|dtMarginOpenOrderReserve")

? "Success."

oHttp:destroy()
oJsonToken:destroy()
oResp:destroy()
oXml:destroy()