Lua Requires Chilkat v11.0.0+
Lua
Schwab: Export daily OHLCV price history
See more Schwab Examples
Downloads one year of daily open, high, low, close, and volume for one equity. The CSV records each candle’s UTC timestamp and original Unix milliseconds. Volume and prices retain their JSON number text. An empty series leaves any previous export in place.
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 resp = chilkat.HttpResponse.new()
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("symbol", "AAPL")
query:add_param("periodType", "year")
query:add_param("period", "1")
query:add_param("frequencyType", "daily")
query:add_param("frequency", "1")
query:add_param("needExtendedHoursData", "false")
success = http:http_req("https://api.schwabapi.com/marketdata/v1/pricehistory", 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 count = reply:size_of_array("candles")
if count <= 0 then
print("No candles for this symbol and period.")
return
end
local csv = chilkat.Csv.new()
csv:set_column_name(0, "utcTime")
csv:set_column_name(1, "unixMilliseconds")
csv:set_column_name(2, "open")
csv:set_column_name(3, "high")
csv:set_column_name(4, "low")
csv:set_column_name(5, "close")
csv:set_column_name(6, "volume")
local millis = chilkat.StringBuilder.new()
local unixTime = chilkat.JsonObject.new()
local candleTime = chilkat.DateTime.new()
local i = 0
while i < count do
reply.i = i
local timestamp = reply:string_of("candles[i].datetime")
-- DateOf reads Unix seconds. Drop the three millisecond digits as text.
millis:set_string(timestamp)
local digits = millis.length - 3
if digits <= 0 then
print("Missing candle timestamp.")
return
end
unixTime:update_number("seconds", millis:get_range(0, digits, false))
success = unixTime:date_of("seconds", candleTime)
if not success then
print("Invalid candle timestamp.")
return
end
csv:set_cell(i, 0, candleTime:get_as_timestamp(false))
csv:set_cell(i, 1, timestamp)
csv:set_cell(i, 2, reply:string_of("candles[i].open"))
csv:set_cell(i, 3, reply:string_of("candles[i].high"))
csv:set_cell(i, 4, reply:string_of("candles[i].low"))
csv:set_cell(i, 5, reply:string_of("candles[i].close"))
csv:set_cell(i, 6, reply:string_of("candles[i].volume"))
i = i + 1
end
success = csv:save_file2("schwab-daily-prices.csv", "utf-8")
if not success then
print("Could not write schwab-daily-prices.csv.")
return
end
print("Wrote " .. csv.num_rows .. " rows to schwab-daily-prices.csv.")