Objective-C Requires Chilkat v11.0.0+
Objective-C
Schwab: Check equity market hours for a date
See more Schwab Examples
Looks up the US equity calendar for a selected trading date. Reads each product’s open flag and the actual regular-session start and end, including their offsets. This distinguishes a holiday closure from a shortened session. The open flag describes the requested date, not whether the market is open at this instant.
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 <CkoDateTime.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;
// Supply the calendar date you want to check, for example a US holiday.
CkoDateTime *clock = [[CkoDateTime alloc] init];
[clock SetFromCurrentSystemTime];
NSString *marketDate = [clock GetAsIso8601: @"YYYY-MM-DD" bLocal: NO];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"date" value: marketDate];
success = [http HttpReq: @"https://api.schwabapi.com/marketdata/v1/markets/equity" 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 *products = [[CkoJsonObject alloc] init];
success = [reply ObjectOf2: @"equity" jsonObj: products];
if ((success != YES) || ([products.Size intValue] == 0)) {
NSLog(@"%@%@",@"No equity market schedule returned for ",marketDate);
return;
}
CkoJsonObject *product = [[CkoJsonObject alloc] init];
int i = 0;
while ((i < [products.Size intValue])) {
[products ObjectAt2: [NSNumber numberWithInt: i] jsonObj: product];
NSLog(@"%@%@%@",[products NameAt: [NSNumber numberWithInt: i]],@" ",[product StringOf: @"date"]);
if ([product BoolOf: @"isOpen"] == YES) {
int j = 0;
int count = [[product SizeOfArray: @"sessionHours.regularMarket"] intValue];
if (count <= 0) {
NSLog(@"%@",@"Open date, but no regular-session times were supplied.");
}
while ((j < count)) {
product.J = [NSNumber numberWithInt: j];
NSLog(@"%@%@%@%@",@"Regular session: ",[product StringOf: @"sessionHours.regularMarket[j].start"],@" to ",[product StringOf: @"sessionHours.regularMarket[j].end"]);
j = j + 1;
}
}
else {
NSLog(@"%@",@"Closed for this date.");
}
i = i + 1;
}