Sample code for 30+ languages & platforms
Objective-C

ETrade List Orders

See more ETrade Examples

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

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttpResponse.h>
#import <CkoXml.h>

BOOL success = NO;

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

CkoHttp *http = [[CkoHttp alloc] init];

http.OAuth1 = YES;
http.OAuthVerifier = @"";
http.OAuthConsumerKey = @"ETRADE_CONSUMER_KEY";
http.OAuthConsumerSecret = @"ETRADE_CONSUMER_SECRET";

//  Load the access token previously obtained via the OAuth1 Authorization
CkoJsonObject *jsonToken = [[CkoJsonObject alloc] init];
success = [jsonToken LoadFile: @"qa_data/tokens/etrade.json"];
if (success != YES) {
    NSLog(@"%@",@"Failed to load OAuth1 token");
    return;
}

http.OAuthToken = [jsonToken StringOf: @"oauth_token"];
http.OAuthTokenSecret = [jsonToken StringOf: @"oauth_token_secret"];

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

[http SetUrlVar: @"accountIdKey" value: @"6_Dpy0rmuQ9cu9IbTfvF2A"];

CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpNoBody: @"GET" url: sandboxUrl response: resp];
if (success == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

//  Make sure a successful response was received.
if ([resp.StatusCode intValue] > 200) {
    NSLog(@"%@",resp.StatusLine);
    NSLog(@"%@",resp.Header);
    NSLog(@"%@",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>
//  

CkoXml *xml = [[CkoXml alloc] init];
[xml LoadXml: resp.BodyStr];
NSLog(@"%@",[xml GetXml]);

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

NSString *marker = [xml GetChildContent: @"marker"];
NSString *next = [xml GetChildContent: @"next"];
int i = 0;
int count_i = [[xml NumChildrenHavingTag: @"Order"] intValue];
while (i < count_i) {
    xml.I = [NSNumber numberWithInt: i];
    orderId = [[xml GetChildIntValue: @"Order[i]|orderId"] intValue];
    details = [xml GetChildContent: @"Order[i]|details"];
    orderType = [xml GetChildContent: @"Order[i]|orderType"];
    j = 0;
    count_j = [[xml NumChildrenHavingTag: @"Order[i]|OrderDetail"] intValue];
    while (j < count_j) {
        xml.J = [NSNumber numberWithInt: j];
        placedTime = [xml GetChildContent: @"Order[i]|OrderDetail[j]|placedTime"];
        orderValue = [xml GetChildContent: @"Order[i]|OrderDetail[j]|orderValue"];
        status = [xml GetChildContent: @"Order[i]|OrderDetail[j]|status"];
        orderTerm = [xml GetChildContent: @"Order[i]|OrderDetail[j]|orderTerm"];
        priceType = [xml GetChildContent: @"Order[i]|OrderDetail[j]|priceType"];
        limitPrice = [xml GetChildContent: @"Order[i]|OrderDetail[j]|limitPrice"];
        stopPrice = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|stopPrice"] intValue];
        marketSession = [xml GetChildContent: @"Order[i]|OrderDetail[j]|marketSession"];
        allOrNone = [xml GetChildContent: @"Order[i]|OrderDetail[j]|allOrNone"];
        k = 0;
        count_k = [[xml NumChildrenHavingTag: @"Order[i]|OrderDetail[j]|Instrument"] intValue];
        while (k < count_k) {
            xml.K = [NSNumber numberWithInt: k];
            symbol = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|Product|symbol"];
            securityType = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|Product|securityType"];
            callPut = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|Product|callPut"];
            expiryYear = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryYear"] intValue];
            expiryMonth = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryMonth"] intValue];
            expiryDay = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|Instrument[k]|Product|expiryDay"] intValue];
            strikePrice = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|Product|strikePrice"];
            symbolDescription = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|symbolDescription"];
            orderAction = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|orderAction"];
            quantityType = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|quantityType"];
            orderedQuantity = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|Instrument[k]|orderedQuantity"] intValue];
            filledQuantity = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|Instrument[k]|filledQuantity"] intValue];
            averageExecutionPrice = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|averageExecutionPrice"];
            estimatedCommission = [xml GetChildContent: @"Order[i]|OrderDetail[j]|Instrument[k]|estimatedCommission"];
            estimatedFees = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|Instrument[k]|estimatedFees"] intValue];
            k = k + 1;
        }

        netPrice = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|netPrice"] intValue];
        netBid = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|netBid"] intValue];
        netAsk = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|netAsk"] intValue];
        gcd = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|gcd"] intValue];
        orderNumber = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|orderNumber"] intValue];
        bracketedLimitPrice = [[xml GetChildIntValue: @"Order[i]|OrderDetail[j]|bracketedLimitPrice"] intValue];
        initialStopPrice = [xml GetChildContent: @"Order[i]|OrderDetail[j]|initialStopPrice"];
        executedTime = [xml GetChildContent: @"Order[i]|OrderDetail[j]|executedTime"];
        j = j + 1;
    }

    totalOrderValue = [xml GetChildContent: @"Order[i]|totalOrderValue"];
    totalCommission = [xml GetChildContent: @"Order[i]|totalCommission"];
    i = i + 1;
}

NSLog(@"%@",@"Success.");