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

Schwab: List recent orders for one account

See more Schwab Examples

Lists orders entered in the last seven days, with an optional status filter such as WORKING. Shows the order ID, entry time, status, filled quantity, and remaining quantity. Select an ID from the results for the order detail and fills example. An older order can still be working outside this entry-time window.

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

PHP
<?php

use Chilkat2\DateTime;
use Chilkat2\Http;
use Chilkat2\HttpRequest;
use Chilkat2\HttpResponse;
use Chilkat2\JsonArray;
use Chilkat2\JsonObject;
use Chilkat2\Secrets;
use Chilkat2\StringBuilder;

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

// Use UTC endpoints for a rolling seven-day window.
$clock = new DateTime();
$clock->SetFromCurrentSystemTime();
$endTime = new StringBuilder();
$endTime->Append($clock->GetAsTimestamp(false));
$replacements = $endTime->Replace('Z', '.000Z');
$clock->AddDays(-7);
$startTime = new StringBuilder();
$startTime->Append($clock->GetAsTimestamp(false));
$replacements = $startTime->Replace('Z', '.000Z');
$statusFilter = '';
$resp = new HttpResponse();
$query = new HttpRequest();
$query->HttpVerb = 'GET';
$query->AddParam('fromEnteredTime', $startTime->GetAsString());
$query->AddParam('toEnteredTime', $endTime->GetAsString());
$query->AddParam('maxResults', '100');
if ($statusFilter !== '') {
    $query->AddParam('status', $statusFilter);
}

try {
    $http->HttpReq('https://api.schwabapi.com/trader/v1/accounts/{$account}/orders', $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;
}

$orders = new JsonArray();
try {
    $orders->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo "Schwab returned unexpected JSON.\n";
    exit;
}

if ($orders->Size == 0) {
    echo "No matching orders entered during this interval.\n";
}

if ($orders->Size == 100) {
    echo "Reached maxResults. Narrow the interval to check for additional orders.\n";
}

$order = new JsonObject();
$i = 0;
while ($i < $orders->Size) {
    $orders->ObjectAt2($i, $order);
    echo 'Order ', $order->StringOf('orderId'), '  ', $order->StringOf('enteredTime'), '  ', $order->StringOf('orderType'), '  ', $order->StringOf('status'), "\n";
    echo '  filled ', $order->StringOf('filledQuantity'), '  remaining ', $order->StringOf('remainingQuantity'), "\n";
    $i = $i + 1;
}