Sample code for 30+ languages & platforms
AutoIt 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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False

$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","SchwabTrader")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","personal-desktop")
$oTokens = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read the token record. Check the store and complete desktop authorization first." & @CRLF)
    Exit
EndIf

Local $sAccessToken = $oTokens.StringOf("access_token")
If ($sAccessToken = "") Then
    ConsoleWrite("Run the Schwab authorization or refresh example to save an access token." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $sAccessToken

;  Read the hash saved by the authorized-account selection example.
$oEntry.UpdateString("service","SelectedAccount")
$oSelection = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oSelection)
Local $sAccountHash = $oSelection.StringOf("hashValue")
If (($bSuccess <> True) Or ($sAccountHash = "")) Then
    ConsoleWrite("Select an authorized account first." & @CRLF)
    Exit
EndIf

$oHttp.SetUrlVar("account",$sAccountHash)

$oResp = ObjCreate("Chilkat.HttpResponse")
$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "fields","positions"
$bSuccess = $oHttp.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}",$oQuery,$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    If ($oResp.StatusCode = 0) Then
        ConsoleWrite("No HTTP response. Check connectivity." & @CRLF)
    Else
        ConsoleWrite("Schwab returned HTTP " & $oResp.StatusCode & @CRLF)
        If ($oResp.StatusCode = 401) Then
            ConsoleWrite("Refresh the access token or repeat desktop authorization." & @CRLF)
        EndIf

    EndIf

    Exit
EndIf

$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Schwab returned unexpected JSON." & @CRLF)
    Exit
EndIf

$oBalances = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.ObjectOf2("securitiesAccount.currentBalances",$oBalances)
If ($bSuccess <> True) Then
    ConsoleWrite("The account response contained no current balances." & @CRLF)
    Exit
EndIf

ConsoleWrite("Cash balance: " & $oBalances.StringOf("cashBalance") & @CRLF)
ConsoleWrite("Liquidation value: " & $oBalances.StringOf("liquidationValue") & @CRLF)
If ($oBalances.HasMember("buyingPower") = True) Then
    ConsoleWrite("Buying power: " & $oBalances.StringOf("buyingPower") & @CRLF)
EndIf

Local $iCount = $oReply.SizeOfArray("securitiesAccount.positions")
If ($iCount <= 0) Then
    ConsoleWrite("No positions." & @CRLF)
EndIf

Local $i = 0
While ($i < $iCount)
    $oReply.I = $i
    ConsoleWrite($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") & @CRLF)
    $i = $i + 1
Wend