Tcl Requires Chilkat v11.0.0+
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
set vault [new_CkSecrets]
CkSecrets_put_Location $vault "local_manager"
CkSecrets_put_AutoSplit $vault 1
set entry [new_CkJsonObject]
CkJsonObject_UpdateString $entry "appName" "SchwabTrader"
CkJsonObject_UpdateString $entry "service" "OAuth2"
CkJsonObject_UpdateString $entry "username" "personal-desktop"
set tokens [new_CkJsonObject]
set success [CkSecrets_GetSecretJson $vault $entry $tokens]
if {$success != 1} then {
puts "Could not read the token record. Check the store and complete desktop authorization first."
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
exit
}
set accessToken [CkJsonObject_stringOf $tokens "access_token"]
if {$accessToken == ""} then {
puts "Run the Schwab authorization or refresh example to save an access token."
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
exit
}
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_AuthToken $http $accessToken
# Read the hash saved by the authorized-account selection example.
CkJsonObject_UpdateString $entry "service" "SelectedAccount"
set selection [new_CkJsonObject]
set success [CkSecrets_GetSecretJson $vault $entry $selection]
set accountHash [CkJsonObject_stringOf $selection "hashValue"]
if {expr [$success != 1] || [$accountHash == ""]} then {
puts "Select an authorized account first."
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkHttp $http
delete_CkJsonObject $selection
exit
}
CkHttp_SetUrlVar $http "account" $accountHash
set resp [new_CkHttpResponse]
set query [new_CkHttpRequest]
CkHttpRequest_put_HttpVerb $query "GET"
CkHttpRequest_AddParam $query "fields" "positions"
set success [CkHttp_HttpReq $http "https://api.schwabapi.com/trader/v1/accounts/{$account}" $query $resp]
if {expr [$success != 1] || [[CkHttpResponse_get_StatusCode $resp] != 200]} then {
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts "No HTTP response. Check connectivity."
} else {
puts "Schwab returned HTTP [CkHttpResponse_get_StatusCode $resp]"
if {[CkHttpResponse_get_StatusCode $resp] == 401} then {
puts "Refresh the access token or repeat desktop authorization."
}
}
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkHttp $http
delete_CkJsonObject $selection
delete_CkHttpResponse $resp
delete_CkHttpRequest $query
exit
}
set reply [new_CkJsonObject]
set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
puts "Schwab returned unexpected JSON."
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkHttp $http
delete_CkJsonObject $selection
delete_CkHttpResponse $resp
delete_CkHttpRequest $query
delete_CkJsonObject $reply
exit
}
set balances [new_CkJsonObject]
set success [CkJsonObject_ObjectOf2 $reply "securitiesAccount.currentBalances" $balances]
if {$success != 1} then {
puts "The account response contained no current balances."
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkHttp $http
delete_CkJsonObject $selection
delete_CkHttpResponse $resp
delete_CkHttpRequest $query
delete_CkJsonObject $reply
delete_CkJsonObject $balances
exit
}
puts "Cash balance: [CkJsonObject_stringOf $balances cashBalance]"
puts "Liquidation value: [CkJsonObject_stringOf $balances liquidationValue]"
if {[CkJsonObject_HasMember $balances "buyingPower"] == 1} then {
puts "Buying power: [CkJsonObject_stringOf $balances buyingPower]"
}
set count [CkJsonObject_SizeOfArray $reply "securitiesAccount.positions"]
if {$count <= 0} then {
puts "No positions."
}
set i 0
while {$i < $count} {
CkJsonObject_put_I $reply $i
puts [CkJsonObject_stringOf $reply {securitiesAccount.positions[i].instrument.symbol}] long [CkJsonObject_stringOf $reply {securitiesAccount.positions[i].longQuantity}] short [CkJsonObject_stringOf $reply {securitiesAccount.positions[i].shortQuantity}] market value [CkJsonObject_stringOf $reply {securitiesAccount.positions[i].marketValue}]
set i [expr $i + 1]
}
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkHttp $http
delete_CkJsonObject $selection
delete_CkHttpResponse $resp
delete_CkHttpRequest $query
delete_CkJsonObject $reply
delete_CkJsonObject $balances