Objective-C Requires Chilkat v11.0.0+
Objective-C
Schwab: Export account transactions to CSV
See more Schwab Examples
Exports the last seven days of trades, dividends, and interest for one account. Each UTF-8 CSV row represents an activity, including its signed net amount. The file is replaced on each run. For incremental imports, key records by account and activityId so overlapping date windows do not duplicate entries. If a window returns the 3,000-transaction maximum, split it before treating it as complete.
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>
#import <CkoCsv.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];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"startDate" value: [startTime GetAsString]];
[query AddParam: @"endDate" value: [endTime GetAsString]];
[query AddParam: @"types" value: @"TRADE,DIVIDEND_OR_INTEREST"];
success = [http HttpReq: @"https://api.schwabapi.com/trader/v1/accounts/{$account}/transactions" 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 *activity = [[CkoJsonArray alloc] init];
success = [activity Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Schwab returned unexpected JSON.");
return;
}
if ([activity.Size intValue] >= 3000) {
NSLog(@"%@",@"The response reached the transaction limit. Export smaller time windows.");
return;
}
CkoCsv *csv = [[CkoCsv alloc] init];
[csv SetColumnName: [NSNumber numberWithInt: 0] columnName: @"activityId"];
[csv SetColumnName: [NSNumber numberWithInt: 1] columnName: @"time"];
[csv SetColumnName: [NSNumber numberWithInt: 2] columnName: @"type"];
[csv SetColumnName: [NSNumber numberWithInt: 3] columnName: @"tradeDate"];
[csv SetColumnName: [NSNumber numberWithInt: 4] columnName: @"settlementDate"];
[csv SetColumnName: [NSNumber numberWithInt: 5] columnName: @"netAmount"];
CkoJsonObject *row = [[CkoJsonObject alloc] init];
int i = 0;
while ((i < [activity.Size intValue])) {
[activity ObjectAt2: [NSNumber numberWithInt: i] jsonObj: row];
// Keep identifiers and decimal amounts as returned by Schwab.
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 0] content: [row StringOf: @"activityId"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 1] content: [row StringOf: @"time"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 2] content: [row StringOf: @"type"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 3] content: [row StringOf: @"tradeDate"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 4] content: [row StringOf: @"settlementDate"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 5] content: [row StringOf: @"netAmount"]];
i = i + 1;
}
success = [csv SaveFile2: @"schwab-transactions.csv" charset: @"utf-8"];
if (success != YES) {
NSLog(@"%@",@"Could not write schwab-transactions.csv.");
return;
}
NSLog(@"%@%d%@",@"Wrote ",[csv.NumRows intValue],@" rows to schwab-transactions.csv.");