Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

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.

Schwab API specification

Chilkat Objective-C Downloads

Objective-C
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoHttpRequest.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;

CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"SchwabTrader"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"personal-desktop"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
success = [vault GetSecretJson: entry jsonOut: tokens];
if (success != YES) {
    NSLog(@"%@",@"Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

NSString *accessToken = [tokens StringOf: @"access_token"];
if (accessToken == @"") {
    NSLog(@"%@",@"Run the Schwab authorization or refresh example to save an access token.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = accessToken;

//  Read the hash saved by the authorized-account selection example.
[entry UpdateString: @"service" value: @"SelectedAccount"];
CkoJsonObject *selection = [[CkoJsonObject alloc] init];
success = [vault GetSecretJson: entry jsonOut: selection];
NSString *accountHash = [selection StringOf: @"hashValue"];
if ((success != YES) || (accountHash == @"")) {
    NSLog(@"%@",@"Select an authorized account first.");
    return;
}

[http SetUrlVar: @"account" value: accountHash];

CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"fields" value: @"positions"];
success = [http HttpReq: @"https://api.schwabapi.com/trader/v1/accounts/{$account}" request: query response: resp];
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
    if ([resp.StatusCode intValue] == 0) {
        NSLog(@"%@",@"No HTTP response. Check connectivity.");
    }
    else {
        NSLog(@"%@%d",@"Schwab returned HTTP ",[resp.StatusCode intValue]);
        if ([resp.StatusCode intValue] == 401) {
            NSLog(@"%@",@"Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

CkoJsonObject *reply = [[CkoJsonObject alloc] init];
success = [reply Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Schwab returned unexpected JSON.");
    return;
}

CkoJsonObject *balances = [[CkoJsonObject alloc] init];
success = [reply ObjectOf2: @"securitiesAccount.currentBalances" jsonObj: balances];
if (success != YES) {
    NSLog(@"%@",@"The account response contained no current balances.");
    return;
}

NSLog(@"%@%@",@"Cash balance: ",[balances StringOf: @"cashBalance"]);
NSLog(@"%@%@",@"Liquidation value: ",[balances StringOf: @"liquidationValue"]);
if ([balances HasMember: @"buyingPower"] == YES) {
    NSLog(@"%@%@",@"Buying power: ",[balances StringOf: @"buyingPower"]);
}

int count = [[reply SizeOfArray: @"securitiesAccount.positions"] intValue];
if (count <= 0) {
    NSLog(@"%@",@"No positions.");
}

int i = 0;
while ((i < count)) {
    reply.I = [NSNumber numberWithInt: i];
    NSLog(@"%@%@%@%@%@%@%@",[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;
}