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

Schwab: Check equity market hours for a date

See more Schwab Examples

Looks up the US equity calendar for a selected trading date. Reads each product’s open flag and the actual regular-session start and end, including their offsets. This distinguishes a holiday closure from a shortened session. The open flag describes the requested date, not whether the market is open at this instant.

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;

  // Supply the calendar date you want to check, for example a US holiday.
  final clock = CkDateTime();
  clock.setFromCurrentSystemTime();
  final marketDate = clock.getAsIso8601('YYYY-MM-DD', false);
  final resp = CkHttpResponse();
  final query = CkHttpRequest();
  query.httpVerb = 'GET';
  query.addParam('date', marketDate);
  success = true;
  try {
    http.httpReq('https://api.schwabapi.com/marketdata/v1/markets/equity', 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;
  }

  final products = CkJsonObject();
  success = true;
  try {
    reply.objectOf2('equity', products);
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (products.size == 0)) {
    print('No equity market schedule returned for $marketDate');
    return;
  }

  final product = CkJsonObject();
  var i = 0;
  while (i < products.size) {
    products.objectAt2(i, product);
    print('${products.nameAt(i)}  ${product.stringOf('date')}');
    if (product.boolOf('isOpen')) {
      var j = 0;
      final count = product.sizeOfArray('sessionHours.regularMarket');
      if (count <= 0) {
        print('Open date, but no regular-session times were supplied.');
      }

      while (j < count) {
        product.j = j;
        print('Regular session: ${product.stringOf('sessionHours.regularMarket[j].start')} to ${product.stringOf('sessionHours.regularMarket[j].end')}');
        j++;
      }
    } else {
      print('Closed for this date.');
    }

    i++;
  }
}