AutoIt Requires Chilkat v11.0.0+
AutoIt
Schwab: List recent orders for one account
See more Schwab Examples
Lists orders entered in the last seven days, with an optional status filter such as WORKING. Shows the order ID, entry time, status, filled quantity, and remaining quantity. Select an ID from the results for the order detail and fills example. An older order can still be working outside this entry-time window.
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 AutoIt Downloads
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)
; Use UTC endpoints for a rolling seven-day window.
$oClock = ObjCreate("Chilkat.CkDateTime")
$oClock.SetFromCurrentSystemTime()
$oEndTime = ObjCreate("Chilkat.StringBuilder")
$oEndTime.Append($oClock.GetAsTimestamp(False))
Local $iReplacements = $oEndTime.Replace("Z",".000Z")
$oClock.AddDays(-7)
$oStartTime = ObjCreate("Chilkat.StringBuilder")
$oStartTime.Append($oClock.GetAsTimestamp(False))
$iReplacements = $oStartTime.Replace("Z",".000Z")
Local $statusFilter = ""
$oResp = ObjCreate("Chilkat.HttpResponse")
$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "fromEnteredTime",$oStartTime.GetAsString()
$oQuery.AddParam "toEnteredTime",$oEndTime.GetAsString()
$oQuery.AddParam "maxResults","100"
If ($statusFilter <> "") Then
$oQuery.AddParam "status",$statusFilter
EndIf
$bSuccess = $oHttp.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}/orders",$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
$oOrders = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oOrders.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Schwab returned unexpected JSON." & @CRLF)
Exit
EndIf
If ($oOrders.Size = 0) Then
ConsoleWrite("No matching orders entered during this interval." & @CRLF)
EndIf
If ($oOrders.Size = 100) Then
ConsoleWrite("Reached maxResults. Narrow the interval to check for additional orders." & @CRLF)
EndIf
$oOrder = ObjCreate("Chilkat.JsonObject")
Local $i = 0
While ($i < $oOrders.Size)
$oOrders.ObjectAt2($i,$oOrder)
ConsoleWrite("Order " & $oOrder.StringOf("orderId") & " " & $oOrder.StringOf("enteredTime") & " " & $oOrder.StringOf("orderType") & " " & $oOrder.StringOf("status") & @CRLF)
ConsoleWrite(" filled " & $oOrder.StringOf("filledQuantity") & " remaining " & $oOrder.StringOf("remainingQuantity") & @CRLF)
$i = $i + 1
Wend