Classic ASP Requires Chilkat v11.0.0+
Classic ASP
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.
Chilkat Classic ASP Downloads
<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
' Read the hash saved by the authorized-account selection example.
success = entry.UpdateString("service","SelectedAccount")
set selection = Server.CreateObject("Chilkat.JsonObject")
success = vault.GetSecretJson(entry,selection)
accountHash = selection.StringOf("hashValue")
If ((success <> 1) Or (accountHash = "")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Select an authorized account first.") & "</pre>"
Response.End
End If
success = http.SetUrlVar("account",accountHash)
set resp = Server.CreateObject("Chilkat.HttpResponse")
set query = Server.CreateObject("Chilkat.HttpRequest")
query.HttpVerb = "GET"
query.AddParam "fields","positions"
success = http.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}",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
set balances = Server.CreateObject("Chilkat.JsonObject")
success = reply.ObjectOf2("securitiesAccount.currentBalances",balances)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "The account response contained no current balances.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Cash balance: " & balances.StringOf("cashBalance")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Liquidation value: " & balances.StringOf("liquidationValue")) & "</pre>"
If (balances.HasMember("buyingPower") = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Buying power: " & balances.StringOf("buyingPower")) & "</pre>"
End If
count = reply.SizeOfArray("securitiesAccount.positions")
If (count <= 0) Then
Response.Write "<pre>" & Server.HTMLEncode( "No positions.") & "</pre>"
End If
i = 0
Do While (i < count)
reply.I = i
Response.Write "<pre>" & Server.HTMLEncode( reply.StringOf("securitiesAccount.positions[i].instrument.symbol") & " long " & reply.StringOf("securitiesAccount.positions[i].longQuantity") _
& " short " & reply.StringOf("securitiesAccount.positions[i].shortQuantity") & " market value " & reply.StringOf("securitiesAccount.positions[i].marketValue")) & "</pre>"
i = i + 1
Loop
%>
</body>
</html>