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

Schwab: Read account balances and positions

See more Schwab Examples

Requests fields=positions for the selected account. Reads current cash and liquidation value, prints buying power when supplied, and reports each holding’s symbol, long and short quantities, and market value. A cash-only portfolio can have balances and zero positions.

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 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);

# Read the hash saved by the authorized-account selection example.
$entry->UpdateString('service', 'SelectedAccount');
my $selection = Chilkat2::JsonObject->new;
$success = $vault->GetSecretJson($entry, $selection);
my $accountHash = $selection->StringOf('hashValue');
if ((!$success) || ($accountHash eq '')) {
    print "Select an authorized account first.\n";
    exit;
}

$http->SetUrlVar('account', $accountHash);

my $resp = Chilkat2::HttpResponse->new;
my $query = Chilkat2::HttpRequest->new;
$query->HttpVerb('GET');
$query->AddParam('fields', 'positions');
$success = $http->HttpReq('https://api.schwabapi.com/trader/v1/accounts/{$account}', $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;
}

my $balances = Chilkat2::JsonObject->new;
$success = $reply->ObjectOf2('securitiesAccount.currentBalances', $balances);
if (!$success) {
    print "The account response contained no current balances.\n";
    exit;
}

print 'Cash balance: ', $balances->StringOf('cashBalance'), "\n";
print 'Liquidation value: ', $balances->StringOf('liquidationValue'), "\n";
if ($balances->HasMember('buyingPower')) {
    print 'Buying power: ', $balances->StringOf('buyingPower'), "\n";
}

my $count = $reply->SizeOfArray('securitiesAccount.positions');
if ($count <= 0) {
    print "No positions.\n";
}

my $i = 0;
while ($i < $count) {
    $reply->I($i);
    print $reply->StringOf('securitiesAccount.positions[i].instrument.symbol'), '  long ', $reply->StringOf('securitiesAccount.positions[i].longQuantity'), '  short ', $reply->StringOf('securitiesAccount.positions[i].shortQuantity'), '  market value ', $reply->StringOf('securitiesAccount.positions[i].marketValue'), "\n";
    $i = $i + 1;
}