Sample code for 30+ languages & platforms
Zig

ETrade View Portfolio

See more ETrade Examples

Gets portfolio information for the selected brokerage account.

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 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}/portfolio";

    http.setUrlVar("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A") 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" standalone="yes"?>
    // <PortfolioResponse>
    //     <AccountPortfolio>
    //         <accountId>83359700</accountId>
    //         <Position>
    //             <positionId>27005131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>BR</symbol>
    //             </Product>
    //             <symbolDescription>BR</symbolDescription>
    //             <dateAcquired>-57600000</dateAcquired>
    //             <pricePaid>0</pricePaid>
    //             <commissions>0</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>10</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>-2.7999</daysGain>
    //             <daysGainPct>-1.3346</daysGainPct>
    //             <marketValue>207</marketValue>
    //             <totalCost>0</totalCost>
    //             <totalGain>207</totalGain>
    //             <totalGainPct>0</totalGainPct>
    //             <pctOfPortfolio>0.0018</pctOfPortfolio>
    //             <costPerShare>0</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.28</change>
    //                 <changePct>-1.3346</changePct>
    //                 <lastTrade>20.70</lastTrade>
    //                 <lastTradeTime>1343160240</lastTradeTime>
    //                 <volume>431591</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/27005131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/BR</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4709131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>GLD</symbol>
    //             </Product>
    //             <symbolDescription>GLD</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>2</quantity>
    //             <positionIndicator>TYPE1</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>0.9799</daysGain>
    //             <daysGainPct>0.3201</daysGainPct>
    //             <marketValue>307.04</marketValue>
    //             <totalCost>7</totalCost>
    //             <totalGain>300.04</totalGain>
    //             <totalGainPct>4286.2857</totalGainPct>
    //             <pctOfPortfolio>0.0028</pctOfPortfolio>
    //             <costPerShare>3.5</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>0.49</change>
    //                 <changePct>0.3201</changePct>
    //                 <lastTrade>153.52</lastTrade>
    //                 <lastTradeTime>1343160000</lastTradeTime>
    //                 <volume>6510878</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4709131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/GLD</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4729131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>MSFT</symbol>
    //             </Product>
    //             <symbolDescription>MSFT</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>1</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>-0.13</daysGain>
    //             <daysGainPct>-0.4439</daysGainPct>
    //             <marketValue>29.1499</marketValue>
    //             <totalCost>6</totalCost>
    //             <totalGain>23.1499</totalGain>
    //             <totalGainPct>385.8333</totalGainPct>
    //             <pctOfPortfolio>0.0002</pctOfPortfolio>
    //             <costPerShare>6</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.13</change>
    //                 <changePct>-0.4439</changePct>
    //                 <lastTrade>29.15</lastTrade>
    //                 <lastTradeTime>1343160000</lastTradeTime>
    //                 <volume>47711254</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4729131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/MSFT</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>23971131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>MSFT</symbol>
    //             </Product>
    //             <symbolDescription>MSFT</symbolDescription>
    //             <dateAcquired>-57600000</dateAcquired>
    //             <pricePaid>0</pricePaid>
    //             <commissions>0</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>-10</quantity>
    //             <positionIndicator>TYPE5</positionIndicator>
    //             <positionType>SHORT</positionType>
    //             <daysGain>1.30</daysGain>
    //             <daysGainPct>-0.4439</daysGainPct>
    //             <marketValue>-291.5</marketValue>
    //             <totalCost>0</totalCost>
    //             <totalGain>-291.5</totalGain>
    //             <totalGainPct>0</totalGainPct>
    //             <pctOfPortfolio>-0.0026</pctOfPortfolio>
    //             <costPerShare>0</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.13</change>
    //                 <changePct>-0.4439</changePct>
    //                 <lastTrade>29.15</lastTrade>
    //                 <lastTradeTime>1343160000</lastTradeTime>
    //                 <volume>47711254</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/23971131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/MSFT</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4725131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>RBL</symbol>
    //             </Product>
    //             <symbolDescription>RBL</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>2</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>-0.52</daysGain>
    //             <daysGainPct>-1.0252</daysGainPct>
    //             <marketValue>50.20</marketValue>
    //             <totalCost>7</totalCost>
    //             <totalGain>43.20</totalGain>
    //             <totalGainPct>617.1428</totalGainPct>
    //             <pctOfPortfolio>0.0004</pctOfPortfolio>
    //             <costPerShare>3.5</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.26</change>
    //                 <changePct>-1.0252</changePct>
    //                 <lastTrade>25.10</lastTrade>
    //                 <lastTradeTime>1343159760</lastTradeTime>
    //                 <volume>11827</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4725131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/RBL</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>20841131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>RPI</symbol>
    //             </Product>
    //             <symbolDescription>RPI</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>7.5599</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>45</otherFees>
    //             <quantity>2</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>0</daysGain>
    //             <daysGainPct>0</daysGainPct>
    //             <marketValue>2.8599</marketValue>
    //             <totalCost>65.12</totalCost>
    //             <totalGain>-62.2599</totalGain>
    //             <totalGainPct>-95.6081</totalGainPct>
    //             <pctOfPortfolio>0.00</pctOfPortfolio>
    //             <costPerShare>32.56</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>0</change>
    //                 <changePct>0</changePct>
    //                 <lastTrade>1.43</lastTrade>
    //                 <lastTradeTime>1343150700</lastTradeTime>
    //                 <volume>1803</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/20841131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/RPI</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4732131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>RXD</symbol>
    //             </Product>
    //             <symbolDescription>RXD</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>2.99</commissions>
    //             <otherFees>0.0099</otherFees>
    //             <quantity>-3</quantity>
    //             <positionIndicator>TYPE5</positionIndicator>
    //             <positionType>SHORT</positionType>
    //             <daysGain>-1.296</daysGain>
    //             <daysGainPct>2.6799</daysGainPct>
    //             <marketValue>-49.6559</marketValue>
    //             <totalCost>0</totalCost>
    //             <totalGain>-49.6559</totalGain>
    //             <totalGainPct>-827.60</totalGainPct>
    //             <pctOfPortfolio>-0.0004</pctOfPortfolio>
    //             <costPerShare>0</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>0.432</change>
    //                 <changePct>2.6799</changePct>
    //                 <lastTrade>16.552</lastTrade>
    //                 <lastTradeTime>1343157480</lastTradeTime>
    //                 <volume>2200</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4732131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/RXD</quoteDetails>
    //         </Position>
    //         <totalPages>1</totalPages>
    //     </AccountPortfolio>
    // </PortfolioResponse>
    //

    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)});

    var position_id: i32 = 0;
    var expiry_day: i32 = 0;
    var expiry_month: i32 = 0;
    var expiry_year: i32 = 0;
    var security_type: [:0]const u8 = "";
    var strike_price: i32 = 0;
    var symbol: [:0]const u8 = "";
    var symbol_description: [:0]const u8 = "";
    var date_acquired: [:0]const u8 = "";
    var price_paid: [:0]const u8 = "";
    var commissions: [:0]const u8 = "";
    var other_fees: [:0]const u8 = "";
    var quantity: i32 = 0;
    var position_indicator: [:0]const u8 = "";
    var position_type: [:0]const u8 = "";
    var days_gain: [:0]const u8 = "";
    var days_gain_pct: [:0]const u8 = "";
    var market_value: [:0]const u8 = "";
    var total_cost: [:0]const u8 = "";
    var total_gain: [:0]const u8 = "";
    var total_gain_pct: [:0]const u8 = "";
    var pct_of_portfolio: [:0]const u8 = "";
    var cost_per_share: [:0]const u8 = "";
    var today_commissions: i32 = 0;
    var today_fees: i32 = 0;
    var today_price_paid: i32 = 0;
    var today_quantity: i32 = 0;
    var change: [:0]const u8 = "";
    var change_pct: [:0]const u8 = "";
    var last_trade: [:0]const u8 = "";
    var last_trade_time: i32 = 0;
    var volume: i32 = 0;
    var lots_details: [:0]const u8 = "";
    var quote_details: [:0]const u8 = "";

    _ = xml.getChildIntValue("AccountPortfolio|accountId");
    var i: i32 = 0;
    const count_i = xml.numChildrenHavingTag("AccountPortfolio|Position");
    while (i < count_i) {
        xml.setI(i);
        position_id = xml.getChildIntValue("AccountPortfolio|Position[i]|positionId");
        expiry_day = xml.getChildIntValue("AccountPortfolio|Position[i]|Product|expiryDay");
        expiry_month = xml.getChildIntValue("AccountPortfolio|Position[i]|Product|expiryMonth");
        expiry_year = xml.getChildIntValue("AccountPortfolio|Position[i]|Product|expiryYear");
        security_type = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|Product|securityType");
        strike_price = xml.getChildIntValue("AccountPortfolio|Position[i]|Product|strikePrice");
        symbol = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|Product|symbol");
        symbol_description = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|symbolDescription");
        date_acquired = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|dateAcquired");
        price_paid = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|pricePaid");
        commissions = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|commissions");
        other_fees = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|otherFees");
        quantity = xml.getChildIntValue("AccountPortfolio|Position[i]|quantity");
        position_indicator = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|positionIndicator");
        position_type = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|positionType");
        days_gain = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|daysGain");
        days_gain_pct = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|daysGainPct");
        market_value = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|marketValue");
        total_cost = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|totalCost");
        total_gain = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|totalGain");
        total_gain_pct = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|totalGainPct");
        pct_of_portfolio = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|pctOfPortfolio");
        cost_per_share = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|costPerShare");
        today_commissions = xml.getChildIntValue("AccountPortfolio|Position[i]|todayCommissions");
        today_fees = xml.getChildIntValue("AccountPortfolio|Position[i]|todayFees");
        today_price_paid = xml.getChildIntValue("AccountPortfolio|Position[i]|todayPricePaid");
        today_quantity = xml.getChildIntValue("AccountPortfolio|Position[i]|todayQuantity");
        change = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|Quick|change");
        change_pct = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|Quick|changePct");
        last_trade = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|Quick|lastTrade");
        last_trade_time = xml.getChildIntValue("AccountPortfolio|Position[i]|Quick|lastTradeTime");
        volume = xml.getChildIntValue("AccountPortfolio|Position[i]|Quick|volume");
        lots_details = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|lotsDetails");
        quote_details = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|quoteDetails");
        i = i + 1;
    }

    _ = xml.getChildIntValue("AccountPortfolio|totalPages");

    std.debug.print("Success.\n", .{});
}