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

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.

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

underlying = "AAPL"
expiration = "2027-01-15"
strike = "250"
side = "CALL"
set resp = Server.CreateObject("Chilkat.HttpResponse")
set query = Server.CreateObject("Chilkat.HttpRequest")
query.HttpVerb = "GET"
query.AddParam "symbol",underlying
query.AddParam "contractType",side
query.AddParam "strike",strike
query.AddParam "fromDate",expiration
query.AddParam "toDate",expiration
success = http.HttpReq("https://api.schwabapi.com/marketdata/v1/chains",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

mapName = "callExpDateMap"
If (side = "PUT") Then
    mapName = "putExpDateMap"
End If

set expirations = Server.CreateObject("Chilkat.JsonObject")
success = reply.ObjectOf2(mapName,expirations)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No option map returned. Chain status: " & reply.StringOf("status")) & "</pre>"
    Response.End
End If

'  Expiration keys contain a day count. Strike keys may contain a decimal point.
set strikes = Server.CreateObject("Chilkat.JsonObject")
set contracts = Server.CreateObject("Chilkat.JsonArray")
set contract = Server.CreateObject("Chilkat.JsonObject")
found = 0
i = 0
Do While (i < expirations.Size)
    success = expirations.ObjectAt2(i,strikes)
    j = 0
    Do While (j < strikes.Size)
        success = strikes.ArrayAt2(j,contracts)
        k = 0
        Do While (k < contracts.Size)
            success = contracts.ObjectAt2(k,contract)
            Response.Write "<pre>" & Server.HTMLEncode( contract.StringOf("description") & "  strike " & contract.StringOf("strikePrice")) & "</pre>"
            Response.Write "<pre>" & Server.HTMLEncode( "Contract symbol: [" & contract.StringOf("symbol") & "]") & "</pre>"
            found = found + 1
            k = k + 1
        Loop
        j = j + 1
    Loop
    i = i + 1
Loop
If (found = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No contracts matched the requested expiration, side and strike.") & "</pre>"
End If


%>
</body>
</html>