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

Schwab: Find an option contract by expiration and strike

See more Schwab Examples

Filters a chain by underlying, call or put, exact expiration, and strike. Choose a currently listed expiration and strike. Schwab groups contracts by expiration and strike and may return multiple deliverables. Compare the descriptions and use the exact returned contract symbol, including its spaces, for a later quote request.

Use the access token saved by desktop authorization. Enable the Market Data API for the Schwab app. 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;

  final underlying = 'AAPL';
  final expiration = '2027-01-15';
  final strike = '250';
  final side = 'CALL';
  final resp = CkHttpResponse();
  final query = CkHttpRequest();
  query.httpVerb = 'GET';
  query.addParam('symbol', underlying);
  query.addParam('contractType', side);
  query.addParam('strike', strike);
  query.addParam('fromDate', expiration);
  query.addParam('toDate', expiration);
  success = true;
  try {
    http.httpReq('https://api.schwabapi.com/marketdata/v1/chains', query, 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 reply = CkJsonObject();
  try {
    reply.load(resp.bodyStr);
  } on ChilkatException {
    print('Schwab returned unexpected JSON.');
    return;
  }

  var mapName = 'callExpDateMap';
  if (side == 'PUT') {
    mapName = 'putExpDateMap';
  }

  final expirations = CkJsonObject();
  try {
    reply.objectOf2(mapName, expirations);
  } on ChilkatException {
    print('No option map returned. Chain status: ${reply.stringOf('status')}');
    return;
  }

  // Expiration keys contain a day count. Strike keys may contain a decimal point.
  final strikes = CkJsonObject();
  final contracts = CkJsonArray();
  final contract = CkJsonObject();
  var found = 0;
  var i = 0;
  while (i < expirations.size) {
    expirations.objectAt2(i, strikes);
    var j = 0;
    while (j < strikes.size) {
      strikes.arrayAt2(j, contracts);
      var k = 0;
      while (k < contracts.size) {
        contracts.objectAt2(k, contract);
        print('${contract.stringOf('description')}  strike ${contract.stringOf('strikePrice')}');
        print('Contract symbol: [${contract.stringOf('symbol')}]');
        found++;
        k++;
      }

      j++;
    }

    i++;
  }

  if (found == 0) {
    print('No contracts matched the requested expiration, side and strike.');
  }
}