Sample code for 30+ languages & platforms
AutoIt

ETrade Get Account Balances

See more ETrade Examples

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

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oHttp = ObjCreate("Chilkat.Http")

$oHttp.OAuth1 = True
$oHttp.OAuthVerifier = ""
$oHttp.OAuthConsumerKey = "ETRADE_CONSUMER_KEY"
$oHttp.OAuthConsumerSecret = "ETRADE_CONSUMER_SECRET"

;  Load the access token previously obtained via the OAuth1 Authorization
$oJsonToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJsonToken.LoadFile("qa_data/tokens/etrade.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Failed to load OAuth1 token" & @CRLF)
    Exit
EndIf

$oHttp.OAuthToken = $oJsonToken.StringOf("oauth_token")
$oHttp.OAuthTokenSecret = $oJsonToken.StringOf("oauth_token_secret")

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

$oHttp.SetUrlVar("accountIdKey","6_Dpy0rmuQ9cu9IbTfvF2A")
$oHttp.SetUrlVar("instType","BROKERAGE")

$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpNoBody("GET",$sandboxUrl,$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

;  Make sure a successful response was received.
If ($oResp.StatusCode > 200) Then
    ConsoleWrite($oResp.StatusLine & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite($oResp.BodyStr & @CRLF)
    Exit
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 = ObjCreate("Chilkat.Xml")
$oXml.LoadXml($oResp.BodyStr)
ConsoleWrite($oXml.GetXml() & @CRLF)

Local $iAccountId
Local $sAccountType
Local $sOptionLevel
Local $sAccountDescription
Local $iQuoteMode
Local $sDayTraderStatus
Local $sAccountMode
Local $iFundsForOpenOrdersCash
Local $iMoneyMktBalance
Local $iCashAvailableForInvestment
Local $sNetCash
Local $sCashBalance
Local $iSettledCashForInvestment
Local $iUnSettledCashForInvestment
Local $iFundsWithheldFromPurchasePower
Local $iFundsWithheldFromWithdrawal
Local $iMarginBuyingPower
Local $sCashBuyingPower
Local $iDtMarginBuyingPower
Local $iDtCashBuyingPower
Local $iShortAdjustBalance
Local $iRegtEquity
Local $iRegtEquityPercent
Local $iAccountBalance
Local $iDtCashOpenOrderReserve
Local $iDtMarginOpenOrderReserve

$iAccountId = $oXml.GetChildIntValue("accountId")
$sAccountType = $oXml.GetChildContent("accountType")
$sOptionLevel = $oXml.GetChildContent("optionLevel")
$sAccountDescription = $oXml.GetChildContent("accountDescription")
$iQuoteMode = $oXml.GetChildIntValue("quoteMode")
$sDayTraderStatus = $oXml.GetChildContent("dayTraderStatus")
$sAccountMode = $oXml.GetChildContent("accountMode")
$iFundsForOpenOrdersCash = $oXml.GetChildIntValue("Cash|fundsForOpenOrdersCash")
$iMoneyMktBalance = $oXml.GetChildIntValue("Cash|moneyMktBalance")
$iCashAvailableForInvestment = $oXml.GetChildIntValue("Computed|cashAvailableForInvestment")
$sNetCash = $oXml.GetChildContent("Computed|netCash")
$sCashBalance = $oXml.GetChildContent("Computed|cashBalance")
$iSettledCashForInvestment = $oXml.GetChildIntValue("Computed|settledCashForInvestment")
$iUnSettledCashForInvestment = $oXml.GetChildIntValue("Computed|unSettledCashForInvestment")
$iFundsWithheldFromPurchasePower = $oXml.GetChildIntValue("Computed|fundsWithheldFromPurchasePower")
$iFundsWithheldFromWithdrawal = $oXml.GetChildIntValue("Computed|fundsWithheldFromWithdrawal")
$iMarginBuyingPower = $oXml.GetChildIntValue("Computed|marginBuyingPower")
$sCashBuyingPower = $oXml.GetChildContent("Computed|cashBuyingPower")
$iDtMarginBuyingPower = $oXml.GetChildIntValue("Computed|dtMarginBuyingPower")
$iDtCashBuyingPower = $oXml.GetChildIntValue("Computed|dtCashBuyingPower")
$iShortAdjustBalance = $oXml.GetChildIntValue("Computed|shortAdjustBalance")
$iRegtEquity = $oXml.GetChildIntValue("Computed|regtEquity")
$iRegtEquityPercent = $oXml.GetChildIntValue("Computed|regtEquityPercent")
$iAccountBalance = $oXml.GetChildIntValue("Computed|accountBalance")
$iDtCashOpenOrderReserve = $oXml.GetChildIntValue("Margin|dtCashOpenOrderReserve")
$iDtMarginOpenOrderReserve = $oXml.GetChildIntValue("Margin|dtMarginOpenOrderReserve")

ConsoleWrite("Success." & @CRLF)