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

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.

Schwab API specification

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0

set vault = Server.CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = Server.CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
set tokens = Server.CreateObject("Chilkat.JsonObject")
success = vault.GetSecretJson(entry,tokens)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read the token record. Check the store and complete desktop authorization first.") & "</pre>"
    Response.End
End If

accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
    Response.Write "<pre>" & Server.HTMLEncode( "Run the Schwab authorization or refresh example to save an access token.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = accessToken

set resp = Server.CreateObject("Chilkat.HttpResponse")
set query = Server.CreateObject("Chilkat.HttpRequest")
query.HttpVerb = "GET"
query.AddParam "symbol","AAPL"
query.AddParam "periodType","year"
query.AddParam "period","1"
query.AddParam "frequencyType","daily"
query.AddParam "frequency","1"
query.AddParam "needExtendedHoursData","false"
success = http.HttpReq("https://api.schwabapi.com/marketdata/v1/pricehistory",query,resp)
If ((success <> 1) Or (resp.StatusCode <> 200)) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response. Check connectivity.") & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned HTTP " & resp.StatusCode) & "</pre>"
        If (resp.StatusCode = 401) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Refresh the access token or repeat desktop authorization.") & "</pre>"
        End If

    End If

    Response.End
End If

set reply = Server.CreateObject("Chilkat.JsonObject")
success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned unexpected JSON.") & "</pre>"
    Response.End
End If

count = reply.SizeOfArray("candles")
If (count <= 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No candles for this symbol and period.") & "</pre>"
    Response.End
End If

set csv = Server.CreateObject("Chilkat.Csv")
success = csv.SetColumnName(0,"utcTime")
success = csv.SetColumnName(1,"unixMilliseconds")
success = csv.SetColumnName(2,"open")
success = csv.SetColumnName(3,"high")
success = csv.SetColumnName(4,"low")
success = csv.SetColumnName(5,"close")
success = csv.SetColumnName(6,"volume")
set millis = Server.CreateObject("Chilkat.StringBuilder")
set unixTime = Server.CreateObject("Chilkat.JsonObject")
set candleTime = Server.CreateObject("Chilkat.CkDateTime")
i = 0
Do While (i < count)
    reply.I = i
    timestamp = reply.StringOf("candles[i].datetime")
    '  DateOf reads Unix seconds. Drop the three millisecond digits as text.
    success = millis.SetString(timestamp)
    digits = millis.Length - 3
    If (digits <= 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing candle timestamp.") & "</pre>"
        Response.End
    End If

    success = unixTime.UpdateNumber("seconds",millis.GetRange(0,digits,0))
    success = unixTime.DateOf("seconds",candleTime)
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid candle timestamp.") & "</pre>"
        Response.End
    End If

    success = csv.SetCell(i,0,candleTime.GetAsTimestamp(0))
    success = csv.SetCell(i,1,timestamp)
    success = csv.SetCell(i,2,reply.StringOf("candles[i].open"))
    success = csv.SetCell(i,3,reply.StringOf("candles[i].high"))
    success = csv.SetCell(i,4,reply.StringOf("candles[i].low"))
    success = csv.SetCell(i,5,reply.StringOf("candles[i].close"))
    success = csv.SetCell(i,6,reply.StringOf("candles[i].volume"))
    i = i + 1
Loop
success = csv.SaveFile2("schwab-daily-prices.csv","utf-8")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not write schwab-daily-prices.csv.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Wrote " & csv.NumRows & " rows to schwab-daily-prices.csv.") & "</pre>"

%>
</body>
</html>