Sample code for 30+ languages & platforms
AutoIt

ETrade v1 Get Account Balances

See more HTTP Misc Examples

Get account balances using the ETrade v1 API.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  This example 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 3-Legged Authorization examples Step1 and Step2.
$oJson = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJson.LoadFile("qa_data/tokens/etrade.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Failed to load OAuth1 token" & @CRLF)
    Exit
EndIf

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

;  See the ETrade v1 API documentation HERE.

$oHttp.SetUrlVar("accountIdKey","vsnhtF7d9jXxBy6HyaAC4vQ")
$oHttp.SetUrlVar("instType","BROKERAGE")
Local $sRespStr = $oHttp.QuickGetStr("https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true")
If ($oHttp.LastMethodSuccess <> True) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

;  A 200 status code indicates success.
Local $iStatusCode = $oHttp.LastStatus
ConsoleWrite("statusCode = " & $iStatusCode & @CRLF)

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

;  A sample XML response is shown below...

$oXml = ObjCreate("Chilkat.Xml")
$oXml.LoadXml($sRespStr)

Local $sTagPath
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")

;  <?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>