Swift Requires Chilkat v11.0.0+
Swift
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.
Chilkat Swift Downloads
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
var underlying: String? = "AAPL"
var expiration: String? = "2027-01-15"
var strike: String? = "250"
var side: String? = "CALL"
let resp = CkoHttpResponse()!
let query = CkoHttpRequest()!
query.httpVerb = "GET"
query.addParam(name: "symbol", value: underlying)
query.addParam(name: "contractType", value: side)
query.addParam(name: "strike", value: strike)
query.addParam(name: "fromDate", value: expiration)
query.addParam(name: "toDate", value: expiration)
success = http.httpReq(url: "https://api.schwabapi.com/marketdata/v1/chains", 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
}
var mapName: String? = "callExpDateMap"
if side == "PUT" {
mapName = "putExpDateMap"
}
let expirations = CkoJsonObject()!
success = reply.objectOf2(jsonPath: mapName, jsonObj: expirations)
if success != true {
print("No option map returned. Chain status: \(reply.string(of: "status")!)")
return
}
// Expiration keys contain a day count. Strike keys may contain a decimal point.
let strikes = CkoJsonObject()!
let contracts = CkoJsonArray()!
let contract = CkoJsonObject()!
var found: Int = 0
var i: Int = 0
while (i < expirations.size.intValue) {
expirations.objectAt2(index: i, jsonObj: strikes)
var j: Int = 0
while (j < strikes.size.intValue) {
strikes.arrayAt2(index: j, jarr: contracts)
var k: Int = 0
while (k < contracts.size.intValue) {
contracts.objectAt2(index: k, jsonObj: contract)
print("\(contract.string(of: "description")!) strike \(contract.string(of: "strikePrice")!)")
print("Contract symbol: [\(contract.string(of: "symbol")!)]")
found = found + 1
k = k + 1
}
j = j + 1
}
i = i + 1
}
if found == 0 {
print("No contracts matched the requested expiration, side and strike.")
}
}