PureBasic
PureBasic
ETrade v1 Get Account Balances
See more HTTP Misc Examples
Get account balances using the ETrade v1 API.Chilkat PureBasic Downloads
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkXml.pb"
Procedure ChilkatExample()
success.i = 0
; This example 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 3-Legged Authorization examples Step1 and Step2.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(json,"qa_data/tokens/etrade.json")
If success <> 1
Debug "Failed to load OAuth1 token"
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
CkHttp::setCkOAuthToken(http, CkJsonObject::ckStringOf(json,"oauth_token"))
CkHttp::setCkOAuthTokenSecret(http, CkJsonObject::ckStringOf(json,"oauth_token_secret"))
; See the ETrade v1 API documentation HERE.
CkHttp::ckSetUrlVar(http,"accountIdKey","vsnhtF7d9jXxBy6HyaAC4vQ")
CkHttp::ckSetUrlVar(http,"instType","BROKERAGE")
respStr.s = CkHttp::ckQuickGetStr(http,"https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true")
If CkHttp::ckLastMethodSuccess(http) <> 1
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
; A 200 status code indicates success.
statusCode.i = CkHttp::ckLastStatus(http)
Debug "statusCode = " + Str(statusCode)
; Use the following online tool to generate parsing code from sample XML:
; Generate Parsing Code from XML
; A sample XML response is shown below...
xml.i = CkXml::ckCreate()
If xml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::ckLoadXml(xml,respStr)
tagPath.s
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")
; <?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>
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkXml::ckDispose(xml)
ProcedureReturn
EndProcedure