Sample code for 30+ languages & platforms
Swift 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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false

    let vault = CkoSecrets()!
    vault.location = "local_manager"
    vault.autoSplit = true
    let entry = CkoJsonObject()!
    entry.updateString(jsonPath: "appName", value: "SchwabTrader")
    entry.updateString(jsonPath: "service", value: "OAuth2")
    entry.updateString(jsonPath: "username", value: "personal-desktop")
    let tokens = CkoJsonObject()!
    success = vault.getSecretJson(jsonId: entry, jsonOut: tokens)
    if success != true {
        print("Could not read the token record. Check the store and complete desktop authorization first.")
        return
    }

    var accessToken: String? = tokens.string(of: "access_token")
    if accessToken == "" {
        print("Run the Schwab authorization or refresh example to save an access token.")
        return
    }

    let http = CkoHttp()!
    http.requireSslCertVerify = true
    http.authToken = accessToken

    let resp = CkoHttpResponse()!
    let query = CkoHttpRequest()!
    query.httpVerb = "GET"
    query.addParam(name: "symbols", value: "AAPL,MSFT,BRK/B")
    query.addParam(name: "fields", value: "quote")
    success = http.httpReq(url: "https://api.schwabapi.com/marketdata/v1/quotes", request: query, response: resp)
    if (success != true) || (resp.statusCode.intValue != 200) {
        if resp.statusCode.intValue == 0 {
            print("No HTTP response. Check connectivity.")
        }
        else {
            print("Schwab returned HTTP \(resp.statusCode.intValue)")
            if resp.statusCode.intValue == 401 {
                print("Refresh the access token or repeat desktop authorization.")
            }

        }

        return
    }

    let reply = CkoJsonObject()!
    success = reply.load(json: resp.bodyStr)
    if success != true {
        print("Schwab returned unexpected JSON.")
        return
    }

    //  Read members by index. A symbol may contain punctuation meaningful in a JSON path.
    let instrument = CkoJsonObject()!
    var i: Int = 0
    while (i < reply.size.intValue) {
        var key: String? = reply.name(at: i)
        if key != "errors" {
            success = reply.objectAt2(index: i, jsonObj: instrument)
            if success == true {
                print("\(key!)  bid \(instrument.string(of: "quote.bidPrice")!)  ask \(instrument.string(of: "quote.askPrice")!)  last \(instrument.string(of: "quote.lastPrice")!)")
                print("  quote time (Unix ms): \(instrument.string(of: "quote.quoteTime")!)  real-time: \(instrument.string(of: "realtime")!)")
            }

        }

        i = i + 1
    }

    var j: Int = 0
    while (j < reply.size(ofArray: "errors.invalidSymbols").intValue) {
        reply.j = j
        print("Invalid symbol: \(reply.string(of: "errors.invalidSymbols[j]")!)")
        j = j + 1
    }


}