Unicode C Requires Chilkat v11.0.0+
Unicode C
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 Unicode C Downloads
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonArrayW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSecretsW vault;
HCkJsonObjectW entry;
HCkJsonObjectW tokens;
const wchar_t *accessToken;
HCkHttpW http;
HCkJsonObjectW selection;
const wchar_t *accountHash;
HCkJsonObjectW order;
HCkHttpResponseW resp;
HCkJsonObjectW reply;
HCkJsonObjectW validation;
HCkJsonArrayW rules;
HCkJsonObjectW detail;
int messages;
int i;
const wchar_t *category;
int j;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
vault = CkSecretsW_Create();
CkSecretsW_putLocation(vault,L"local_manager");
CkSecretsW_putAutoSplit(vault,TRUE);
entry = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(entry,L"appName",L"SchwabTrader");
CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
CkJsonObjectW_UpdateString(entry,L"username",L"personal-desktop");
tokens = CkJsonObjectW_Create();
success = CkSecretsW_GetSecretJson(vault,entry,tokens);
if (success != TRUE) {
wprintf(L"Could not read the token record. Check the store and complete desktop authorization first.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
return;
}
accessToken = CkJsonObjectW_stringOf(tokens,L"access_token");
if (accessToken == L"") {
wprintf(L"Run the Schwab authorization or refresh example to save an access token.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
return;
}
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
CkHttpW_putAuthToken(http,accessToken);
// Read the hash saved by the authorized-account selection example.
CkJsonObjectW_UpdateString(entry,L"service",L"SelectedAccount");
selection = CkJsonObjectW_Create();
success = CkSecretsW_GetSecretJson(vault,entry,selection);
accountHash = CkJsonObjectW_stringOf(selection,L"hashValue");
if ((success != TRUE) || (accountHash == L"")) {
wprintf(L"Select an authorized account first.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(selection);
return;
}
CkHttpW_SetUrlVar(http,L"account",accountHash);
// These are illustrative preview inputs. Set the symbol, quantity and limit price.
order = CkJsonObjectW_Create();
// The PreviewOrder schema puts orderLegs inside orderStrategy.
CkJsonObjectW_UpdateString(order,L"orderStrategy.session",L"NORMAL");
CkJsonObjectW_UpdateString(order,L"orderStrategy.duration",L"DAY");
CkJsonObjectW_UpdateString(order,L"orderStrategy.orderType",L"LIMIT");
CkJsonObjectW_UpdateString(order,L"orderStrategy.orderStrategyType",L"SINGLE");
CkJsonObjectW_UpdateNumber(order,L"orderStrategy.price",L"100.00");
CkJsonObjectW_UpdateInt(order,L"orderStrategy.quantity",1);
CkJsonObjectW_UpdateString(order,L"orderStrategy.orderLegs[0].instruction",L"BUY");
CkJsonObjectW_UpdateInt(order,L"orderStrategy.orderLegs[0].quantity",1);
CkJsonObjectW_UpdateString(order,L"orderStrategy.orderLegs[0].finalSymbol",L"AAPL");
CkJsonObjectW_UpdateString(order,L"orderStrategy.orderLegs[0].assetType",L"EQUITY");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",order,L"application/json",resp);
if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) != 200)) {
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"No HTTP response. Check connectivity.\n");
}
else {
wprintf(L"Schwab returned HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
if (CkHttpResponseW_getStatusCode(resp) == 401) {
wprintf(L"Refresh the access token or repeat desktop authorization.\n");
}
}
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(selection);
CkJsonObjectW_Dispose(order);
CkHttpResponseW_Dispose(resp);
return;
}
reply = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Schwab returned unexpected JSON.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(selection);
CkJsonObjectW_Dispose(order);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
return;
}
wprintf(L"Preview only. No order was submitted.\n");
if (CkJsonObjectW_HasMember(reply,L"orderStrategy.orderType") == TRUE) {
wprintf(L"Projected order: %s price %s\n",CkJsonObjectW_stringOf(reply,L"orderStrategy.orderType"),CkJsonObjectW_stringOf(reply,L"orderStrategy.price"));
}
if (CkJsonObjectW_HasMember(reply,L"orderStrategy.orderBalance.projectedBuyingPower") == TRUE) {
wprintf(L"Projected buying power: %s\n",CkJsonObjectW_stringOf(reply,L"orderStrategy.orderBalance.projectedBuyingPower"));
}
validation = CkJsonObjectW_Create();
success = CkJsonObjectW_ObjectOf2(reply,L"orderValidationResult",validation);
if (success != TRUE) {
wprintf(L"Schwab supplied no validation result.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(selection);
CkJsonObjectW_Dispose(order);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkJsonObjectW_Dispose(validation);
return;
}
// Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
rules = CkJsonArrayW_Create();
detail = CkJsonObjectW_Create();
messages = 0;
i = 0;
while ((i < CkJsonObjectW_getSize(validation))) {
category = CkJsonObjectW_nameAt(validation,i);
success = CkJsonObjectW_ArrayAt2(validation,i,rules);
if (success == TRUE) {
j = 0;
while ((j < CkJsonArrayW_getSize(rules))) {
CkJsonArrayW_ObjectAt2(rules,j,detail);
wprintf(L"%s %s %s %s\n",category,CkJsonObjectW_stringOf(detail,L"validationRuleName"),CkJsonObjectW_stringOf(detail,L"message"),CkJsonObjectW_stringOf(detail,L"activityMessage"));
messages = messages + 1;
j = j + 1;
}
}
i = i + 1;
}
if (messages == 0) {
wprintf(L"No validation messages returned.\n");
}
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(selection);
CkJsonObjectW_Dispose(order);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkJsonObjectW_Dispose(validation);
CkJsonArrayW_Dispose(rules);
CkJsonObjectW_Dispose(detail);
}