Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
accessToken: PWideChar;
http: HCkHttp;
selection: HCkJsonObject;
accountHash: PWideChar;
order: HCkJsonObject;
resp: HCkHttpResponse;
reply: HCkJsonObject;
validation: HCkJsonObject;
rules: HCkJsonArray;
detail: HCkJsonObject;
messages: Integer;
i: Integer;
category: PWideChar;
j: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','SchwabTrader');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','personal-desktop');
tokens := CkJsonObject_Create();
success := CkSecrets_GetSecretJson(vault,entry,tokens);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read the token record. Check the store and complete desktop authorization first.');
Exit;
end;
accessToken := CkJsonObject__stringOf(tokens,'access_token');
if (accessToken = '') then
begin
Memo1.Lines.Add('Run the Schwab authorization or refresh example to save an access token.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,accessToken);
// Read the hash saved by the authorized-account selection example.
CkJsonObject_UpdateString(entry,'service','SelectedAccount');
selection := CkJsonObject_Create();
success := CkSecrets_GetSecretJson(vault,entry,selection);
accountHash := CkJsonObject__stringOf(selection,'hashValue');
if ((success <> True) or (accountHash = '')) then
begin
Memo1.Lines.Add('Select an authorized account first.');
Exit;
end;
CkHttp_SetUrlVar(http,'account',accountHash);
// These are illustrative preview inputs. Set the symbol, quantity and limit price.
order := CkJsonObject_Create();
// The PreviewOrder schema puts orderLegs inside orderStrategy.
CkJsonObject_UpdateString(order,'orderStrategy.session','NORMAL');
CkJsonObject_UpdateString(order,'orderStrategy.duration','DAY');
CkJsonObject_UpdateString(order,'orderStrategy.orderType','LIMIT');
CkJsonObject_UpdateString(order,'orderStrategy.orderStrategyType','SINGLE');
CkJsonObject_UpdateNumber(order,'orderStrategy.price','100.00');
CkJsonObject_UpdateInt(order,'orderStrategy.quantity',1);
CkJsonObject_UpdateString(order,'orderStrategy.orderLegs[0].instruction','BUY');
CkJsonObject_UpdateInt(order,'orderStrategy.orderLegs[0].quantity',1);
CkJsonObject_UpdateString(order,'orderStrategy.orderLegs[0].finalSymbol','AAPL');
CkJsonObject_UpdateString(order,'orderStrategy.orderLegs[0].assetType','EQUITY');
resp := CkHttpResponse_Create();
success := CkHttp_HttpJson(http,'POST','https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder',order,'application/json',resp);
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('No HTTP response. Check connectivity.');
end
else
begin
Memo1.Lines.Add('Schwab returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
if (CkHttpResponse_getStatusCode(resp) = 401) then
begin
Memo1.Lines.Add('Refresh the access token or repeat desktop authorization.');
end;
end;
Exit;
end;
reply := CkJsonObject_Create();
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Schwab returned unexpected JSON.');
Exit;
end;
Memo1.Lines.Add('Preview only. No order was submitted.');
if (CkJsonObject_HasMember(reply,'orderStrategy.orderType') = True) then
begin
Memo1.Lines.Add('Projected order: ' + CkJsonObject__stringOf(reply,'orderStrategy.orderType') + ' price ' + CkJsonObject__stringOf(reply,'orderStrategy.price'));
end;
if (CkJsonObject_HasMember(reply,'orderStrategy.orderBalance.projectedBuyingPower') = True) then
begin
Memo1.Lines.Add('Projected buying power: ' + CkJsonObject__stringOf(reply,'orderStrategy.orderBalance.projectedBuyingPower'));
end;
validation := CkJsonObject_Create();
success := CkJsonObject_ObjectOf2(reply,'orderValidationResult',validation);
if (success <> True) then
begin
Memo1.Lines.Add('Schwab supplied no validation result.');
Exit;
end;
// Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
rules := CkJsonArray_Create();
detail := CkJsonObject_Create();
messages := 0;
i := 0;
while (i < CkJsonObject_getSize(validation)) do
begin
category := CkJsonObject__nameAt(validation,i);
success := CkJsonObject_ArrayAt2(validation,i,rules);
if (success = True) then
begin
j := 0;
while (j < CkJsonArray_getSize(rules)) do
begin
CkJsonArray_ObjectAt2(rules,j,detail);
Memo1.Lines.Add(category + ' ' + CkJsonObject__stringOf(detail,'validationRuleName') + ' ' + CkJsonObject__stringOf(detail,'message') + ' ' + CkJsonObject__stringOf(detail,'activityMessage'));
messages := messages + 1;
j := j + 1;
end;
end;
i := i + 1;
end;
if (messages = 0) then
begin
Memo1.Lines.Add('No validation messages returned.');
end;
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkJsonObject_Dispose(selection);
CkJsonObject_Dispose(order);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(validation);
CkJsonArray_Dispose(rules);
CkJsonObject_Dispose(detail);