Sample code for 30+ languages & platforms
Tcl

ETrade List Orders

See more ETrade Examples

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

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set http [new_CkHttp]

CkHttp_put_OAuth1 $http 1
CkHttp_put_OAuthVerifier $http ""
CkHttp_put_OAuthConsumerKey $http "ETRADE_CONSUMER_KEY"
CkHttp_put_OAuthConsumerSecret $http "ETRADE_CONSUMER_SECRET"

# Load the access token previously obtained via the OAuth1 Authorization
set jsonToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/etrade.json"]
if {$success != 1} then {
    puts "Failed to load OAuth1 token"
    delete_CkHttp $http
    delete_CkJsonObject $jsonToken
    exit
}

CkHttp_put_OAuthToken $http [CkJsonObject_stringOf $jsonToken "oauth_token"]
CkHttp_put_OAuthTokenSecret $http [CkJsonObject_stringOf $jsonToken "oauth_token_secret"]

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

CkHttp_SetUrlVar $http "accountIdKey" "6_Dpy0rmuQ9cu9IbTfvF2A"

set resp [new_CkHttpResponse]

set success [CkHttp_HttpNoBody $http "GET" $sandboxUrl $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkJsonObject $jsonToken
    delete_CkHttpResponse $resp
    exit
}

# Make sure a successful response was received.
if {[CkHttpResponse_get_StatusCode $resp] > 200} then {
    puts [CkHttpResponse_statusLine $resp]
    puts [CkHttpResponse_header $resp]
    puts [CkHttpResponse_bodyStr $resp]
    delete_CkHttp $http
    delete_CkJsonObject $jsonToken
    delete_CkHttpResponse $resp
    exit
}

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

set xml [new_CkXml]

CkXml_LoadXml $xml [CkHttpResponse_bodyStr $resp]
puts [CkXml_getXml $xml]

set marker [CkXml_getChildContent $xml "marker"]
set next [CkXml_getChildContent $xml "next"]
set i 0
set count_i [CkXml_NumChildrenHavingTag $xml "Order"]
while {$i < $count_i} {
    CkXml_put_I $xml $i
    set orderId [CkXml_GetChildIntValue $xml "Order[i]|orderId"]
    set details [CkXml_getChildContent $xml "Order[i]|details"]
    set orderType [CkXml_getChildContent $xml "Order[i]|orderType"]
    set j 0
    set count_j [CkXml_NumChildrenHavingTag $xml "Order[i]|OrderDetail"]
    while {$j < $count_j} {
        CkXml_put_J $xml $j
        set placedTime [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|placedTime"]
        set orderValue [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|orderValue"]
        set status [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|status"]
        set orderTerm [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|orderTerm"]
        set priceType [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|priceType"]
        set limitPrice [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|limitPrice"]
        set stopPrice [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|stopPrice"]
        set marketSession [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|marketSession"]
        set allOrNone [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|allOrNone"]
        set k 0
        set count_k [CkXml_NumChildrenHavingTag $xml "Order[i]|OrderDetail[j]|Instrument"]
        while {$k < $count_k} {
            CkXml_put_K $xml $k
            set symbol [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|Product|symbol"]
            set securityType [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|Product|securityType"]
            set callPut [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|Product|callPut"]
            set expiryYear [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryYear"]
            set expiryMonth [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryMonth"]
            set expiryDay [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryDay"]
            set strikePrice [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|Product|strikePrice"]
            set symbolDescription [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|symbolDescription"]
            set orderAction [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|orderAction"]
            set quantityType [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|quantityType"]
            set orderedQuantity [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|Instrument[k]|orderedQuantity"]
            set filledQuantity [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|Instrument[k]|filledQuantity"]
            set averageExecutionPrice [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|averageExecutionPrice"]
            set estimatedCommission [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|Instrument[k]|estimatedCommission"]
            set estimatedFees [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|Instrument[k]|estimatedFees"]
            set k [expr $k + 1]
        }
        set netPrice [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|netPrice"]
        set netBid [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|netBid"]
        set netAsk [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|netAsk"]
        set gcd [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|gcd"]
        set orderNumber [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|orderNumber"]
        set bracketedLimitPrice [CkXml_GetChildIntValue $xml "Order[i]|OrderDetail[j]|bracketedLimitPrice"]
        set initialStopPrice [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|initialStopPrice"]
        set executedTime [CkXml_getChildContent $xml "Order[i]|OrderDetail[j]|executedTime"]
        set j [expr $j + 1]
    }
    set totalOrderValue [CkXml_getChildContent $xml "Order[i]|totalOrderValue"]
    set totalCommission [CkXml_getChildContent $xml "Order[i]|totalCommission"]
    set i [expr $i + 1]
}

puts "Success."

delete_CkHttp $http
delete_CkJsonObject $jsonToken
delete_CkHttpResponse $resp
delete_CkXml $xml