Lua Requires Chilkat v11.0.0+
Lua
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 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
-- Supply the calendar date you want to check, for example a US holiday.
local clock = chilkat.DateTime.new()
clock:set_from_current_system_time()
local marketDate = clock:get_as_iso8601("YYYY-MM-DD", false)
local resp = chilkat.HttpResponse.new()
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("date", marketDate)
success = http:http_req("https://api.schwabapi.com/marketdata/v1/markets/equity", 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 reply = chilkat.JsonObject.new()
success = reply:load(resp.body_str)
if not success then
print("Schwab returned unexpected JSON.")
return
end
local products = chilkat.JsonObject.new()
success = reply:object_of2("equity", products)
if (not success) or (products.size == 0) then
print("No equity market schedule returned for " .. marketDate)
return
end
local product = chilkat.JsonObject.new()
local i = 0
while i < products.size do
products:object_at2(i, product)
print(products:name_at(i) .. " " .. product:string_of("date"))
if product:bool_of("isOpen") then
local j = 0
local count = product:size_of_array("sessionHours.regularMarket")
if count <= 0 then
print("Open date, but no regular-session times were supplied.")
end
while j < count do
product.j = j
print("Regular session: " .. product:string_of("sessionHours.regularMarket[j].start") .. " to " .. product:string_of("sessionHours.regularMarket[j].end"))
j = j + 1
end
else
print("Closed for this date.")
end
i = i + 1
end