Sample code for 30+ languages & platforms
Lua

ETrade List Orders

See more ETrade Examples

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

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

http.oauth1 = true
http.oauth_verifier = ""
http.oauth_consumer_key = "ETRADE_CONSUMER_KEY"
http.oauth_consumer_secret = "ETRADE_CONSUMER_SECRET"

-- Load the access token previously obtained via the OAuth1 Authorization
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/etrade.json")
if not success then
    print("Failed to load OAuth1 token")
    return
end

http.oauth_token = jsonToken:string_of("oauth_token")
http.oauth_token_secret = jsonToken:string_of("oauth_token_secret")

local sandboxUrl = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders"
local liveUrl = "https://api.etrade.com/v1/accounts/{$accountIdKey}/orders"

http:set_url_var("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A")

local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", sandboxUrl, resp)
if not success then
    print(http.last_error_text)
    return
end

-- Make sure a successful response was received.
if resp.status_code > 200 then
    print(resp.status_line)
    print(resp.header)
    print(resp.body_str)
    return
end

-- 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>
--

local xml = chilkat.Xml.new()
xml:load_xml(resp.body_str)
print(xml:get_xml())

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

local marker = xml:get_child_content("marker")
local next_ = xml:get_child_content("next")
local i = 0
local count_i = xml:num_children_having_tag("Order")
while i < count_i do
    xml.i = i
    orderId = xml:get_child_int_value("Order[i]|orderId")
    details = xml:get_child_content("Order[i]|details")
    orderType = xml:get_child_content("Order[i]|orderType")
    j = 0
    count_j = xml:num_children_having_tag("Order[i]|OrderDetail")
    while j < count_j do
        xml.j = j
        placedTime = xml:get_child_content("Order[i]|OrderDetail[j]|placedTime")
        orderValue = xml:get_child_content("Order[i]|OrderDetail[j]|orderValue")
        status = xml:get_child_content("Order[i]|OrderDetail[j]|status")
        orderTerm = xml:get_child_content("Order[i]|OrderDetail[j]|orderTerm")
        priceType = xml:get_child_content("Order[i]|OrderDetail[j]|priceType")
        limitPrice = xml:get_child_content("Order[i]|OrderDetail[j]|limitPrice")
        stopPrice = xml:get_child_int_value("Order[i]|OrderDetail[j]|stopPrice")
        marketSession = xml:get_child_content("Order[i]|OrderDetail[j]|marketSession")
        allOrNone = xml:get_child_content("Order[i]|OrderDetail[j]|allOrNone")
        k = 0
        count_k = xml:num_children_having_tag("Order[i]|OrderDetail[j]|Instrument")
        while k < count_k do
            xml.k = k
            symbol = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|Product|symbol")
            securityType = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|Product|securityType")
            callPut = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|Product|callPut")
            expiryYear = xml:get_child_int_value("Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryYear")
            expiryMonth = xml:get_child_int_value("Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryMonth")
            expiryDay = xml:get_child_int_value("Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryDay")
            strikePrice = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|Product|strikePrice")
            symbolDescription = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|symbolDescription")
            orderAction = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|orderAction")
            quantityType = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|quantityType")
            orderedQuantity = xml:get_child_int_value("Order[i]|OrderDetail[j]|Instrument[k]|orderedQuantity")
            filledQuantity = xml:get_child_int_value("Order[i]|OrderDetail[j]|Instrument[k]|filledQuantity")
            averageExecutionPrice = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|averageExecutionPrice")
            estimatedCommission = xml:get_child_content("Order[i]|OrderDetail[j]|Instrument[k]|estimatedCommission")
            estimatedFees = xml:get_child_int_value("Order[i]|OrderDetail[j]|Instrument[k]|estimatedFees")
            k = k + 1
        end

        netPrice = xml:get_child_int_value("Order[i]|OrderDetail[j]|netPrice")
        netBid = xml:get_child_int_value("Order[i]|OrderDetail[j]|netBid")
        netAsk = xml:get_child_int_value("Order[i]|OrderDetail[j]|netAsk")
        gcd = xml:get_child_int_value("Order[i]|OrderDetail[j]|gcd")
        orderNumber = xml:get_child_int_value("Order[i]|OrderDetail[j]|orderNumber")
        bracketedLimitPrice = xml:get_child_int_value("Order[i]|OrderDetail[j]|bracketedLimitPrice")
        initialStopPrice = xml:get_child_content("Order[i]|OrderDetail[j]|initialStopPrice")
        executedTime = xml:get_child_content("Order[i]|OrderDetail[j]|executedTime")
        j = j + 1
    end

    totalOrderValue = xml:get_child_content("Order[i]|totalOrderValue")
    totalCommission = xml:get_child_content("Order[i]|totalCommission")
    i = i + 1
end

print("Success.")