Sample code for 30+ languages & platforms
Unicode C++

ETrade List Orders

See more ETrade Examples

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

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkXmlW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttpW http;

    http.put_OAuth1(true);
    http.put_OAuthVerifier(L"");
    http.put_OAuthConsumerKey(L"ETRADE_CONSUMER_KEY");
    http.put_OAuthConsumerSecret(L"ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 Authorization
    CkJsonObjectW jsonToken;
    success = jsonToken.LoadFile(L"qa_data/tokens/etrade.json");
    if (success != true) {
        wprintf(L"Failed to load OAuth1 token\n");
        return;
    }

    http.put_OAuthToken(jsonToken.stringOf(L"oauth_token"));
    http.put_OAuthTokenSecret(jsonToken.stringOf(L"oauth_token_secret"));

    const wchar_t *sandboxUrl = L"https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders";
    const wchar_t *liveUrl = L"https://api.etrade.com/v1/accounts/{$accountIdKey}/orders";

    http.SetUrlVar(L"accountIdKey",L"6_Dpy0rmuQ9cu9IbTfvF2A");

    CkHttpResponseW resp;
    success = http.HttpNoBody(L"GET",sandboxUrl,resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    // Make sure a successful response was received.
    if (resp.get_StatusCode() > 200) {
        wprintf(L"%s\n",resp.statusLine());
        wprintf(L"%s\n",resp.header());
        wprintf(L"%s\n",resp.bodyStr());
        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>
    // 

    CkXmlW xml;
    xml.LoadXml(resp.bodyStr());
    wprintf(L"%s\n",xml.getXml());

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    int orderId;
    const wchar_t *details = 0;
    const wchar_t *orderType = 0;
    int j;
    int count_j;
    const wchar_t *placedTime = 0;
    const wchar_t *orderValue = 0;
    const wchar_t *status = 0;
    const wchar_t *orderTerm = 0;
    const wchar_t *priceType = 0;
    const wchar_t *limitPrice = 0;
    int stopPrice;
    const wchar_t *marketSession = 0;
    const wchar_t *allOrNone = 0;
    int k;
    int count_k;
    const wchar_t *symbol = 0;
    const wchar_t *securityType = 0;
    const wchar_t *callPut = 0;
    int expiryYear;
    int expiryMonth;
    int expiryDay;
    const wchar_t *strikePrice = 0;
    const wchar_t *symbolDescription = 0;
    const wchar_t *orderAction = 0;
    const wchar_t *quantityType = 0;
    int orderedQuantity;
    int filledQuantity;
    const wchar_t *averageExecutionPrice = 0;
    const wchar_t *estimatedCommission = 0;
    int estimatedFees;
    int netPrice;
    int netBid;
    int netAsk;
    int gcd;
    int orderNumber;
    int bracketedLimitPrice;
    const wchar_t *initialStopPrice = 0;
    const wchar_t *executedTime = 0;
    const wchar_t *totalOrderValue = 0;
    const wchar_t *totalCommission = 0;

    const wchar_t *marker = xml.getChildContent(L"marker");
    const wchar_t *next = xml.getChildContent(L"next");
    int i = 0;
    int count_i = xml.NumChildrenHavingTag(L"Order");
    while (i < count_i) {
        xml.put_I(i);
        orderId = xml.GetChildIntValue(L"Order[i]|orderId");
        details = xml.getChildContent(L"Order[i]|details");
        orderType = xml.getChildContent(L"Order[i]|orderType");
        j = 0;
        count_j = xml.NumChildrenHavingTag(L"Order[i]|OrderDetail");
        while (j < count_j) {
            xml.put_J(j);
            placedTime = xml.getChildContent(L"Order[i]|OrderDetail[j]|placedTime");
            orderValue = xml.getChildContent(L"Order[i]|OrderDetail[j]|orderValue");
            status = xml.getChildContent(L"Order[i]|OrderDetail[j]|status");
            orderTerm = xml.getChildContent(L"Order[i]|OrderDetail[j]|orderTerm");
            priceType = xml.getChildContent(L"Order[i]|OrderDetail[j]|priceType");
            limitPrice = xml.getChildContent(L"Order[i]|OrderDetail[j]|limitPrice");
            stopPrice = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|stopPrice");
            marketSession = xml.getChildContent(L"Order[i]|OrderDetail[j]|marketSession");
            allOrNone = xml.getChildContent(L"Order[i]|OrderDetail[j]|allOrNone");
            k = 0;
            count_k = xml.NumChildrenHavingTag(L"Order[i]|OrderDetail[j]|Instrument");
            while (k < count_k) {
                xml.put_K(k);
                symbol = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|Product|symbol");
                securityType = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|Product|securityType");
                callPut = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|Product|callPut");
                expiryYear = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryYear");
                expiryMonth = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryMonth");
                expiryDay = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryDay");
                strikePrice = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|Product|strikePrice");
                symbolDescription = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|symbolDescription");
                orderAction = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|orderAction");
                quantityType = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|quantityType");
                orderedQuantity = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|Instrument[k]|orderedQuantity");
                filledQuantity = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|Instrument[k]|filledQuantity");
                averageExecutionPrice = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|averageExecutionPrice");
                estimatedCommission = xml.getChildContent(L"Order[i]|OrderDetail[j]|Instrument[k]|estimatedCommission");
                estimatedFees = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|Instrument[k]|estimatedFees");
                k = k + 1;
            }

            netPrice = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|netPrice");
            netBid = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|netBid");
            netAsk = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|netAsk");
            gcd = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|gcd");
            orderNumber = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|orderNumber");
            bracketedLimitPrice = xml.GetChildIntValue(L"Order[i]|OrderDetail[j]|bracketedLimitPrice");
            initialStopPrice = xml.getChildContent(L"Order[i]|OrderDetail[j]|initialStopPrice");
            executedTime = xml.getChildContent(L"Order[i]|OrderDetail[j]|executedTime");
            j = j + 1;
        }

        totalOrderValue = xml.getChildContent(L"Order[i]|totalOrderValue");
        totalCommission = xml.getChildContent(L"Order[i]|totalCommission");
        i = i + 1;
    }

    wprintf(L"Success.\n");
    }