PowerShell Requires Chilkat v11.0.0+
PowerShell
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 PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = $false
$vault = New-Object Chilkat.Secrets
$vault.Location = "local_manager"
$vault.AutoSplit = $true
$entry = New-Object Chilkat.JsonObject
$entry.UpdateString("appName","SchwabTrader")
$entry.UpdateString("service","OAuth2")
$entry.UpdateString("username","personal-desktop")
$tokens = New-Object Chilkat.JsonObject
$success = $vault.GetSecretJson($entry,$tokens)
if ($success -ne $true) {
$("Could not read the token record. Check the store and complete desktop authorization first.")
exit
}
$accessToken = $tokens.StringOf("access_token")
if ($accessToken -eq "") {
$("Run the Schwab authorization or refresh example to save an access token.")
exit
}
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.AuthToken = $accessToken
# Read the hash saved by the authorized-account selection example.
$entry.UpdateString("service","SelectedAccount")
$selection = New-Object Chilkat.JsonObject
$success = $vault.GetSecretJson($entry,$selection)
$accountHash = $selection.StringOf("hashValue")
if (($success -ne $true) -or ($accountHash -eq "")) {
$("Select an authorized account first.")
exit
}
$http.SetUrlVar("account",$accountHash)
# Supply the ID as text so large numeric identifiers keep all their digits.
$orderId = "ORDER_ID_FROM_LIST"
$http.SetUrlVar("orderId",$orderId)
$resp = New-Object Chilkat.HttpResponse
$success = $http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",$resp)
if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
if ($resp.StatusCode -eq 0) {
$("No HTTP response. Check connectivity.")
}
else {
$("Schwab returned HTTP " + $resp.StatusCode)
if ($resp.StatusCode -eq 401) {
$("Refresh the access token or repeat desktop authorization.")
}
}
exit
}
$order = New-Object Chilkat.JsonObject
$success = $order.Load($resp.BodyStr)
if ($success -ne $true) {
$("Schwab returned unexpected JSON.")
exit
}
$("Order " + $order.StringOf("orderId") + " " + $order.StringOf("status"))
$symbols = New-Object Chilkat.JsonObject
$i = 0
while (($i -lt $order.SizeOfArray("orderLegCollection"))) {
$order.I = $i
$legId = $order.StringOf("orderLegCollection[i].legId")
$symbol = $order.StringOf("orderLegCollection[i].instrument.symbol")
$symbols.UpdateString($legId,$symbol)
$("Leg " + $legId + " " + $order.StringOf("orderLegCollection[i].instruction") + " " + $order.StringOf("orderLegCollection[i].quantity") + " " + $symbol)
$i = $i + 1
}
$fills = 0
$i = 0
while (($i -lt $order.SizeOfArray("orderActivityCollection"))) {
$order.I = $i
if ($order.StringOfEquals("orderActivityCollection[i].executionType","FILL",$true) -eq $true) {
$j = 0
while (($j -lt $order.SizeOfArray("orderActivityCollection[i].executionLegs"))) {
$order.J = $j
$filledLeg = $order.StringOf("orderActivityCollection[i].executionLegs[j].legId")
$("Fill on leg " + $filledLeg + " " + $symbols.StringOf($filledLeg) + " quantity " + $order.StringOf("orderActivityCollection[i].executionLegs[j].quantity")
+ " price " + $order.StringOf("orderActivityCollection[i].executionLegs[j].price") + " time " + $order.StringOf("orderActivityCollection[i].executionLegs[j].time"))
$fills = $fills + 1
$j = $j + 1
}
}
$i = $i + 1
}
if ($fills -eq 0) {
$("No fills reported for this order.")
}