Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.Secrets,
Chilkat.HttpRequest,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
vault: TSecrets;
entry: TJsonObject;
tokens: TJsonObject;
accessToken: string;
http: THttp;
selection: TJsonObject;
accountHash: string;
resp: THttpResponse;
query: THttpRequest;
reply: TJsonObject;
balances: TJsonObject;
count: Integer;
i: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
vault := TSecrets.Create;
vault.Location := 'local_manager';
vault.AutoSplit := True;
entry := TJsonObject.Create;
entry.UpdateString('appName','SchwabTrader');
entry.UpdateString('service','OAuth2');
entry.UpdateString('username','personal-desktop');
tokens := TJsonObject.Create;
success := vault.GetSecretJson(entry,tokens);
if (success <> True) then
begin
WriteLn('Could not read the token record. Check the store and complete desktop authorization first.');
Exit;
end;
accessToken := tokens.StringOf('access_token');
if (accessToken = '') then
begin
WriteLn('Run the Schwab authorization or refresh example to save an access token.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := accessToken;
// Read the hash saved by the authorized-account selection example.
entry.UpdateString('service','SelectedAccount');
selection := TJsonObject.Create;
success := vault.GetSecretJson(entry,selection);
accountHash := selection.StringOf('hashValue');
if ((success <> True) or (accountHash = '')) then
begin
WriteLn('Select an authorized account first.');
Exit;
end;
http.SetUrlVar('account',accountHash);
resp := THttpResponse.Create;
query := THttpRequest.Create;
query.HttpVerb := 'GET';
query.AddParam('fields','positions');
success := http.HttpReq('https://api.schwabapi.com/trader/v1/accounts/{$account}',query,resp);
if ((success <> True) or (resp.StatusCode <> 200)) then
begin
if (resp.StatusCode = 0) then
begin
WriteLn('No HTTP response. Check connectivity.');
end
else
begin
WriteLn('Schwab returned HTTP ' + resp.StatusCode);
if (resp.StatusCode = 401) then
begin
WriteLn('Refresh the access token or repeat desktop authorization.');
end;
end;
Exit;
end;
reply := TJsonObject.Create;
success := reply.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Schwab returned unexpected JSON.');
Exit;
end;
balances := TJsonObject.Create;
success := reply.ObjectOf2('securitiesAccount.currentBalances',balances);
if (success <> True) then
begin
WriteLn('The account response contained no current balances.');
Exit;
end;
WriteLn('Cash balance: ' + balances.StringOf('cashBalance'));
WriteLn('Liquidation value: ' + balances.StringOf('liquidationValue'));
if (balances.HasMember('buyingPower') = True) then
begin
WriteLn('Buying power: ' + balances.StringOf('buyingPower'));
end;
count := reply.SizeOfArray('securitiesAccount.positions');
if (count <= 0) then
begin
WriteLn('No positions.');
end;
i := 0;
while (i < count) do
begin
reply.I := i;
WriteLn(reply.StringOf('securitiesAccount.positions[i].instrument.symbol') + ' long ' + reply.StringOf('securitiesAccount.positions[i].longQuantity') + ' short '
+ reply.StringOf('securitiesAccount.positions[i].shortQuantity') + ' market value ' + reply.StringOf('securitiesAccount.positions[i].marketValue'));
i := i + 1;
end;
vault.Free;
entry.Free;
tokens.Free;
http.Free;
selection.Free;
resp.Free;
query.Free;
reply.Free;
balances.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.