PHP ActiveX Requires Chilkat v11.0.0+
PHP ActiveX
Schwab: Preview a single-leg equity limit order
See more Schwab Examples
Builds a day limit order for one share and posts it to previewOrder. Reads the projected order and the validation messages, including warnings and rejection reasons. A successful HTTP response means the preview completed. A preview does not submit an order, reserve buying power, or guarantee later acceptance.
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 ActiveX Downloads
<?php
$success = 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$vault = new COM("Chilkat.Secrets");
$vault->Location = 'local_manager';
$vault->AutoSplit = 1;
$entry = new COM("Chilkat.JsonObject");
$entry->UpdateString('appName','SchwabTrader');
$entry->UpdateString('service','OAuth2');
$entry->UpdateString('username','personal-desktop');
$tokens = new COM("Chilkat.JsonObject");
$success = $vault->GetSecretJson($entry,$tokens);
if ($success != 1) {
print 'Could not read the token record. Check the store and complete desktop authorization first.' . "\n";
exit;
}
$accessToken = $tokens->stringOf('access_token');
if ($accessToken == '') {
print 'Run the Schwab authorization or refresh example to save an access token.' . "\n";
exit;
}
$http = new COM("Chilkat.Http");
$http->RequireSslCertVerify = 1;
$http->AuthToken = $accessToken;
// Read the hash saved by the authorized-account selection example.
$entry->UpdateString('service','SelectedAccount');
$selection = new COM("Chilkat.JsonObject");
$success = $vault->GetSecretJson($entry,$selection);
$accountHash = $selection->stringOf('hashValue');
if (($success != 1) or ($accountHash == '')) {
print 'Select an authorized account first.' . "\n";
exit;
}
$http->SetUrlVar('account',$accountHash);
// These are illustrative preview inputs. Set the symbol, quantity and limit price.
$order = new COM("Chilkat.JsonObject");
// The PreviewOrder schema puts orderLegs inside orderStrategy.
$order->UpdateString('orderStrategy.session','NORMAL');
$order->UpdateString('orderStrategy.duration','DAY');
$order->UpdateString('orderStrategy.orderType','LIMIT');
$order->UpdateString('orderStrategy.orderStrategyType','SINGLE');
$order->UpdateNumber('orderStrategy.price','100.00');
$order->UpdateInt('orderStrategy.quantity',1);
$order->UpdateString('orderStrategy.orderLegs[0].instruction','BUY');
$order->UpdateInt('orderStrategy.orderLegs[0].quantity',1);
$order->UpdateString('orderStrategy.orderLegs[0].finalSymbol','AAPL');
$order->UpdateString('orderStrategy.orderLegs[0].assetType','EQUITY');
$resp = new COM("Chilkat.HttpResponse");
$success = $http->HttpJson('POST','https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder',$order,'application/json',$resp);
if (($success != 1) or ($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;
}
$reply = new COM("Chilkat.JsonObject");
$success = $reply->Load($resp->BodyStr);
if ($success != 1) {
print 'Schwab returned unexpected JSON.' . "\n";
exit;
}
print 'Preview only. No order was submitted.' . "\n";
if ($reply->HasMember('orderStrategy.orderType') == 1) {
print 'Projected order: ' . $reply->stringOf('orderStrategy.orderType') . ' price ' . $reply->stringOf('orderStrategy.price') . "\n";
}
if ($reply->HasMember('orderStrategy.orderBalance.projectedBuyingPower') == 1) {
print 'Projected buying power: ' . $reply->stringOf('orderStrategy.orderBalance.projectedBuyingPower') . "\n";
}
$validation = new COM("Chilkat.JsonObject");
$success = $reply->ObjectOf2('orderValidationResult',$validation);
if ($success != 1) {
print 'Schwab supplied no validation result.' . "\n";
exit;
}
// Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
$rules = new COM("Chilkat.JsonArray");
$detail = new COM("Chilkat.JsonObject");
$messages = 0;
$i = 0;
while (($i < $validation->Size)) {
$category = $validation->nameAt($i);
$success = $validation->ArrayAt2($i,$rules);
if ($success == 1) {
$j = 0;
while (($j < $rules->Size)) {
$rules->ObjectAt2($j,$detail);
print $category . ' ' . $detail->stringOf('validationRuleName') . ' ' . $detail->stringOf('message') . ' ' . $detail->stringOf('activityMessage') . "\n";
$messages = $messages + 1;
$j = $j + 1;
}
}
$i = $i + 1;
}
if ($messages == 0) {
print 'No validation messages returned.' . "\n";
}
?>