Lua
Lua
ETrade v1 Get Account Balances
See more HTTP Misc Examples
Get account balances using the ETrade v1 API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
http.oauth1 = true
http.oauth_verifier = ""
http.oauth_consumer_key = "ETRADE_CONSUMER_KEY"
http.oauth_consumer_secret = "ETRADE_CONSUMER_SECRET"
-- Load the access token previously obtained via the OAuth1 3-Legged Authorization examples Step1 and Step2.
local json = chilkat.JsonObject.new()
local success = json:load_file("qa_data/tokens/etrade.json")
if not success then
print("Failed to load OAuth1 token")
return
end
http.oauth_token = json:string_of("oauth_token")
http.oauth_token_secret = json:string_of("oauth_token_secret")
-- See the ETrade v1 API documentation HERE.
http:set_url_var("accountIdKey", "vsnhtF7d9jXxBy6HyaAC4vQ")
http:set_url_var("instType", "BROKERAGE")
local respStr = http:quick_get_str("https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true")
if not http.last_method_success then
print(http.last_error_text)
return
end
-- A 200 status code indicates success.
local statusCode = http.last_status
print("statusCode = " .. 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...
local xml = chilkat.Xml.new()
xml:load_xml(respStr)
local tagPath = ""
local accountId = xml:get_child_int_value("accountId")
local accountType = xml:get_child_content("accountType")
local optionLevel = xml:get_child_content("optionLevel")
local accountDescription = xml:get_child_content("accountDescription")
local quoteMode = xml:get_child_int_value("quoteMode")
local dayTraderStatus = xml:get_child_content("dayTraderStatus")
local accountMode = xml:get_child_content("accountMode")
local fundsForOpenOrdersCash = xml:get_child_int_value("Cash|fundsForOpenOrdersCash")
local moneyMktBalance = xml:get_child_int_value("Cash|moneyMktBalance")
local cashAvailableForInvestment = xml:get_child_int_value("Computed|cashAvailableForInvestment")
local netCash = xml:get_child_content("Computed|netCash")
local cashBalance = xml:get_child_content("Computed|cashBalance")
local settledCashForInvestment = xml:get_child_int_value("Computed|settledCashForInvestment")
local unSettledCashForInvestment = xml:get_child_int_value("Computed|unSettledCashForInvestment")
local fundsWithheldFromPurchasePower = xml:get_child_int_value("Computed|fundsWithheldFromPurchasePower")
local fundsWithheldFromWithdrawal = xml:get_child_int_value("Computed|fundsWithheldFromWithdrawal")
local marginBuyingPower = xml:get_child_int_value("Computed|marginBuyingPower")
local cashBuyingPower = xml:get_child_content("Computed|cashBuyingPower")
local dtMarginBuyingPower = xml:get_child_int_value("Computed|dtMarginBuyingPower")
local dtCashBuyingPower = xml:get_child_int_value("Computed|dtCashBuyingPower")
local shortAdjustBalance = xml:get_child_int_value("Computed|shortAdjustBalance")
local regtEquity = xml:get_child_int_value("Computed|regtEquity")
local regtEquityPercent = xml:get_child_int_value("Computed|regtEquityPercent")
local accountBalance = xml:get_child_int_value("Computed|accountBalance")
local dtCashOpenOrderReserve = xml:get_child_int_value("Margin|dtCashOpenOrderReserve")
local dtMarginOpenOrderReserve = xml:get_child_int_value("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>