Zig
Zig
ETrade v1 View Portfolio
See more HTTP Misc Examples
Get portfolio information for a selected brokerage 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 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 {};
const resp_str = http.quickGetStr(alloc, "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/portfolio") 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 {};
var i: i32 = 0;
var position_id: [:0]const u8 = "";
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: i32 = 0;
var price_paid: i32 = 0;
var commissions: i32 = 0;
var other_fees: i32 = 0;
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: i32 = 0;
var total_gain: [:0]const u8 = "";
var total_gain_pct: i32 = 0;
var pct_of_portfolio: [:0]const u8 = "";
var cost_per_share: i32 = 0;
var today_commissions: i32 = 0;
var today_fees: i32 = 0;
var today_price_paid: i32 = 0;
var today_quantity: i32 = 0;
var adj_prev_close: [:0]const u8 = "";
var change: [:0]const u8 = "";
var change_pct: [:0]const u8 = "";
var last_trade: [:0]const u8 = "";
var last_trade_time: i32 = 0;
var quote_status: [:0]const u8 = "";
var volume: i32 = 0;
var lots_details: [:0]const u8 = "";
var quote_details: [:0]const u8 = "";
_ = xml.getChildIntValue("AccountPortfolio|accountId");
i = 0;
const count_i = xml.numChildrenHavingTag("AccountPortfolio|Position");
while (i < count_i) {
xml.setI(i);
position_id = try xml.getChildContent(alloc, "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 = xml.getChildIntValue("AccountPortfolio|Position[i]|dateAcquired");
price_paid = xml.getChildIntValue("AccountPortfolio|Position[i]|pricePaid");
commissions = xml.getChildIntValue("AccountPortfolio|Position[i]|commissions");
other_fees = xml.getChildIntValue("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 = xml.getChildIntValue("AccountPortfolio|Position[i]|totalCost");
total_gain = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|totalGain");
total_gain_pct = xml.getChildIntValue("AccountPortfolio|Position[i]|totalGainPct");
pct_of_portfolio = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|pctOfPortfolio");
cost_per_share = xml.getChildIntValue("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");
adj_prev_close = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|adjPrevClose");
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");
quote_status = try xml.getChildContent(alloc, "AccountPortfolio|Position[i]|Quick|quoteStatus");
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");
// <?xml version="1.0" encoding="UTF-8"?>
// <PortfolioResponse>
// <AccountPortfolio>
// <accountId>83554788</accountId>
// <Position>
// <positionId>10087531</positionId>
// <Product>
// <expiryDay>0</expiryDay>
// <expiryMonth>0</expiryMonth>
// <expiryYear>0</expiryYear>
// <securityType>EQ</securityType>
// <strikePrice>0</strikePrice>
// <symbol>A</symbol>
// </Product>
// <symbolDescription>A</symbolDescription>
// <dateAcquired>-68400000</dateAcquired>
// <pricePaid>0</pricePaid>
// <commissions>0</commissions>
// <otherFees>0</otherFees>
// <quantity>-120</quantity>
// <positionIndicator>TYPE2</positionIndicator>
// <positionType>SHORT</positionType>
// <daysGain>190.80</daysGain>
// <daysGainPct>2.4472</daysGainPct>
// <marketValue>-7605.60</marketValue>
// <totalCost>0</totalCost>
// <totalGain>-7605.60</totalGain>
// <totalGainPct>0</totalGainPct>
// <pctOfPortfolio>-0.0008</pctOfPortfolio>
// <costPerShare>0</costPerShare>
// <todayCommissions>0</todayCommissions>
// <todayFees>0</todayFees>
// <todayPricePaid>0</todayPricePaid>
// <todayQuantity>0</todayQuantity>
// <adjPrevClose>64.970000</adjPrevClose>
// <Quick>
// <change>-1.59</change>
// <changePct>-2.4472</changePct>
// <lastTrade>63.38</lastTrade>
// <lastTradeTime>1529429280</lastTradeTime>
// <quoteStatus>DELAYED</quoteStatus>
// <volume>2431617</volume>
// </Quick>
// <lotsDetails>https://api.etrade.com/v1/accounts/JDIozUumZpHdgbIjMnAAHQ/portfolio/10087531</lotsDetails>
// <quoteDetails>https://api.etrade.com/v1/market/quote/A</quoteDetails>
// </Position>
// <Position>
// <positionId>140357348131</positionId>
// <Product>
// <expiryDay>0</expiryDay>
// <expiryMonth>0</expiryMonth>
// <expiryYear>0</expiryYear>
// <securityType>EQ</securityType>
// <strikePrice>0</strikePrice>
// <symbol>TWTR</symbol>
// </Product>
// <symbolDescription>TWTR</symbolDescription>
// <dateAcquired>-68400000</dateAcquired>
// <pricePaid>0</pricePaid>
// <commissions>0</commissions>
// <otherFees>0</otherFees>
// <quantity>3</quantity>
// <positionIndicator>TYPE2</positionIndicator>
// <positionType>LONG</positionType>
// <daysGain>-3.915</daysGain>
// <daysGainPct>-2.8369</daysGainPct>
// <marketValue>134.085</marketValue>
// <totalCost>0</totalCost>
// <totalGain>134.085</totalGain>
// <totalGainPct>0</totalGainPct>
// <pctOfPortfolio>0.0235</pctOfPortfolio>
// <costPerShare>0</costPerShare>
// <todayCommissions>0</todayCommissions>
// <todayFees>0</todayFees>
// <todayPricePaid>0</todayPricePaid>
// <todayQuantity>0</todayQuantity>
// <adjPrevClose>46.000000</adjPrevClose>
// <Quick>
// <change>-1.305</change>
// <changePct>-2.8369</changePct>
// <lastTrade>44.695</lastTrade>
// <lastTradeTime>1529429280</lastTradeTime>
// <quoteStatus>DELAYED</quoteStatus>
// <volume>26582141</volume>
// </Quick>
// <lotsDetails>https://api.etrade.com/v1/accounts/yIFaUoJ81qyAhgxLWRQ42g/portfolio/140357348131</lotsDetails>
// <quoteDetails>https://api.etrade.com/v1/market/quote/TWTR</quoteDetails>
// </Position>
// <totalPages>1</totalPages>
// </AccountPortfolio>
// </PortfolioResponse>
}