Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Schwab: Inspect an order, its legs, and fills

See more Schwab Examples

Use an order ID from the selected account’s recent orders. The example reads a single order and matches executions to order legs by legId. Each execution can have its own quantity, price, and timestamp. Working orders can have zero or partial fills. The loops cover the returned order’s own legs and activities.

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.

Schwab API specification

Chilkat Lua Downloads

Lua
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)

-- Supply the ID as text so large numeric identifiers keep all their digits.
local orderId = "ORDER_ID_FROM_LIST"
http:set_url_var("orderId", orderId)
local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", "https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}", 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 order = chilkat.JsonObject.new()
success = order:load(resp.body_str)
if not success then
    print("Schwab returned unexpected JSON.")
    return
end

print("Order " .. order:string_of("orderId") .. "  " .. order:string_of("status"))
local symbols = chilkat.JsonObject.new()
local i = 0
while i < order:size_of_array("orderLegCollection") do
    order.i = i
    local legId = order:string_of("orderLegCollection[i].legId")
    local symbol = order:string_of("orderLegCollection[i].instrument.symbol")
    symbols:update_string(legId, symbol)
    print("Leg " .. legId .. "  " .. order:string_of("orderLegCollection[i].instruction") .. " " .. order:string_of("orderLegCollection[i].quantity") .. " " .. symbol)
    i = i + 1
end

local fills = 0
i = 0
while i < order:size_of_array("orderActivityCollection") do
    order.i = i
    if order:string_of_equals("orderActivityCollection[i].executionType", "FILL", true) then
        local j = 0
        while j < order:size_of_array("orderActivityCollection[i].executionLegs") do
            order.j = j
            local filledLeg = order:string_of("orderActivityCollection[i].executionLegs[j].legId")
            print("Fill on leg " .. filledLeg .. "  " .. symbols:string_of(filledLeg) .. "  quantity " .. order:string_of("orderActivityCollection[i].executionLegs[j].quantity") .. "  price " .. order:string_of("orderActivityCollection[i].executionLegs[j].price") .. "  time " .. order:string_of("orderActivityCollection[i].executionLegs[j].time"))
            fills = fills + 1
            j = j + 1
        end
    end

    i = i + 1
end

if fills == 0 then
    print("No fills reported for this order.")
end