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

Schwab: Read account balances and positions

See more Schwab Examples

Requests fields=positions for the selected account. Reads current cash and liquidation value, prints buying power when supplied, and reports each holding’s symbol, long and short quantities, and market value. A cash-only portfolio can have balances and zero positions.

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)

local resp = chilkat.HttpResponse.new()
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("fields", "positions")
success = http:http_req("https://api.schwabapi.com/trader/v1/accounts/{$account}", 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 balances = chilkat.JsonObject.new()
success = reply:object_of2("securitiesAccount.currentBalances", balances)
if not success then
    print("The account response contained no current balances.")
    return
end

print("Cash balance: " .. balances:string_of("cashBalance"))
print("Liquidation value: " .. balances:string_of("liquidationValue"))
if balances:has_member("buyingPower") then
    print("Buying power: " .. balances:string_of("buyingPower"))
end

local count = reply:size_of_array("securitiesAccount.positions")
if count <= 0 then
    print("No positions.")
end

local i = 0
while i < count do
    reply.i = i
    print(reply:string_of("securitiesAccount.positions[i].instrument.symbol") .. "  long " .. reply:string_of("securitiesAccount.positions[i].longQuantity") .. "  short " .. reply:string_of("securitiesAccount.positions[i].shortQuantity") .. "  market value " .. reply:string_of("securitiesAccount.positions[i].marketValue"))
    i = i + 1
end