Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Schwab: Find an option contract by expiration and strike

See more Schwab Examples

Filters a chain by underlying, call or put, exact expiration, and strike. Choose a currently listed expiration and strike. Schwab groups contracts by expiration and strike and may return multiple deliverables. Compare the descriptions and use the exact returned contract symbol, including its spaces, for a later quote request.

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.

Schwab API specification

Chilkat Objective-C Downloads

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

NSString *underlying = @"AAPL";
NSString *expiration = @"2027-01-15";
NSString *strike = @"250";
NSString *side = @"CALL";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"symbol" value: underlying];
[query AddParam: @"contractType" value: side];
[query AddParam: @"strike" value: strike];
[query AddParam: @"fromDate" value: expiration];
[query AddParam: @"toDate" value: expiration];
success = [http HttpReq: @"https://api.schwabapi.com/marketdata/v1/chains" 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;
}

NSString *mapName = @"callExpDateMap";
if (side == @"PUT") {
    mapName = @"putExpDateMap";
}

CkoJsonObject *expirations = [[CkoJsonObject alloc] init];
success = [reply ObjectOf2: mapName jsonObj: expirations];
if (success != YES) {
    NSLog(@"%@%@",@"No option map returned. Chain status: ",[reply StringOf: @"status"]);
    return;
}

//  Expiration keys contain a day count. Strike keys may contain a decimal point.
CkoJsonObject *strikes = [[CkoJsonObject alloc] init];
CkoJsonArray *contracts = [[CkoJsonArray alloc] init];
CkoJsonObject *contract = [[CkoJsonObject alloc] init];
int found = 0;
int i = 0;
while ((i < [expirations.Size intValue])) {
    [expirations ObjectAt2: [NSNumber numberWithInt: i] jsonObj: strikes];
    int j = 0;
    while ((j < [strikes.Size intValue])) {
        [strikes ArrayAt2: [NSNumber numberWithInt: j] jarr: contracts];
        int k = 0;
        while ((k < [contracts.Size intValue])) {
            [contracts ObjectAt2: [NSNumber numberWithInt: k] jsonObj: contract];
            NSLog(@"%@%@%@",[contract StringOf: @"description"],@"  strike ",[contract StringOf: @"strikePrice"]);
            NSLog(@"%@%@%@",@"Contract symbol: [",[contract StringOf: @"symbol"],@"]");
            found = found + 1;
            k = k + 1;
        }

        j = j + 1;
    }

    i = i + 1;
}

if (found == 0) {
    NSLog(@"%@",@"No contracts matched the requested expiration, side and strike.");
}