PHP Requires Chilkat v11.0.0+
PHP
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.
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpRequest;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\Secrets;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$vault = new Secrets();
$vault->Location = 'local_manager';
$vault->AutoSplit = true;
$entry = new JsonObject();
$entry->UpdateString('appName', 'SchwabTrader');
$entry->UpdateString('service', 'OAuth2');
$entry->UpdateString('username', 'personal-desktop');
$tokens = new JsonObject();
try {
$vault->GetSecretJson($entry, $tokens);
} catch (\Chilkat2\Exception $e) {
echo "Could not read the token record. Check the store and complete desktop authorization first.\n";
exit;
}
$accessToken = $tokens->StringOf('access_token');
if ($accessToken === '') {
echo "Run the Schwab authorization or refresh example to save an access token.\n";
exit;
}
$http = new Http();
$http->RequireSslCertVerify = true;
$http->AuthToken = $accessToken;
// Read the hash saved by the authorized-account selection example.
$entry->UpdateString('service', 'SelectedAccount');
$selection = new JsonObject();
try {
$vault->GetSecretJson($entry, $selection);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
$accountHash = $selection->StringOf('hashValue');
if ((!$success) || ($accountHash === '')) {
echo "Select an authorized account first.\n";
exit;
}
$http->SetUrlVar('account', $accountHash);
$resp = new HttpResponse();
$query = new HttpRequest();
$query->HttpVerb = 'GET';
$query->AddParam('fields', 'positions');
try {
$http->HttpReq('https://api.schwabapi.com/trader/v1/accounts/{$account}', $query, $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ((!$success) || ($resp->StatusCode != 200)) {
if ($resp->StatusCode == 0) {
echo "No HTTP response. Check connectivity.\n";
} else {
echo 'Schwab returned HTTP ', $resp->StatusCode, "\n";
if ($resp->StatusCode == 401) {
echo "Refresh the access token or repeat desktop authorization.\n";
}
}
exit;
}
$reply = new JsonObject();
try {
$reply->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Schwab returned unexpected JSON.\n";
exit;
}
$balances = new JsonObject();
try {
$reply->ObjectOf2('securitiesAccount.currentBalances', $balances);
} catch (\Chilkat2\Exception $e) {
echo "The account response contained no current balances.\n";
exit;
}
echo 'Cash balance: ', $balances->StringOf('cashBalance'), "\n";
echo 'Liquidation value: ', $balances->StringOf('liquidationValue'), "\n";
if ($balances->HasMember('buyingPower')) {
echo 'Buying power: ', $balances->StringOf('buyingPower'), "\n";
}
$count = $reply->SizeOfArray('securitiesAccount.positions');
if ($count <= 0) {
echo "No positions.\n";
}
$i = 0;
while ($i < $count) {
$reply->I = $i;
echo $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;
}