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

Schwab: Get quotes for a watchlist

See more Schwab Examples

Requests several symbols in one call and prints the bid, ask, last price, quote timestamp in Unix milliseconds, and the real-time indicator when available. Missing quote fields can occur for individual instruments. The response uses symbol names as JSON keys and can report invalid symbols alongside valid quotes.

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 Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# Assumes Chilkat is unlocked. See Global Unlock Sample.
my $success = 0;

my $vault = Chilkat2::Secrets->new;
$vault->Location('local_manager');
$vault->AutoSplit(1);
my $entry = Chilkat2::JsonObject->new;
$entry->UpdateString('appName', 'SchwabTrader');
$entry->UpdateString('service', 'OAuth2');
$entry->UpdateString('username', 'personal-desktop');
my $tokens = Chilkat2::JsonObject->new;
$success = $vault->GetSecretJson($entry, $tokens);
if (!$success) {
    print "Could not read the token record. Check the store and complete desktop authorization first.\n";
    exit;
}

my $accessToken = $tokens->StringOf('access_token');
if ($accessToken eq '') {
    print "Run the Schwab authorization or refresh example to save an access token.\n";
    exit;
}

my $http = Chilkat2::Http->new;
$http->RequireSslCertVerify(1);
$http->AuthToken($accessToken);

my $resp = Chilkat2::HttpResponse->new;
my $query = Chilkat2::HttpRequest->new;
$query->HttpVerb('GET');
$query->AddParam('symbols', 'AAPL,MSFT,BRK/B');
$query->AddParam('fields', 'quote');
$success = $http->HttpReq('https://api.schwabapi.com/marketdata/v1/quotes', $query, $resp);
if ((!$success) || ($resp->StatusCode != 200)) {
    if ($resp->StatusCode == 0) {
        print "No HTTP response. Check connectivity.\n";
    } else {
        print 'Schwab returned HTTP ', $resp->StatusCode, "\n";
        if ($resp->StatusCode == 401) {
            print "Refresh the access token or repeat desktop authorization.\n";
        }
    }

    exit;
}

my $reply = Chilkat2::JsonObject->new;
$success = $reply->Load($resp->BodyStr);
if (!$success) {
    print "Schwab returned unexpected JSON.\n";
    exit;
}

# Read members by index. A symbol may contain punctuation meaningful in a JSON path.
my $instrument = Chilkat2::JsonObject->new;
my $i = 0;
while ($i < $reply->Size) {
    my $key = $reply->NameAt($i);
    if ($key ne 'errors') {
        $success = $reply->ObjectAt2($i, $instrument);
        if ($success) {
            print $key, '  bid ', $instrument->StringOf('quote.bidPrice'), '  ask ', $instrument->StringOf('quote.askPrice'), '  last ', $instrument->StringOf('quote.lastPrice'), "\n";
            print '  quote time (Unix ms): ', $instrument->StringOf('quote.quoteTime'), '  real-time: ', $instrument->StringOf('realtime'), "\n";
        }
    }

    $i = $i + 1;
}

my $j = 0;
while ($j < $reply->SizeOfArray('errors.invalidSymbols')) {
    $reply->J($j);
    print 'Invalid symbol: ', $reply->StringOf('errors.invalidSymbols[j]'), "\n";
    $j = $j + 1;
}