CkPhp Requires Chilkat v11.0.0+
CkPhp
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 CkPhp Downloads
<?php
include("chilkat.php");
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$vault = new CkSecrets();
$vault->put_Location('local_manager');
$vault->put_AutoSplit(true);
$entry = new CkJsonObject();
$entry->UpdateString('appName','SchwabTrader');
$entry->UpdateString('service','OAuth2');
$entry->UpdateString('username','personal-desktop');
$tokens = new CkJsonObject();
$success = $vault->GetSecretJson($entry,$tokens);
if ($success != true) {
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 CkHttp();
$http->put_RequireSslCertVerify(true);
$http->put_AuthToken($accessToken);
// Read the hash saved by the authorized-account selection example.
$entry->UpdateString('service','SelectedAccount');
$selection = new CkJsonObject();
$success = $vault->GetSecretJson($entry,$selection);
$accountHash = $selection->stringOf('hashValue');
if (($success != true) 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 CkJsonObject();
// 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 CkHttpResponse();
$success = $http->HttpJson('POST','https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder',$order,'application/json',$resp);
if (($success != true) or ($resp->get_StatusCode() != 200)) {
if ($resp->get_StatusCode() == 0) {
print 'No HTTP response. Check connectivity.' . "\n";
}
else {
print 'Schwab returned HTTP ' . $resp->get_StatusCode() . "\n";
if ($resp->get_StatusCode() == 401) {
print 'Refresh the access token or repeat desktop authorization.' . "\n";
}
}
exit;
}
$reply = new CkJsonObject();
$success = $reply->Load($resp->bodyStr());
if ($success != true) {
print 'Schwab returned unexpected JSON.' . "\n";
exit;
}
print 'Preview only. No order was submitted.' . "\n";
if ($reply->HasMember('orderStrategy.orderType') == true) {
print 'Projected order: ' . $reply->stringOf('orderStrategy.orderType') . ' price ' . $reply->stringOf('orderStrategy.price') . "\n";
}
if ($reply->HasMember('orderStrategy.orderBalance.projectedBuyingPower') == true) {
print 'Projected buying power: ' . $reply->stringOf('orderStrategy.orderBalance.projectedBuyingPower') . "\n";
}
$validation = new CkJsonObject();
$success = $reply->ObjectOf2('orderValidationResult',$validation);
if ($success != true) {
print 'Schwab supplied no validation result.' . "\n";
exit;
}
// Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
$rules = new CkJsonArray();
$detail = new CkJsonObject();
$messages = 0;
$i = 0;
while (($i < $validation->get_Size())) {
$category = $validation->nameAt($i);
$success = $validation->ArrayAt2($i,$rules);
if ($success == true) {
$j = 0;
while (($j < $rules->get_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";
}
?>