Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "SchwabTrader")
entry:update_string("service", "OAuth2")
entry:update_string("username", "personal-desktop")
local tokens = chilkat.JsonObject.new()
success = vault:get_secret_json(entry, tokens)
if not success then
print("Could not read the token record. Check the store and complete desktop authorization first.")
return
end
local accessToken = tokens:string_of("access_token")
if accessToken == "" then
print("Run the Schwab authorization or refresh example to save an access token.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = accessToken
-- Read the hash saved by the authorized-account selection example.
entry:update_string("service", "SelectedAccount")
local selection = chilkat.JsonObject.new()
success = vault:get_secret_json(entry, selection)
local accountHash = selection:string_of("hashValue")
if (not success) or (accountHash == "") then
print("Select an authorized account first.")
return
end
http:set_url_var("account", accountHash)
-- Use UTC endpoints for a rolling seven-day window.
local clock = chilkat.DateTime.new()
clock:set_from_current_system_time()
local endTime = chilkat.StringBuilder.new()
endTime:append(clock:get_as_timestamp(false))
local replacements = endTime:replace("Z", ".000Z")
clock:add_days(-7)
local startTime = chilkat.StringBuilder.new()
startTime:append(clock:get_as_timestamp(false))
replacements = startTime:replace("Z", ".000Z")
local statusFilter = ""
local resp = chilkat.HttpResponse.new()
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("fromEnteredTime", startTime:get_as_string())
query:add_param("toEnteredTime", endTime:get_as_string())
query:add_param("maxResults", "100")
if statusFilter ~= "" then
query:add_param("status", statusFilter)
end
success = http:http_req("https://api.schwabapi.com/trader/v1/accounts/{$account}/orders", query, resp)
if (not success) or (resp.status_code ~= 200) then
if resp.status_code == 0 then
print("No HTTP response. Check connectivity.")
else
print("Schwab returned HTTP " .. resp.status_code)
if resp.status_code == 401 then
print("Refresh the access token or repeat desktop authorization.")
end
end
return
end
local orders = chilkat.JsonArray.new()
success = orders:load(resp.body_str)
if not success then
print("Schwab returned unexpected JSON.")
return
end
if orders.size == 0 then
print("No matching orders entered during this interval.")
end
if orders.size == 100 then
print("Reached maxResults. Narrow the interval to check for additional orders.")
end
local order = chilkat.JsonObject.new()
local i = 0
while i < orders.size do
orders:object_at2(i, 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
end