Sample code for 30+ languages & platforms
Objective-C 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 Objective-C Downloads

Objective-C
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.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];

//  Supply the ID as text so large numeric identifiers keep all their digits.
NSString *orderId = @"ORDER_ID_FROM_LIST";
[http SetUrlVar: @"orderId" value: orderId];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpNoBody: @"GET" url: @"https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}" 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 *order = [[CkoJsonObject alloc] init];
success = [order Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Schwab returned unexpected JSON.");
    return;
}

NSLog(@"%@%@%@%@",@"Order ",[order StringOf: @"orderId"],@"  ",[order StringOf: @"status"]);
CkoJsonObject *symbols = [[CkoJsonObject alloc] init];
int i = 0;
while ((i < [[order SizeOfArray: @"orderLegCollection"] intValue])) {
    order.I = [NSNumber numberWithInt: i];
    NSString *legId = [order StringOf: @"orderLegCollection[i].legId"];
    NSString *symbol = [order StringOf: @"orderLegCollection[i].instrument.symbol"];
    [symbols UpdateString: legId value: symbol];
    NSLog(@"%@%@%@%@%@%@%@%@",@"Leg ",legId,@"  ",[order StringOf: @"orderLegCollection[i].instruction"],@" ",[order StringOf: @"orderLegCollection[i].quantity"]
        ,@" ",symbol);
    i = i + 1;
}

int fills = 0;
i = 0;
while ((i < [[order SizeOfArray: @"orderActivityCollection"] intValue])) {
    order.I = [NSNumber numberWithInt: i];
    if ([order StringOfEquals: @"orderActivityCollection[i].executionType" value: @"FILL" caseSensitive: YES] == YES) {
        int j = 0;
        while ((j < [[order SizeOfArray: @"orderActivityCollection[i].executionLegs"] intValue])) {
            order.J = [NSNumber numberWithInt: j];
            NSString *filledLeg = [order StringOf: @"orderActivityCollection[i].executionLegs[j].legId"];
            NSLog(@"%@%@%@%@%@%@%@%@%@%@",@"Fill on leg ",filledLeg,@"  ",[symbols StringOf: filledLeg],@"  quantity ",[order StringOf: @"orderActivityCollection[i].executionLegs[j].quantity"]
                ,@"  price ",[order StringOf: @"orderActivityCollection[i].executionLegs[j].price"],@"  time ",[order StringOf: @"orderActivityCollection[i].executionLegs[j].time"]);
            fills = fills + 1;
            j = j + 1;
        }

    }

    i = i + 1;
}

if (fills == 0) {
    NSLog(@"%@",@"No fills reported for this order.");
}