Visual FoxPro Requires Chilkat v11.0.0+
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lcAccessToken
LOCAL loHttp
LOCAL loSelection
LOCAL lcAccountHash
LOCAL loResp
LOCAL loQuery
LOCAL loReply
LOCAL loBalances
LOCAL lnCount
LOCAL i
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","SchwabTrader")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","personal-desktop")
loTokens = CreateObject('Chilkat.JsonObject')
lnSuccess = loVault.GetSecretJson(loEntry,loTokens)
IF (lnSuccess <> 1) THEN
? "Could not read the token record. Check the store and complete desktop authorization first."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
CANCEL
ENDIF
lcAccessToken = loTokens.StringOf("access_token")
IF (lcAccessToken = "") THEN
? "Run the Schwab authorization or refresh example to save an access token."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = lcAccessToken
* Read the hash saved by the authorized-account selection example.
loEntry.UpdateString("service","SelectedAccount")
loSelection = CreateObject('Chilkat.JsonObject')
lnSuccess = loVault.GetSecretJson(loEntry,loSelection)
lcAccountHash = loSelection.StringOf("hashValue")
IF ((lnSuccess <> 1) OR (lcAccountHash = "")) THEN
? "Select an authorized account first."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
CANCEL
ENDIF
loHttp.SetUrlVar("account",lcAccountHash)
loResp = CreateObject('Chilkat.HttpResponse')
loQuery = CreateObject('Chilkat.HttpRequest')
loQuery.HttpVerb = "GET"
loQuery.AddParam("fields","positions")
lnSuccess = loHttp.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}",loQuery,loResp)
IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
IF (loResp.StatusCode = 0) THEN
? "No HTTP response. Check connectivity."
ELSE
? "Schwab returned HTTP " + STR(loResp.StatusCode)
IF (loResp.StatusCode = 401) THEN
? "Refresh the access token or repeat desktop authorization."
ENDIF
ENDIF
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loResp
RELEASE loQuery
CANCEL
ENDIF
loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Schwab returned unexpected JSON."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loResp
RELEASE loQuery
RELEASE loReply
CANCEL
ENDIF
loBalances = CreateObject('Chilkat.JsonObject')
lnSuccess = loReply.ObjectOf2("securitiesAccount.currentBalances",loBalances)
IF (lnSuccess <> 1) THEN
? "The account response contained no current balances."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loResp
RELEASE loQuery
RELEASE loReply
RELEASE loBalances
CANCEL
ENDIF
? "Cash balance: " + loBalances.StringOf("cashBalance")
? "Liquidation value: " + loBalances.StringOf("liquidationValue")
IF (loBalances.HasMember("buyingPower") = 1) THEN
? "Buying power: " + loBalances.StringOf("buyingPower")
ENDIF
lnCount = loReply.SizeOfArray("securitiesAccount.positions")
IF (lnCount <= 0) THEN
? "No positions."
ENDIF
i = 0
DO WHILE (i < lnCount)
loReply.I = i
? loReply.StringOf("securitiesAccount.positions[i].instrument.symbol") + " long " + loReply.StringOf("securitiesAccount.positions[i].longQuantity") + " short " + loReply.StringOf("securitiesAccount.positions[i].shortQuantity") + " market value " + loReply.StringOf("securitiesAccount.positions[i].marketValue")
i = i + 1
ENDDO
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loResp
RELEASE loQuery
RELEASE loReply
RELEASE loBalances