Sample code for 30+ languages & platforms
Zig

ETrade v1 Get Account Balances

See more HTTP Misc Examples

Get account balances using the ETrade v1 API.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example 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 3-Legged Authorization examples Step1 and Step2.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadFile("qa_data/tokens/etrade.json") catch {
        std.debug.print("Failed to load OAuth1 token\n", .{});
        return;
    };

    http.setOAuthToken(try json.stringOf(alloc, "oauth_token"));
    http.setOAuthTokenSecret(try json.stringOf(alloc, "oauth_token_secret"));

    // See the ETrade v1 API documentation HERE.

    http.setUrlVar("accountIdKey", "vsnhtF7d9jXxBy6HyaAC4vQ") catch {};
    http.setUrlVar("instType", "BROKERAGE") catch {};
    const resp_str = http.quickGetStr(alloc, "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/balance?instType={$instType}&realTimeNAV=true") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // A 200 status code indicates success.
    const status_code = http.getLastStatus();
    std.debug.print("statusCode = {d}\n", .{status_code});

    // Use the following online tool to generate parsing code from sample XML:
    // Generate Parsing Code from XML

    // A sample XML response is shown below...

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(resp_str) catch {};

    _ = 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");

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