Sample code for 30+ languages & platforms
Zig

ETrade List Orders

See more ETrade Examples

Gets the order details for a selected brokerage account based on the search criteria provided.

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

    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"?>
    // <OrdersResponse>
    //     <marker>12345678999</marker>
    //     <next>https://api.sit.etrade.com/accounts/E5Nd4LJBsEi_UyHm4Vio9g/orders?marker=12345678999</next>
    //     <Order>
    //         <orderId>479</orderId>
    //         <details>https://api.etrade.com/accounts/E5Nd4LJBsEi_UyHm4Vio9g/orders/479</details>
    //         <orderType>OPTN</orderType>
    //         <OrderDetail>
    //             <placedTime>123453456</placedTime>
    //             <orderValue>123.0000</orderValue>
    //             <status>OPEN</status>
    //             <orderTerm>GOOD_FOR_DAY</orderTerm>
    //             <priceType>LIMIT</priceType>
    //             <limitPrice>1.5</limitPrice>
    //             <stopPrice>0</stopPrice>
    //             <marketSession>REGULAR</marketSession>
    //             <allOrNone>false</allOrNone>
    //             <Instrument>
    //                 <Product>
    //                     <symbol>RIMM</symbol>
    //                     <securityType>OPTN</securityType>
    //                     <callPut>CALL</callPut>
    //                     <expiryYear>2012</expiryYear>
    //                     <expiryMonth>3</expiryMonth>
    //                     <expiryDay>9</expiryDay>
    //                     <strikePrice>12</strikePrice>
    //                 </Product>
    //                 <symbolDescription>RESEARCH IN MOTION LTD COM</symbolDescription>
    //                 <orderAction>BUY_OPEN</orderAction>
    //                 <quantityType>QUANTITY</quantityType>
    //                 <orderedQuantity>5</orderedQuantity>
    //                 <filledQuantity>5</filledQuantity>
    //                 <averageExecutionPrice>0</averageExecutionPrice>
    //                 <estimatedCommission>9.99</estimatedCommission>
    //                 <estimatedFees>0</estimatedFees>
    //             </Instrument>
    //             <netPrice>0</netPrice>
    //             <netBid>0</netBid>
    //             <netAsk>0</netAsk>
    //             <gcd>0</gcd>
    //             <ratio/>
    //         </OrderDetail>
    //     </Order>
    //     <Order>
    //         <orderId>477</orderId>
    //         <details>https://api.etrade.com/accounts/E5Nd4LJBsEi_UyHm4Vio9g/orders/477</details>
    //         <orderType>ONE_CANCELS_ALL</orderType>
    //         <totalOrderValue>209.99</totalOrderValue>
    //         <totalCommission>10.74</totalCommission>
    //         <OrderDetail>
    //             <orderNumber>1</orderNumber>
    //             <placedTime>1331699203122</placedTime>
    //             <orderValue>123.0000</orderValue>
    //             <status>OPEN</status>
    //             <orderTerm>GOOD_FOR_DAY</orderTerm>
    //             <priceType>LIMIT</priceType>
    //             <limitPrice>2</limitPrice>
    //             <stopPrice>0</stopPrice>
    //             <marketSession>REGULAR</marketSession>
    //             <bracketedLimitPrice>2</bracketedLimitPrice>
    //             <initialStopPrice>2</initialStopPrice>
    //             <allOrNone>false</allOrNone>
    //             <Instrument>
    //                 <Product>
    //                     <symbol>ETFC</symbol>
    //                     <securityType>EQ</securityType>
    //                 </Product>
    //                 <symbolDescription>ETRADE Financials</symbolDescription>
    //                 <orderAction>BUY</orderAction>
    //                 <quantityType>QUANTITY</quantityType>
    //                 <orderedQuantity>100</orderedQuantity>
    //                 <filledQuantity>0</filledQuantity>
    //                 <averageExecutionPrice>0</averageExecutionPrice>
    //                 <estimatedCommission>9.99</estimatedCommission>
    //                 <estimatedFees>0</estimatedFees>
    //             </Instrument>
    //             <netPrice>0</netPrice>
    //             <netBid>0</netBid>
    //             <netAsk>0</netAsk>
    //             <gcd>0</gcd>
    //             <ratio/>
    //         </OrderDetail>
    //         <OrderDetail>
    //             <orderNumber>2</orderNumber>
    //             <placedTime>1331699203</placedTime>
    //             <orderValue>231.0000</orderValue>
    //             <status>OPEN</status>
    //             <orderTerm>GOOD_FOR_DAY</orderTerm>
    //             <priceType>LIMIT</priceType>
    //             <limitPrice>0.5</limitPrice>
    //             <stopPrice>0</stopPrice>
    //             <marketSession>REGULAR</marketSession>
    //             <initialStopPrice>0.5</initialStopPrice>
    //             <allOrNone>false</allOrNone>
    //             <Instrument>
    //                 <Product>
    //                     <symbol>MON</symbol>
    //                     <securityType>OPTN</securityType>
    //                     <callPut>CALL</callPut>
    //                     <expiryYear>2012</expiryYear>
    //                     <expiryMonth>4</expiryMonth>
    //                     <expiryDay>21</expiryDay>
    //                     <strikePrice>85</strikePrice>
    //                 </Product>
    //                 <symbolDescription>MON Mar 9 '12 $85 Call</symbolDescription>
    //                 <orderAction>BUY_OPEN</orderAction>
    //                 <quantityType>QUANTITY</quantityType>
    //                 <orderedQuantity>1</orderedQuantity>
    //                 <filledQuantity>0</filledQuantity>
    //                 <averageExecutionPrice>0</averageExecutionPrice>
    //                 <estimatedCommission>9.99</estimatedCommission>
    //                 <estimatedFees>0</estimatedFees>
    //             </Instrument>
    //             <netPrice>0</netPrice>
    //             <netBid>0</netBid>
    //             <netAsk>0</netAsk>
    //             <gcd>0</gcd>
    //             <ratio/>
    //         </OrderDetail>
    //     </Order>
    //     <Order>
    //         <orderId>475</orderId>
    //         <details>https://api.etrade.com/accounts/E5Nd4LJBsEi_UyHm4Vio9g/orders/475</details>
    //         <orderType>SPREADS</orderType>
    //         <OrderDetail>
    //             <placedTime>1331742953</placedTime>
    //             <executedTime>1331742955432</executedTime>
    //             <orderValue>4445.99</orderValue>
    //             <status>EXECUTED</status>
    //             <orderTerm>GOOD_FOR_DAY</orderTerm>
    //             <priceType>NET_DEBIT</priceType>
    //             <limitPrice>1.5</limitPrice>
    //             <stopPrice>0</stopPrice>
    //             <marketSession>REGULAR</marketSession>
    //             <allOrNone>false</allOrNone>
    //             <Instrument>
    //                 <Product>
    //                     <symbol>REE</symbol>
    //                     <securityType>OPTN</securityType>
    //                     <callPut>CALL</callPut>
    //                     <expiryYear>2012</expiryYear>
    //                     <expiryMonth>7</expiryMonth>
    //                     <expiryDay>21</expiryDay>
    //                     <strikePrice>7</strikePrice>
    //                 </Product>
    //                 <symbolDescription>REE Jul 21 '12 $7 Call</symbolDescription>
    //                 <orderAction>BUY_OPEN</orderAction>
    //                 <quantityType>QUANTITY</quantityType>
    //                 <orderedQuantity>2</orderedQuantity>
    //                 <filledQuantity>2</filledQuantity>
    //                 <averageExecutionPrice>1.5</averageExecutionPrice>
    //                 <estimatedCommission>7.24</estimatedCommission>
    //                 <estimatedFees>0</estimatedFees>
    //             </Instrument>
    //             <Instrument>
    //                 <Product>
    //                     <symbol>REE</symbol>
    //                     <securityType>OPTN</securityType>
    //                     <callPut>PUT</callPut>
    //                     <expiryYear>2013</expiryYear>
    //                     <expiryMonth>1</expiryMonth>
    //                     <expiryDay>19</expiryDay>
    //                     <strikePrice>12.50</strikePrice>
    //                 </Product>
    //                 <symbolDescription>REE Jan 19 '13 $12.50 Put</symbolDescription>
    //                 <orderAction>BUY_OPEN</orderAction>
    //                 <quantityType>QUANTITY</quantityType>
    //                 <orderedQuantity>2</orderedQuantity>
    //                 <filledQuantity>2</filledQuantity>
    //                 <averageExecutionPrice>1.5</averageExecutionPrice>
    //                 <estimatedCommission>7.24</estimatedCommission>
    //                 <estimatedFees>0</estimatedFees>
    //             </Instrument>
    //             <netPrice>0</netPrice>
    //             <netBid>0</netBid>
    //             <netAsk>0</netAsk>
    //             <gcd>0</gcd>
    //             <ratio/>
    //         </OrderDetail>
    //     </Order>
    // </OrdersResponse>
    //

    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 order_id: i32 = 0;
    var details: [:0]const u8 = "";
    var order_type: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var placed_time: [:0]const u8 = "";
    var order_value: [:0]const u8 = "";
    var status: [:0]const u8 = "";
    var order_term: [:0]const u8 = "";
    var price_type: [:0]const u8 = "";
    var limit_price: [:0]const u8 = "";
    var stop_price: i32 = 0;
    var market_session: [:0]const u8 = "";
    var all_or_none: [:0]const u8 = "";
    var k: i32 = 0;
    var count_k: i32 = 0;
    var symbol: [:0]const u8 = "";
    var security_type: [:0]const u8 = "";
    var call_put: [:0]const u8 = "";
    var expiry_year: i32 = 0;
    var expiry_month: i32 = 0;
    var expiry_day: i32 = 0;
    var strike_price: [:0]const u8 = "";
    var symbol_description: [:0]const u8 = "";
    var order_action: [:0]const u8 = "";
    var quantity_type: [:0]const u8 = "";
    var ordered_quantity: i32 = 0;
    var filled_quantity: i32 = 0;
    var average_execution_price: [:0]const u8 = "";
    var estimated_commission: [:0]const u8 = "";
    var estimated_fees: i32 = 0;
    var net_price: i32 = 0;
    var net_bid: i32 = 0;
    var net_ask: i32 = 0;
    var gcd: i32 = 0;
    var order_number: i32 = 0;
    var bracketed_limit_price: i32 = 0;
    var initial_stop_price: [:0]const u8 = "";
    var executed_time: [:0]const u8 = "";
    var total_order_value: [:0]const u8 = "";
    var total_commission: [:0]const u8 = "";

    _ = try xml.getChildContent(alloc, "marker");
    _ = try xml.getChildContent(alloc, "next");
    var i: i32 = 0;
    const count_i = xml.numChildrenHavingTag("Order");
    while (i < count_i) {
        xml.setI(i);
        order_id = xml.getChildIntValue("Order[i]|orderId");
        details = try xml.getChildContent(alloc, "Order[i]|details");
        order_type = try xml.getChildContent(alloc, "Order[i]|orderType");
        j = 0;
        count_j = xml.numChildrenHavingTag("Order[i]|OrderDetail");
        while (j < count_j) {
            xml.setJ(j);
            placed_time = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|placedTime");
            order_value = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|orderValue");
            status = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|status");
            order_term = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|orderTerm");
            price_type = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|priceType");
            limit_price = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|limitPrice");
            stop_price = xml.getChildIntValue("Order[i]|OrderDetail[j]|stopPrice");
            market_session = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|marketSession");
            all_or_none = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|allOrNone");
            k = 0;
            count_k = xml.numChildrenHavingTag("Order[i]|OrderDetail[j]|Instrument");
            while (k < count_k) {
                xml.setK(k);
                symbol = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|Product|symbol");
                security_type = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|Product|securityType");
                call_put = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|Product|callPut");
                expiry_year = xml.getChildIntValue("Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryYear");
                expiry_month = xml.getChildIntValue("Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryMonth");
                expiry_day = xml.getChildIntValue("Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryDay");
                strike_price = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|Product|strikePrice");
                symbol_description = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|symbolDescription");
                order_action = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|orderAction");
                quantity_type = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|quantityType");
                ordered_quantity = xml.getChildIntValue("Order[i]|OrderDetail[j]|Instrument[k]|orderedQuantity");
                filled_quantity = xml.getChildIntValue("Order[i]|OrderDetail[j]|Instrument[k]|filledQuantity");
                average_execution_price = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|averageExecutionPrice");
                estimated_commission = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|Instrument[k]|estimatedCommission");
                estimated_fees = xml.getChildIntValue("Order[i]|OrderDetail[j]|Instrument[k]|estimatedFees");
                k = k + 1;
            }

            net_price = xml.getChildIntValue("Order[i]|OrderDetail[j]|netPrice");
            net_bid = xml.getChildIntValue("Order[i]|OrderDetail[j]|netBid");
            net_ask = xml.getChildIntValue("Order[i]|OrderDetail[j]|netAsk");
            gcd = xml.getChildIntValue("Order[i]|OrderDetail[j]|gcd");
            order_number = xml.getChildIntValue("Order[i]|OrderDetail[j]|orderNumber");
            bracketed_limit_price = xml.getChildIntValue("Order[i]|OrderDetail[j]|bracketedLimitPrice");
            initial_stop_price = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|initialStopPrice");
            executed_time = try xml.getChildContent(alloc, "Order[i]|OrderDetail[j]|executedTime");
            j = j + 1;
        }

        total_order_value = try xml.getChildContent(alloc, "Order[i]|totalOrderValue");
        total_commission = try xml.getChildContent(alloc, "Order[i]|totalCommission");
        i = i + 1;
    }

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