Swift Requires Chilkat v11.0.0+
Swift
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 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
// Supply the calendar date you want to check, for example a US holiday.
let clock = CkoDateTime()!
clock.setFromCurrentSystemTime()
var marketDate: String? = clock.get(asIso8601: "YYYY-MM-DD", bLocal: false)
let resp = CkoHttpResponse()!
let query = CkoHttpRequest()!
query.httpVerb = "GET"
query.addParam(name: "date", value: marketDate)
success = http.httpReq(url: "https://api.schwabapi.com/marketdata/v1/markets/equity", 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
}
let products = CkoJsonObject()!
success = reply.objectOf2(jsonPath: "equity", jsonObj: products)
if (success != true) || (products.size.intValue == 0) {
print("No equity market schedule returned for \(marketDate!)")
return
}
let product = CkoJsonObject()!
var i: Int = 0
while (i < products.size.intValue) {
products.objectAt2(index: i, jsonObj: product)
print("\(products.name(at: i)!) \(product.string(of: "date")!)")
if product.bool(of: "isOpen") == true {
var j: Int = 0
var count: Int = product.size(ofArray: "sessionHours.regularMarket").intValue
if count <= 0 {
print("Open date, but no regular-session times were supplied.")
}
while (j < count) {
product.j = j
print("Regular session: \(product.string(of: "sessionHours.regularMarket[j].start")!) to \(product.string(of: "sessionHours.regularMarket[j].end")!)")
j = j + 1
}
}
else {
print("Closed for this date.")
}
i = i + 1
}
}