Objective-C Requires Chilkat v11.0.0+
Objective-C
Schwab: List recent orders for one account
See more Schwab Examples
Lists orders entered in the last seven days, with an optional status filter such as WORKING. Shows the order ID, entry time, status, filled quantity, and remaining quantity. Select an ID from the results for the order detail and fills example. An older order can still be working outside this entry-time window.
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 Objective-C Downloads
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoDateTime.h>
#import <CkoStringBuilder.h>
#import <CkoHttpResponse.h>
#import <CkoHttpRequest.h>
#import <CkoJsonArray.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];
// Use UTC endpoints for a rolling seven-day window.
CkoDateTime *clock = [[CkoDateTime alloc] init];
[clock SetFromCurrentSystemTime];
CkoStringBuilder *endTime = [[CkoStringBuilder alloc] init];
[endTime Append: [clock GetAsTimestamp: NO]];
int replacements = [[endTime Replace: @"Z" replacement: @".000Z"] intValue];
[clock AddDays: [NSNumber numberWithInt: -7]];
CkoStringBuilder *startTime = [[CkoStringBuilder alloc] init];
[startTime Append: [clock GetAsTimestamp: NO]];
replacements = [[startTime Replace: @"Z" replacement: @".000Z"] intValue];
NSString *statusFilter = @"";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"fromEnteredTime" value: [startTime GetAsString]];
[query AddParam: @"toEnteredTime" value: [endTime GetAsString]];
[query AddParam: @"maxResults" value: @"100"];
if (statusFilter != @"") {
[query AddParam: @"status" value: statusFilter];
}
success = [http HttpReq: @"https://api.schwabapi.com/trader/v1/accounts/{$account}/orders" 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;
}
CkoJsonArray *orders = [[CkoJsonArray alloc] init];
success = [orders Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Schwab returned unexpected JSON.");
return;
}
if ([orders.Size intValue] == 0) {
NSLog(@"%@",@"No matching orders entered during this interval.");
}
if ([orders.Size intValue] == 100) {
NSLog(@"%@",@"Reached maxResults. Narrow the interval to check for additional orders.");
}
CkoJsonObject *order = [[CkoJsonObject alloc] init];
int i = 0;
while ((i < [orders.Size intValue])) {
[orders ObjectAt2: [NSNumber numberWithInt: i] jsonObj: order];
NSLog(@"%@%@%@%@%@%@%@%@",@"Order ",[order StringOf: @"orderId"],@" ",[order StringOf: @"enteredTime"],@" ",[order StringOf: @"orderType"],@" ",[order StringOf: @"status"]);
NSLog(@"%@%@%@%@",@" filled ",[order StringOf: @"filledQuantity"],@" remaining ",[order StringOf: @"remainingQuantity"]);
i = i + 1;
}