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

Schwab: Get quotes for a watchlist

See more Schwab Examples

Requests several symbols in one call and prints the bid, ask, last price, quote timestamp in Unix milliseconds, and the real-time indicator when available. Missing quote fields can occur for individual instruments. The response uses symbol names as JSON keys and can report invalid symbols alongside valid quotes.

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>

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: @"symbols" value: @"AAPL,MSFT,BRK/B"];
[query AddParam: @"fields" value: @"quote"];
success = [http HttpReq: @"https://api.schwabapi.com/marketdata/v1/quotes" 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;
}

//  Read members by index. A symbol may contain punctuation meaningful in a JSON path.
CkoJsonObject *instrument = [[CkoJsonObject alloc] init];
int i = 0;
while ((i < [reply.Size intValue])) {
    NSString *key = [reply NameAt: [NSNumber numberWithInt: i]];
    if (key != @"errors") {
        success = [reply ObjectAt2: [NSNumber numberWithInt: i] jsonObj: instrument];
        if (success == YES) {
            NSLog(@"%@%@%@%@%@%@%@",key,@"  bid ",[instrument StringOf: @"quote.bidPrice"],@"  ask ",[instrument StringOf: @"quote.askPrice"],@"  last ",[instrument StringOf: @"quote.lastPrice"]);
            NSLog(@"%@%@%@%@",@"  quote time (Unix ms): ",[instrument StringOf: @"quote.quoteTime"],@"  real-time: ",[instrument StringOf: @"realtime"]);
        }

    }

    i = i + 1;
}

int j = 0;
while ((j < [[reply SizeOfArray: @"errors.invalidSymbols"] intValue])) {
    reply.J = [NSNumber numberWithInt: j];
    NSLog(@"%@%@",@"Invalid symbol: ",[reply StringOf: @"errors.invalidSymbols[j]"]);
    j = j + 1;
}