Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Schwab: Inspect an order, its legs, and fills

See more Schwab Examples

Use an order ID from the selected account’s recent orders. The example reads a single order and matches executions to order legs by legId. Each execution can have its own quantity, price, and timestamp. Working orders can have zero or partial fills. The loops cover the returned order’s own legs and activities.

Use the access token saved by desktop authorization. First select the authorized account whose data you want to use. Use the same Chilkat Secrets store as the OAuth example.

Schwab API specification

Chilkat Dart Downloads

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

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  success = false;

  final vault = CkSecrets();
  vault.location = 'local_manager';
  vault.autoSplit = true;
  final entry = CkJsonObject();
  entry.updateString('appName', 'SchwabTrader');
  entry.updateString('service', 'OAuth2');
  entry.updateString('username', 'personal-desktop');
  final tokens = CkJsonObject();
  try {
    vault.getSecretJson(entry, tokens);
  } on ChilkatException {
    print('Could not read the token record. Check the store and complete desktop authorization first.');
    return;
  }

  final accessToken = tokens.stringOf('access_token');
  if (accessToken == '') {
    print('Run the Schwab authorization or refresh example to save an access token.');
    return;
  }

  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.authToken = accessToken;

  // Read the hash saved by the authorized-account selection example.
  entry.updateString('service', 'SelectedAccount');
  final selection = CkJsonObject();
  success = true;
  try {
    vault.getSecretJson(entry, selection);
  } on ChilkatException {
    success = false;
  }
  final accountHash = selection.stringOf('hashValue');
  if ((!success) || (accountHash == '')) {
    print('Select an authorized account first.');
    return;
  }

  http.setUrlVar('account', accountHash);

  // Supply the ID as text so large numeric identifiers keep all their digits.
  final orderId = 'ORDER_ID_FROM_LIST';
  http.setUrlVar('orderId', orderId);
  final resp = CkHttpResponse();
  success = true;
  try {
    http.httpNoBody('GET', 'https://api.schwabapi.com/trader/v1/accounts/{\$account}/orders/{\$orderId}', resp);
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (resp.statusCode != 200)) {
    if (resp.statusCode == 0) {
      print('No HTTP response. Check connectivity.');
    } else {
      print('Schwab returned HTTP ${resp.statusCode}');
      if (resp.statusCode == 401) {
        print('Refresh the access token or repeat desktop authorization.');
      }
    }

    return;
  }

  final order = CkJsonObject();
  try {
    order.load(resp.bodyStr);
  } on ChilkatException {
    print('Schwab returned unexpected JSON.');
    return;
  }

  print('Order ${order.stringOf('orderId')}  ${order.stringOf('status')}');
  final symbols = CkJsonObject();
  var i = 0;
  while (i < order.sizeOfArray('orderLegCollection')) {
    order.i = i;
    final legId = order.stringOf('orderLegCollection[i].legId');
    final symbol = order.stringOf('orderLegCollection[i].instrument.symbol');
    symbols.updateString(legId, symbol);
    print('Leg $legId  ${order.stringOf('orderLegCollection[i].instruction')} ${order.stringOf('orderLegCollection[i].quantity')} $symbol');
    i++;
  }

  var fills = 0;
  i = 0;
  while (i < order.sizeOfArray('orderActivityCollection')) {
    order.i = i;
    if (order.stringOfEquals('orderActivityCollection[i].executionType', 'FILL', true)) {
      var j = 0;
      while (j < order.sizeOfArray('orderActivityCollection[i].executionLegs')) {
        order.j = j;
        final filledLeg = order.stringOf('orderActivityCollection[i].executionLegs[j].legId');
        print('Fill on leg $filledLeg  ${symbols.stringOf(filledLeg)}  quantity ${order.stringOf('orderActivityCollection[i].executionLegs[j].quantity')}  price ${order.stringOf('orderActivityCollection[i].executionLegs[j].price')}  time ${order.stringOf('orderActivityCollection[i].executionLegs[j].time')}');
        fills++;
        j++;
      }
    }

    i++;
  }

  if (fills == 0) {
    print('No fills reported for this order.');
  }
}