Lua Requires Chilkat v11.0.0+
Lua
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 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
local underlying = "AAPL"
local expiration = "2027-01-15"
local strike = "250"
local side = "CALL"
local resp = chilkat.HttpResponse.new()
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("symbol", underlying)
query:add_param("contractType", side)
query:add_param("strike", strike)
query:add_param("fromDate", expiration)
query:add_param("toDate", expiration)
success = http:http_req("https://api.schwabapi.com/marketdata/v1/chains", 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 mapName = "callExpDateMap"
if side == "PUT" then
mapName = "putExpDateMap"
end
local expirations = chilkat.JsonObject.new()
success = reply:object_of2(mapName, expirations)
if not success then
print("No option map returned. Chain status: " .. reply:string_of("status"))
return
end
-- Expiration keys contain a day count. Strike keys may contain a decimal point.
local strikes = chilkat.JsonObject.new()
local contracts = chilkat.JsonArray.new()
local contract = chilkat.JsonObject.new()
local found = 0
local i = 0
while i < expirations.size do
expirations:object_at2(i, strikes)
local j = 0
while j < strikes.size do
strikes:array_at2(j, contracts)
local k = 0
while k < contracts.size do
contracts:object_at2(k, contract)
print(contract:string_of("description") .. " strike " .. contract:string_of("strikePrice"))
print("Contract symbol: [" .. contract:string_of("symbol") .. "]")
found = found + 1
k = k + 1
end
j = j + 1
end
i = i + 1
end
if found == 0 then
print("No contracts matched the requested expiration, side and strike.")
end