Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
final vault = CkSecrets();
vault.location = 'local_manager';
vault.autoSplit = true;
final entry = CkJsonObject();
entry.updateString('appName', 'SchwabTrader');
entry.updateString('service', 'OAuth2');
entry.updateString('username', 'personal-desktop');
final tokens = CkJsonObject();
try {
vault.getSecretJson(entry, tokens);
} on ChilkatException {
print('Could not read the token record. Check the store and complete desktop authorization first.');
return;
}
final accessToken = tokens.stringOf('access_token');
if (accessToken == '') {
print('Run the Schwab authorization or refresh example to save an access token.');
return;
}
final http = CkHttp();
http.requireSslCertVerify = true;
http.authToken = accessToken;
// Read the hash saved by the authorized-account selection example.
entry.updateString('service', 'SelectedAccount');
final selection = CkJsonObject();
success = true;
try {
vault.getSecretJson(entry, selection);
} on ChilkatException {
success = false;
}
final accountHash = selection.stringOf('hashValue');
if ((!success) || (accountHash == '')) {
print('Select an authorized account first.');
return;
}
http.setUrlVar('account', accountHash);
final resp = CkHttpResponse();
final query = CkHttpRequest();
query.httpVerb = 'GET';
query.addParam('fields', 'positions');
success = true;
try {
http.httpReq('https://api.schwabapi.com/trader/v1/accounts/{\$account}', query, resp);
} on ChilkatException {
success = false;
}
if ((!success) || (resp.statusCode != 200)) {
if (resp.statusCode == 0) {
print('No HTTP response. Check connectivity.');
} else {
print('Schwab returned HTTP ${resp.statusCode}');
if (resp.statusCode == 401) {
print('Refresh the access token or repeat desktop authorization.');
}
}
return;
}
final reply = CkJsonObject();
try {
reply.load(resp.bodyStr);
} on ChilkatException {
print('Schwab returned unexpected JSON.');
return;
}
final balances = CkJsonObject();
try {
reply.objectOf2('securitiesAccount.currentBalances', balances);
} on ChilkatException {
print('The account response contained no current balances.');
return;
}
print('Cash balance: ${balances.stringOf('cashBalance')}');
print('Liquidation value: ${balances.stringOf('liquidationValue')}');
if (balances.hasMember('buyingPower')) {
print('Buying power: ${balances.stringOf('buyingPower')}');
}
final count = reply.sizeOfArray('securitiesAccount.positions');
if (count <= 0) {
print('No positions.');
}
var i = 0;
while (i < count) {
reply.i = i;
print('${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++;
}
}