Swift Requires Chilkat v11.0.0+
Swift
Schwab: List recent orders for one account
See more Schwab Examples
Lists orders entered in the last seven days, with an optional status filter such as WORKING. Shows the order ID, entry time, status, filled quantity, and remaining quantity. Select an ID from the results for the order detail and fills example. An older order can still be working outside this entry-time window.
Use the access token saved by desktop authorization. First select the authorized account whose data you want to use. 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
// Read the hash saved by the authorized-account selection example.
entry.updateString(jsonPath: "service", value: "SelectedAccount")
let selection = CkoJsonObject()!
success = vault.getSecretJson(jsonId: entry, jsonOut: selection)
var accountHash: String? = selection.string(of: "hashValue")
if (success != true) || (accountHash == "") {
print("Select an authorized account first.")
return
}
http.setUrlVar(name: "account", value: accountHash)
// Use UTC endpoints for a rolling seven-day window.
let clock = CkoDateTime()!
clock.setFromCurrentSystemTime()
let endTime = CkoStringBuilder()!
endTime.append(value: clock.get(asTimestamp: false))
var replacements: Int = endTime.replace(value: "Z", replacement: ".000Z").intValue
clock.addDays(numDays: -7)
let startTime = CkoStringBuilder()!
startTime.append(value: clock.get(asTimestamp: false))
replacements = startTime.replace(value: "Z", replacement: ".000Z").intValue
var statusFilter: String? = ""
let resp = CkoHttpResponse()!
let query = CkoHttpRequest()!
query.httpVerb = "GET"
query.addParam(name: "fromEnteredTime", value: startTime.getAsString())
query.addParam(name: "toEnteredTime", value: endTime.getAsString())
query.addParam(name: "maxResults", value: "100")
if statusFilter != "" {
query.addParam(name: "status", value: statusFilter)
}
success = http.httpReq(url: "https://api.schwabapi.com/trader/v1/accounts/{$account}/orders", 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 orders = CkoJsonArray()!
success = orders.load(jsonArray: resp.bodyStr)
if success != true {
print("Schwab returned unexpected JSON.")
return
}
if orders.size.intValue == 0 {
print("No matching orders entered during this interval.")
}
if orders.size.intValue == 100 {
print("Reached maxResults. Narrow the interval to check for additional orders.")
}
let order = CkoJsonObject()!
var i: Int = 0
while (i < orders.size.intValue) {
orders.objectAt2(index: i, jsonObj: order)
print("Order \(order.string(of: "orderId")!) \(order.string(of: "enteredTime")!) \(order.string(of: "orderType")!) \(order.string(of: "status")!)")
print(" filled \(order.string(of: "filledQuantity")!) remaining \(order.string(of: "remainingQuantity")!)")
i = i + 1
}
}