Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
Schwab: Read account balances and positions
See more Schwab Examples
Requests fields=positions for the selected account. Reads current cash and liquidation value, prints buying power when supplied, and reports each holding’s symbol, long and short quantities, and market value. A cash-only portfolio can have balances and zero positions.
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;
resp: HCkHttpResponse;
query: HCkHttpRequest;
reply: HCkJsonObject;
balances: HCkJsonObject;
count: Integer;
i: 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);
resp := CkHttpResponse_Create();
query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'fields','positions');
success := CkHttp_HttpReq(http,'https://api.schwabapi.com/trader/v1/accounts/{$account}',query,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;
balances := CkJsonObject_Create();
success := CkJsonObject_ObjectOf2(reply,'securitiesAccount.currentBalances',balances);
if (success <> True) then
begin
Memo1.Lines.Add('The account response contained no current balances.');
Exit;
end;
Memo1.Lines.Add('Cash balance: ' + CkJsonObject__stringOf(balances,'cashBalance'));
Memo1.Lines.Add('Liquidation value: ' + CkJsonObject__stringOf(balances,'liquidationValue'));
if (CkJsonObject_HasMember(balances,'buyingPower') = True) then
begin
Memo1.Lines.Add('Buying power: ' + CkJsonObject__stringOf(balances,'buyingPower'));
end;
count := CkJsonObject_SizeOfArray(reply,'securitiesAccount.positions');
if (count <= 0) then
begin
Memo1.Lines.Add('No positions.');
end;
i := 0;
while (i < count) do
begin
CkJsonObject_putI(reply,i);
Memo1.Lines.Add(CkJsonObject__stringOf(reply,'securitiesAccount.positions[i].instrument.symbol') + ' long ' + CkJsonObject__stringOf(reply,'securitiesAccount.positions[i].longQuantity')
+ ' short ' + CkJsonObject__stringOf(reply,'securitiesAccount.positions[i].shortQuantity') + ' market value ' + CkJsonObject__stringOf(reply,'securitiesAccount.positions[i].marketValue'));
i := i + 1;
end;
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkJsonObject_Dispose(selection);
CkHttpResponse_Dispose(resp);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(balances);