Sample code for 30+ languages & platforms
PowerBuilder

ETrade Get Account Balances

See more ETrade Examples

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

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_JsonToken
string ls_SandboxUrl
string ls_LiveUrl
oleobject loo_Resp
oleobject loo_Xml
integer li_AccountId
string ls_AccountType
string ls_OptionLevel
string ls_AccountDescription
integer li_QuoteMode
string ls_DayTraderStatus
string ls_AccountMode
integer li_FundsForOpenOrdersCash
integer li_MoneyMktBalance
integer li_CashAvailableForInvestment
string ls_NetCash
string ls_CashBalance
integer li_SettledCashForInvestment
integer li_UnSettledCashForInvestment
integer li_FundsWithheldFromPurchasePower
integer li_FundsWithheldFromWithdrawal
integer li_MarginBuyingPower
string ls_CashBuyingPower
integer li_DtMarginBuyingPower
integer li_DtCashBuyingPower
integer li_ShortAdjustBalance
integer li_RegtEquity
integer li_RegtEquityPercent
integer li_AccountBalance
integer li_DtCashOpenOrderReserve
integer li_DtMarginOpenOrderReserve

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.OAuth1 = 1
loo_Http.OAuthVerifier = ""
loo_Http.OAuthConsumerKey = "ETRADE_CONSUMER_KEY"
loo_Http.OAuthConsumerSecret = "ETRADE_CONSUMER_SECRET"

// Load the access token previously obtained via the OAuth1 Authorization
loo_JsonToken = create oleobject
li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_JsonToken.LoadFile("qa_data/tokens/etrade.json")
if li_Success <> 1 then
    Write-Debug "Failed to load OAuth1 token"
    destroy loo_Http
    destroy loo_JsonToken
    return
end if

loo_Http.OAuthToken = loo_JsonToken.StringOf("oauth_token")
loo_Http.OAuthTokenSecret = loo_JsonToken.StringOf("oauth_token_secret")

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

loo_Http.SetUrlVar("accountIdKey","6_Dpy0rmuQ9cu9IbTfvF2A")
loo_Http.SetUrlVar("instType","BROKERAGE")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpNoBody("GET",ls_SandboxUrl,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_JsonToken
    destroy loo_Resp
    return
end if

// Make sure a successful response was received.
if loo_Resp.StatusCode > 200 then
    Write-Debug loo_Resp.StatusLine
    Write-Debug loo_Resp.Header
    Write-Debug loo_Resp.BodyStr
    destroy loo_Http
    destroy loo_JsonToken
    destroy loo_Resp
    return
end if

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

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.LoadXml(loo_Resp.BodyStr)
Write-Debug loo_Xml.GetXml()

li_AccountId = loo_Xml.GetChildIntValue("accountId")
ls_AccountType = loo_Xml.GetChildContent("accountType")
ls_OptionLevel = loo_Xml.GetChildContent("optionLevel")
ls_AccountDescription = loo_Xml.GetChildContent("accountDescription")
li_QuoteMode = loo_Xml.GetChildIntValue("quoteMode")
ls_DayTraderStatus = loo_Xml.GetChildContent("dayTraderStatus")
ls_AccountMode = loo_Xml.GetChildContent("accountMode")
li_FundsForOpenOrdersCash = loo_Xml.GetChildIntValue("Cash|fundsForOpenOrdersCash")
li_MoneyMktBalance = loo_Xml.GetChildIntValue("Cash|moneyMktBalance")
li_CashAvailableForInvestment = loo_Xml.GetChildIntValue("Computed|cashAvailableForInvestment")
ls_NetCash = loo_Xml.GetChildContent("Computed|netCash")
ls_CashBalance = loo_Xml.GetChildContent("Computed|cashBalance")
li_SettledCashForInvestment = loo_Xml.GetChildIntValue("Computed|settledCashForInvestment")
li_UnSettledCashForInvestment = loo_Xml.GetChildIntValue("Computed|unSettledCashForInvestment")
li_FundsWithheldFromPurchasePower = loo_Xml.GetChildIntValue("Computed|fundsWithheldFromPurchasePower")
li_FundsWithheldFromWithdrawal = loo_Xml.GetChildIntValue("Computed|fundsWithheldFromWithdrawal")
li_MarginBuyingPower = loo_Xml.GetChildIntValue("Computed|marginBuyingPower")
ls_CashBuyingPower = loo_Xml.GetChildContent("Computed|cashBuyingPower")
li_DtMarginBuyingPower = loo_Xml.GetChildIntValue("Computed|dtMarginBuyingPower")
li_DtCashBuyingPower = loo_Xml.GetChildIntValue("Computed|dtCashBuyingPower")
li_ShortAdjustBalance = loo_Xml.GetChildIntValue("Computed|shortAdjustBalance")
li_RegtEquity = loo_Xml.GetChildIntValue("Computed|regtEquity")
li_RegtEquityPercent = loo_Xml.GetChildIntValue("Computed|regtEquityPercent")
li_AccountBalance = loo_Xml.GetChildIntValue("Computed|accountBalance")
li_DtCashOpenOrderReserve = loo_Xml.GetChildIntValue("Margin|dtCashOpenOrderReserve")
li_DtMarginOpenOrderReserve = loo_Xml.GetChildIntValue("Margin|dtMarginOpenOrderReserve")

Write-Debug "Success."


destroy loo_Http
destroy loo_JsonToken
destroy loo_Resp
destroy loo_Xml