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

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.

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)

;  Supply the ID as text so large numeric identifiers keep all their digits.
Local $sOrderId = "ORDER_ID_FROM_LIST"
$oHttp.SetUrlVar("orderId",$sOrderId)
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",$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

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

ConsoleWrite("Order " & $oOrder.StringOf("orderId") & "  " & $oOrder.StringOf("status") & @CRLF)
$oSymbols = ObjCreate("Chilkat.JsonObject")
Local $i = 0
While ($i < $oOrder.SizeOfArray("orderLegCollection"))
    $oOrder.I = $i
Local $sLegId = $oOrder.StringOf("orderLegCollection[i].legId")
Local $symbol = $oOrder.StringOf("orderLegCollection[i].instrument.symbol")
    $oSymbols.UpdateString($sLegId,$symbol)
    ConsoleWrite("Leg " & $sLegId & "  " & $oOrder.StringOf("orderLegCollection[i].instruction") & " " & $oOrder.StringOf("orderLegCollection[i].quantity") & " " _
         & $symbol & @CRLF)
    $i = $i + 1
Wend
Local $iFills = 0
$i = 0
While ($i < $oOrder.SizeOfArray("orderActivityCollection"))
    $oOrder.I = $i
    If ($oOrder.StringOfEquals("orderActivityCollection[i].executionType","FILL",True) = True) Then
Local $iJ = 0
        While ($iJ < $oOrder.SizeOfArray("orderActivityCollection[i].executionLegs"))
            $oOrder.J = $iJ
Local $sFilledLeg = $oOrder.StringOf("orderActivityCollection[i].executionLegs[j].legId")
            ConsoleWrite("Fill on leg " & $sFilledLeg & "  " & $oSymbols.StringOf($sFilledLeg) & "  quantity " & $oOrder.StringOf("orderActivityCollection[i].executionLegs[j].quantity") _
                 & "  price " & $oOrder.StringOf("orderActivityCollection[i].executionLegs[j].price") & "  time " & $oOrder.StringOf("orderActivityCollection[i].executionLegs[j].time") & @CRLF)
            $iFills = $iFills + 1
            $iJ = $iJ + 1
        Wend
    EndIf

    $i = $i + 1
Wend
If ($iFills = 0) Then
    ConsoleWrite("No fills reported for this order." & @CRLF)
EndIf