Sample code for 30+ languages & platforms
Dart

ETrade Cancel Order

See more ETrade Examples

The cancel order API is used to cancel an existing order.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final http = CkHttp();

  http.oAuth1 = true;
  http.oAuthVerifier = '';
  http.oAuthConsumerKey = 'ETRADE_CONSUMER_KEY';
  http.oAuthConsumerSecret = 'ETRADE_CONSUMER_SECRET';

  // Load the access token previously obtained via the OAuth1 Authorization
  final jsonToken = CkJsonObject();
  try {
    jsonToken.loadFile('qa_data/tokens/etrade.json');
  } on ChilkatException {
    print('Failed to load OAuth1 token');
    return;
  }

  http.oAuthToken = jsonToken.stringOf('oauth_token');
  http.oAuthTokenSecret = jsonToken.stringOf('oauth_token_secret');

  final sandboxUrl = 'https://apisb.etrade.com/v1/accounts/{\$accountIdKey}/orders/cancel';
  final liveUrl = 'https://api.etrade.com/v1/accounts/{\$accountIdKey}/orders/cancel';

  http.setUrlVar('accountIdKey', '6_Dpy0rmuQ9cu9IbTfvF2A');

  // Send a PUT with the following XML body

  // Use this online tool to generate the code from sample XML: 
  // Generate Code to Create XML

  // <CancelOrderRequest>
  //    <orderId>11</orderId>
  // </CancelOrderRequest>

  final xml = CkXml();
  xml.tag = 'CancelOrderRequest';
  xml.updateChildContent('orderId', '11');
  xml.emitCompact = true;

  final httpRequestBody = xml.getXml();
  final resp = CkHttpResponse();
  try {
    http.httpStr('PUT', sandboxUrl, httpRequestBody, 'utf-8', 'application/xml', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Make sure a successful response was received.
  if (resp.statusCode > 200) {
    print(resp.statusLine);
    print(resp.header);
    print(resp.bodyStr);
    return;
  }

  // Sample XML response:

  // Use this online tool to generate parsing code from sample XML: 
  // Generate Parsing Code from XML

  // <CancelOrderResponse>
  //    <accountId>63438617</accountId>
  //    <orderId>11</orderId>
  //    <cancelTime>1529563499081</cancelTime>
  //    <Messages>
  //       <Message>
  //          <code>5011</code>
  //          <description>200|Your request to cancel your order is being processed.</description>
  //          <type>WARNING</type>
  //       </Message>
  //    </Messages>
  // </CancelOrderResponse>

  xml.loadXml(resp.bodyStr);
  print(xml.getXml());

  final accountId = xml.getChildIntValue('accountId');
  final orderId = xml.getChildIntValue('orderId');
  final cancelTime = xml.getChildContent('cancelTime');
  final code = xml.getChildIntValue('Messages|Message|code');
  final description = xml.getChildContent('Messages|Message|description');
  final vType = xml.getChildContent('Messages|Message|type');

  print('Success.');
}