Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

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.

Schwab API specification

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
accessToken: PWideChar;
http: HCkHttp;
selection: HCkJsonObject;
accountHash: PWideChar;
orderId: PWideChar;
resp: HCkHttpResponse;
order: HCkJsonObject;
symbols: HCkJsonObject;
i: Integer;
legId: PWideChar;
symbol: PWideChar;
fills: Integer;
j: Integer;
filledLeg: PWideChar;

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);

//  Supply the ID as text so large numeric identifiers keep all their digits.
orderId := 'ORDER_ID_FROM_LIST';
CkHttp_SetUrlVar(http,'orderId',orderId);
resp := CkHttpResponse_Create();
success := CkHttp_HttpNoBody(http,'GET','https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}',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;
order := CkJsonObject_Create();
success := CkJsonObject_Load(order,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Schwab returned unexpected JSON.');
    Exit;
  end;

Memo1.Lines.Add('Order ' + CkJsonObject__stringOf(order,'orderId') + '  ' + CkJsonObject__stringOf(order,'status'));
symbols := CkJsonObject_Create();
i := 0;
while (i < CkJsonObject_SizeOfArray(order,'orderLegCollection')) do
  begin
    CkJsonObject_putI(order,i);
    legId := CkJsonObject__stringOf(order,'orderLegCollection[i].legId');
    symbol := CkJsonObject__stringOf(order,'orderLegCollection[i].instrument.symbol');
    CkJsonObject_UpdateString(symbols,legId,symbol);
    Memo1.Lines.Add('Leg ' + legId + '  ' + CkJsonObject__stringOf(order,'orderLegCollection[i].instruction') + ' ' + CkJsonObject__stringOf(order,'orderLegCollection[i].quantity')
         + ' ' + symbol);
    i := i + 1;
  end;

fills := 0;
i := 0;
while (i < CkJsonObject_SizeOfArray(order,'orderActivityCollection')) do
  begin
    CkJsonObject_putI(order,i);
    if (CkJsonObject_StringOfEquals(order,'orderActivityCollection[i].executionType','FILL',True) = True) then
      begin
        j := 0;
        while (j < CkJsonObject_SizeOfArray(order,'orderActivityCollection[i].executionLegs')) do
          begin
            CkJsonObject_putJ(order,j);
            filledLeg := CkJsonObject__stringOf(order,'orderActivityCollection[i].executionLegs[j].legId');
            Memo1.Lines.Add('Fill on leg ' + filledLeg + '  ' + CkJsonObject__stringOf(symbols,filledLeg) + '  quantity ' + CkJsonObject__stringOf(order,'orderActivityCollection[i].executionLegs[j].quantity')
                 + '  price ' + CkJsonObject__stringOf(order,'orderActivityCollection[i].executionLegs[j].price') + '  time ' + CkJsonObject__stringOf(order,'orderActivityCollection[i].executionLegs[j].time'));
            fills := fills + 1;
            j := j + 1;
          end;

      end;
    i := i + 1;
  end;

if (fills = 0) then
  begin
    Memo1.Lines.Add('No fills reported for this order.');
  end;

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkJsonObject_Dispose(selection);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(order);
CkJsonObject_Dispose(symbols);