Sample code for 30+ languages & platforms
PureBasic

ETrade Get Account Balances

See more ETrade Examples

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkXml.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkOAuth1(http, 1)
    CkHttp::setCkOAuthVerifier(http, "")
    CkHttp::setCkOAuthConsumerKey(http, "ETRADE_CONSUMER_KEY")
    CkHttp::setCkOAuthConsumerSecret(http, "ETRADE_CONSUMER_SECRET")

    ; Load the access token previously obtained via the OAuth1 Authorization
    jsonToken.i = CkJsonObject::ckCreate()
    If jsonToken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/etrade.json")
    If success <> 1
        Debug "Failed to load OAuth1 token"
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        ProcedureReturn
    EndIf

    CkHttp::setCkOAuthToken(http, CkJsonObject::ckStringOf(jsonToken,"oauth_token"))
    CkHttp::setCkOAuthTokenSecret(http, CkJsonObject::ckStringOf(jsonToken,"oauth_token_secret"))

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

    CkHttp::ckSetUrlVar(http,"accountIdKey","6_Dpy0rmuQ9cu9IbTfvF2A")
    CkHttp::ckSetUrlVar(http,"instType","BROKERAGE")

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpNoBody(http,"GET",sandboxUrl,resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    ; Make sure a successful response was received.
    If CkHttpResponse::ckStatusCode(resp) > 200
        Debug CkHttpResponse::ckStatusLine(resp)
        Debug CkHttpResponse::ckHeader(resp)
        Debug CkHttpResponse::ckBodyStr(resp)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    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>

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckLoadXml(xml,CkHttpResponse::ckBodyStr(resp))
    Debug CkXml::ckGetXml(xml)

    accountId.i
    accountType.s
    optionLevel.s
    accountDescription.s
    quoteMode.i
    dayTraderStatus.s
    accountMode.s
    fundsForOpenOrdersCash.i
    moneyMktBalance.i
    cashAvailableForInvestment.i
    netCash.s
    cashBalance.s
    settledCashForInvestment.i
    unSettledCashForInvestment.i
    fundsWithheldFromPurchasePower.i
    fundsWithheldFromWithdrawal.i
    marginBuyingPower.i
    cashBuyingPower.s
    dtMarginBuyingPower.i
    dtCashBuyingPower.i
    shortAdjustBalance.i
    regtEquity.i
    regtEquityPercent.i
    accountBalance.i
    dtCashOpenOrderReserve.i
    dtMarginOpenOrderReserve.i

    accountId = CkXml::ckGetChildIntValue(xml,"accountId")
    accountType = CkXml::ckGetChildContent(xml,"accountType")
    optionLevel = CkXml::ckGetChildContent(xml,"optionLevel")
    accountDescription = CkXml::ckGetChildContent(xml,"accountDescription")
    quoteMode = CkXml::ckGetChildIntValue(xml,"quoteMode")
    dayTraderStatus = CkXml::ckGetChildContent(xml,"dayTraderStatus")
    accountMode = CkXml::ckGetChildContent(xml,"accountMode")
    fundsForOpenOrdersCash = CkXml::ckGetChildIntValue(xml,"Cash|fundsForOpenOrdersCash")
    moneyMktBalance = CkXml::ckGetChildIntValue(xml,"Cash|moneyMktBalance")
    cashAvailableForInvestment = CkXml::ckGetChildIntValue(xml,"Computed|cashAvailableForInvestment")
    netCash = CkXml::ckGetChildContent(xml,"Computed|netCash")
    cashBalance = CkXml::ckGetChildContent(xml,"Computed|cashBalance")
    settledCashForInvestment = CkXml::ckGetChildIntValue(xml,"Computed|settledCashForInvestment")
    unSettledCashForInvestment = CkXml::ckGetChildIntValue(xml,"Computed|unSettledCashForInvestment")
    fundsWithheldFromPurchasePower = CkXml::ckGetChildIntValue(xml,"Computed|fundsWithheldFromPurchasePower")
    fundsWithheldFromWithdrawal = CkXml::ckGetChildIntValue(xml,"Computed|fundsWithheldFromWithdrawal")
    marginBuyingPower = CkXml::ckGetChildIntValue(xml,"Computed|marginBuyingPower")
    cashBuyingPower = CkXml::ckGetChildContent(xml,"Computed|cashBuyingPower")
    dtMarginBuyingPower = CkXml::ckGetChildIntValue(xml,"Computed|dtMarginBuyingPower")
    dtCashBuyingPower = CkXml::ckGetChildIntValue(xml,"Computed|dtCashBuyingPower")
    shortAdjustBalance = CkXml::ckGetChildIntValue(xml,"Computed|shortAdjustBalance")
    regtEquity = CkXml::ckGetChildIntValue(xml,"Computed|regtEquity")
    regtEquityPercent = CkXml::ckGetChildIntValue(xml,"Computed|regtEquityPercent")
    accountBalance = CkXml::ckGetChildIntValue(xml,"Computed|accountBalance")
    dtCashOpenOrderReserve = CkXml::ckGetChildIntValue(xml,"Margin|dtCashOpenOrderReserve")
    dtMarginOpenOrderReserve = CkXml::ckGetChildIntValue(xml,"Margin|dtMarginOpenOrderReserve")

    Debug "Success."


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(jsonToken)
    CkHttpResponse::ckDispose(resp)
    CkXml::ckDispose(xml)


    ProcedureReturn
EndProcedure