Zig
Zig
ETrade Get Account Balances
See more ETrade Examples
Retrieves the current account balance and related details for a specified account.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
http.setOAuth1(true);
http.setOAuthVerifier("");
http.setOAuthConsumerKey("ETRADE_CONSUMER_KEY");
http.setOAuthConsumerSecret("ETRADE_CONSUMER_SECRET");
// Load the access token previously obtained via the OAuth1 Authorization
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/etrade.json") catch {
std.debug.print("Failed to load OAuth1 token\n", .{});
return;
};
http.setOAuthToken(try json_token.stringOf(alloc, "oauth_token"));
http.setOAuthTokenSecret(try json_token.stringOf(alloc, "oauth_token_secret"));
const sandbox_url = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true";
http.setUrlVar("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A") catch {};
http.setUrlVar("instType", "BROKERAGE") catch {};
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpNoBody("GET", sandbox_url, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// Make sure a successful response was received.
if (resp.getStatusCode() > 200) {
std.debug.print("{s}\n", .{try resp.getStatusLine(alloc)});
std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
return;
}
// 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>
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadXml(try resp.getBodyStr(alloc)) catch {};
std.debug.print("{s}\n", .{try xml.getXml(alloc)});
_ = xml.getChildIntValue("accountId");
_ = try xml.getChildContent(alloc, "accountType");
_ = try xml.getChildContent(alloc, "optionLevel");
_ = try xml.getChildContent(alloc, "accountDescription");
_ = xml.getChildIntValue("quoteMode");
_ = try xml.getChildContent(alloc, "dayTraderStatus");
_ = try xml.getChildContent(alloc, "accountMode");
_ = xml.getChildIntValue("Cash|fundsForOpenOrdersCash");
_ = xml.getChildIntValue("Cash|moneyMktBalance");
_ = xml.getChildIntValue("Computed|cashAvailableForInvestment");
_ = try xml.getChildContent(alloc, "Computed|netCash");
_ = try xml.getChildContent(alloc, "Computed|cashBalance");
_ = xml.getChildIntValue("Computed|settledCashForInvestment");
_ = xml.getChildIntValue("Computed|unSettledCashForInvestment");
_ = xml.getChildIntValue("Computed|fundsWithheldFromPurchasePower");
_ = xml.getChildIntValue("Computed|fundsWithheldFromWithdrawal");
_ = xml.getChildIntValue("Computed|marginBuyingPower");
_ = try xml.getChildContent(alloc, "Computed|cashBuyingPower");
_ = xml.getChildIntValue("Computed|dtMarginBuyingPower");
_ = xml.getChildIntValue("Computed|dtCashBuyingPower");
_ = xml.getChildIntValue("Computed|shortAdjustBalance");
_ = xml.getChildIntValue("Computed|regtEquity");
_ = xml.getChildIntValue("Computed|regtEquityPercent");
_ = xml.getChildIntValue("Computed|accountBalance");
_ = xml.getChildIntValue("Margin|dtCashOpenOrderReserve");
_ = xml.getChildIntValue("Margin|dtMarginOpenOrderReserve");
std.debug.print("Success.\n", .{});
}