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

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.

Schwab API specification

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL cAccessToken
LOCAL oHttp
LOCAL oSelection
LOCAL cAccountHash
LOCAL oResp
LOCAL oQuery
LOCAL oReply
LOCAL oBalances
LOCAL nCount
LOCAL i

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "SchwabTrader")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "personal-desktop")
oTokens := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oTokens)
IF (nSuccess != 1)
    ? "Could not read the token record. Check the store and complete desktop authorization first."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

cAccessToken := oTokens:StringOf("access_token")
IF (cAccessToken == "")
    ? "Run the Schwab authorization or refresh example to save an access token."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := cAccessToken

//  Read the hash saved by the authorized-account selection example.
oEntry:UpdateString("service", "SelectedAccount")
oSelection := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oSelection)
cAccountHash := oSelection:StringOf("hashValue")
IF ((nSuccess != 1) .OR. (cAccountHash == ""))
    ? "Select an authorized account first."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    RETURN
ENDIF

oHttp:SetUrlVar("account", cAccountHash)

oResp := CreateObject("Chilkat.HttpResponse")
oQuery := CreateObject("Chilkat.HttpRequest")
oQuery:HttpVerb := "GET"
oQuery:AddParam("fields", "positions")
nSuccess := oHttp:HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}", oQuery, oResp)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
    IF (oResp:StatusCode == 0)
        ? "No HTTP response. Check connectivity."
    ELSE
        ? "Schwab returned HTTP " + Str(oResp:StatusCode)
        IF (oResp:StatusCode == 401)
            ? "Refresh the access token or repeat desktop authorization."
        ENDIF

    ENDIF

    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oResp:destroy()
    oQuery:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Schwab returned unexpected JSON."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oResp:destroy()
    oQuery:destroy()
    oReply:destroy()
    RETURN
ENDIF

oBalances := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:ObjectOf2("securitiesAccount.currentBalances", oBalances)
IF (nSuccess != 1)
    ? "The account response contained no current balances."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oResp:destroy()
    oQuery:destroy()
    oReply:destroy()
    oBalances:destroy()
    RETURN
ENDIF

? "Cash balance: " + oBalances:StringOf("cashBalance")
? "Liquidation value: " + oBalances:StringOf("liquidationValue")
IF (oBalances:HasMember("buyingPower") == 1)
    ? "Buying power: " + oBalances:StringOf("buyingPower")
ENDIF

nCount := oReply:SizeOfArray("securitiesAccount.positions")
IF (nCount <= 0)
    ? "No positions."
ENDIF

i := 0
DO WHILE (i < nCount)
    oReply:I := i
    ? oReply:StringOf("securitiesAccount.positions[i].instrument.symbol") + "  long " + oReply:StringOf("securitiesAccount.positions[i].longQuantity") + "  short " + oReply:StringOf("securitiesAccount.positions[i].shortQuantity") + "  market value " + oReply:StringOf("securitiesAccount.positions[i].marketValue")
    i := i + 1
ENDDO

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oSelection:destroy()
oResp:destroy()
oQuery:destroy()
oReply:destroy()
oBalances:destroy()