Unicode C Requires Chilkat v11.0.0+
Unicode C
Schwab: Inspect an order, its legs, and fills
See more Schwab Examples
Use an order ID from the selected account’s recent orders. The example reads a single order and matches executions to order legs by legId. Each execution can have its own quantity, price, and timestamp. Working orders can have zero or partial fills. The loops cover the returned order’s own legs and activities.
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>
void ChilkatSample(void)
{
BOOL success;
HCkSecretsW vault;
HCkJsonObjectW entry;
HCkJsonObjectW tokens;
const wchar_t *accessToken;
HCkHttpW http;
HCkJsonObjectW selection;
const wchar_t *accountHash;
const wchar_t *orderId;
HCkHttpResponseW resp;
HCkJsonObjectW order;
HCkJsonObjectW symbols;
int i;
const wchar_t *legId;
const wchar_t *symbol;
int fills;
int j;
const wchar_t *filledLeg;
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);
// Supply the ID as text so large numeric identifiers keep all their digits.
orderId = L"ORDER_ID_FROM_LIST";
CkHttpW_SetUrlVar(http,L"orderId",orderId);
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpNoBody(http,L"GET",L"https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",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);
CkHttpResponseW_Dispose(resp);
return;
}
order = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(order,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);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(order);
return;
}
wprintf(L"Order %s %s\n",CkJsonObjectW_stringOf(order,L"orderId"),CkJsonObjectW_stringOf(order,L"status"));
symbols = CkJsonObjectW_Create();
i = 0;
while ((i < CkJsonObjectW_SizeOfArray(order,L"orderLegCollection"))) {
CkJsonObjectW_putI(order,i);
legId = CkJsonObjectW_stringOf(order,L"orderLegCollection[i].legId");
symbol = CkJsonObjectW_stringOf(order,L"orderLegCollection[i].instrument.symbol");
CkJsonObjectW_UpdateString(symbols,legId,symbol);
wprintf(L"Leg %s %s %s %s\n",legId,CkJsonObjectW_stringOf(order,L"orderLegCollection[i].instruction"),CkJsonObjectW_stringOf(order,L"orderLegCollection[i].quantity")
,symbol);
i = i + 1;
}
fills = 0;
i = 0;
while ((i < CkJsonObjectW_SizeOfArray(order,L"orderActivityCollection"))) {
CkJsonObjectW_putI(order,i);
if (CkJsonObjectW_StringOfEquals(order,L"orderActivityCollection[i].executionType",L"FILL",TRUE) == TRUE) {
j = 0;
while ((j < CkJsonObjectW_SizeOfArray(order,L"orderActivityCollection[i].executionLegs"))) {
CkJsonObjectW_putJ(order,j);
filledLeg = CkJsonObjectW_stringOf(order,L"orderActivityCollection[i].executionLegs[j].legId");
wprintf(L"Fill on leg %s %s quantity %s price %s time %s\n",filledLeg,CkJsonObjectW_stringOf(symbols,filledLeg),CkJsonObjectW_stringOf(order,L"orderActivityCollection[i].executionLegs[j].quantity")
,CkJsonObjectW_stringOf(order,L"orderActivityCollection[i].executionLegs[j].price"),CkJsonObjectW_stringOf(order,L"orderActivityCollection[i].executionLegs[j].time"));
fills = fills + 1;
j = j + 1;
}
}
i = i + 1;
}
if (fills == 0) {
wprintf(L"No fills reported for this order.\n");
}
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(selection);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(order);
CkJsonObjectW_Dispose(symbols);
}