Objective-C Requires Chilkat v11.0.0+
Objective-C
Schwab: Export daily OHLCV price history
See more Schwab Examples
Downloads one year of daily open, high, low, close, and volume for one equity. The CSV records each candle’s UTC timestamp and original Unix milliseconds. Volume and prices retain their JSON number text. An empty series leaves any previous export in place.
Use the access token saved by desktop authorization. Enable the Market Data API for the Schwab app. 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 <CkoHttpResponse.h>
#import <CkoHttpRequest.h>
#import <CkoCsv.h>
#import <CkoStringBuilder.h>
#import <CkoDateTime.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;
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"symbol" value: @"AAPL"];
[query AddParam: @"periodType" value: @"year"];
[query AddParam: @"period" value: @"1"];
[query AddParam: @"frequencyType" value: @"daily"];
[query AddParam: @"frequency" value: @"1"];
[query AddParam: @"needExtendedHoursData" value: @"false"];
success = [http HttpReq: @"https://api.schwabapi.com/marketdata/v1/pricehistory" 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;
}
int count = [[reply SizeOfArray: @"candles"] intValue];
if (count <= 0) {
NSLog(@"%@",@"No candles for this symbol and period.");
return;
}
CkoCsv *csv = [[CkoCsv alloc] init];
[csv SetColumnName: [NSNumber numberWithInt: 0] columnName: @"utcTime"];
[csv SetColumnName: [NSNumber numberWithInt: 1] columnName: @"unixMilliseconds"];
[csv SetColumnName: [NSNumber numberWithInt: 2] columnName: @"open"];
[csv SetColumnName: [NSNumber numberWithInt: 3] columnName: @"high"];
[csv SetColumnName: [NSNumber numberWithInt: 4] columnName: @"low"];
[csv SetColumnName: [NSNumber numberWithInt: 5] columnName: @"close"];
[csv SetColumnName: [NSNumber numberWithInt: 6] columnName: @"volume"];
CkoStringBuilder *millis = [[CkoStringBuilder alloc] init];
CkoJsonObject *unixTime = [[CkoJsonObject alloc] init];
CkoDateTime *candleTime = [[CkoDateTime alloc] init];
int i = 0;
while ((i < count)) {
reply.I = [NSNumber numberWithInt: i];
NSString *timestamp = [reply StringOf: @"candles[i].datetime"];
// DateOf reads Unix seconds. Drop the three millisecond digits as text.
[millis SetString: timestamp];
int digits = [millis.Length intValue] - 3;
if (digits <= 0) {
NSLog(@"%@",@"Missing candle timestamp.");
return;
}
[unixTime UpdateNumber: @"seconds" numericStr: [millis GetRange: [NSNumber numberWithInt: 0] numChars: [NSNumber numberWithInt: digits] removeFlag: NO]];
success = [unixTime DateOf: @"seconds" dateTime: candleTime];
if (success != YES) {
NSLog(@"%@",@"Invalid candle timestamp.");
return;
}
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 0] content: [candleTime GetAsTimestamp: NO]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 1] content: timestamp];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 2] content: [reply StringOf: @"candles[i].open"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 3] content: [reply StringOf: @"candles[i].high"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 4] content: [reply StringOf: @"candles[i].low"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 5] content: [reply StringOf: @"candles[i].close"]];
[csv SetCell: [NSNumber numberWithInt: i] col: [NSNumber numberWithInt: 6] content: [reply StringOf: @"candles[i].volume"]];
i = i + 1;
}
success = [csv SaveFile2: @"schwab-daily-prices.csv" charset: @"utf-8"];
if (success != YES) {
NSLog(@"%@",@"Could not write schwab-daily-prices.csv.");
return;
}
NSLog(@"%@%d%@",@"Wrote ",[csv.NumRows intValue],@" rows to schwab-daily-prices.csv.");