Tcl Requires Chilkat v11.0.0+
Tcl
Schwab: Inspect an order, its legs, and fills
See more Schwab Examples
Use an order ID from the selected account’s recent orders. The example reads a single order and matches executions to order legs by legId. Each execution can have its own quantity, price, and timestamp. Working orders can have zero or partial fills. The loops cover the returned order’s own legs and activities.
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
# Supply the ID as text so large numeric identifiers keep all their digits.
set orderId "ORDER_ID_FROM_LIST"
CkHttp_SetUrlVar $http "orderId" $orderId
set resp [new_CkHttpResponse]
set success [CkHttp_HttpNoBody $http "GET" "https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}" $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
exit
}
set order [new_CkJsonObject]
set success [CkJsonObject_Load $order [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_CkJsonObject $order
exit
}
puts "Order [CkJsonObject_stringOf $order orderId] [CkJsonObject_stringOf $order status]"
set symbols [new_CkJsonObject]
set i 0
while {$i < [CkJsonObject_SizeOfArray $order "orderLegCollection"]} {
CkJsonObject_put_I $order $i
set legId [CkJsonObject_stringOf $order "orderLegCollection[i].legId"]
set symbol [CkJsonObject_stringOf $order "orderLegCollection[i].instrument.symbol"]
CkJsonObject_UpdateString $symbols $legId $symbol
puts "Leg $legId [CkJsonObject_stringOf $order {orderLegCollection[i].instruction}] [CkJsonObject_stringOf $order {orderLegCollection[i].quantity}] $symbol"
set i [expr $i + 1]
}
set fills 0
set i 0
while {$i < [CkJsonObject_SizeOfArray $order "orderActivityCollection"]} {
CkJsonObject_put_I $order $i
if {[CkJsonObject_StringOfEquals $order "orderActivityCollection[i].executionType" "FILL" 1] == 1} then {
set j 0
while {$j < [CkJsonObject_SizeOfArray $order "orderActivityCollection[i].executionLegs"]} {
CkJsonObject_put_J $order $j
set filledLeg [CkJsonObject_stringOf $order "orderActivityCollection[i].executionLegs[j].legId"]
puts "Fill on leg $filledLeg [CkJsonObject_stringOf $symbols $filledLeg] quantity [CkJsonObject_stringOf $order {orderActivityCollection[i].executionLegs[j].quantity}] price [CkJsonObject_stringOf $order {orderActivityCollection[i].executionLegs[j].price}] time [CkJsonObject_stringOf $order {orderActivityCollection[i].executionLegs[j].time}]"
set fills [expr $fills + 1]
set j [expr $j + 1]
}
}
set i [expr $i + 1]
}
if {$fills == 0} then {
puts "No fills reported for this order."
}
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkHttp $http
delete_CkJsonObject $selection
delete_CkHttpResponse $resp
delete_CkJsonObject $order
delete_CkJsonObject $symbols