PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
string ls_AccessToken
oleobject loo_Http
oleobject loo_Selection
string ls_AccountHash
oleobject loo_Clock
oleobject loo_EndTime
long li_Replacements
oleobject loo_StartTime
string ls_StatusFilter
oleobject loo_Resp
oleobject loo_Query
oleobject loo_Orders
oleobject loo_Order
long i
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
destroy loo_Vault
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")
loo_Entry.UpdateString("appName","SchwabTrader")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","personal-desktop")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Success <> 1 then
Write-Debug "Could not read the token record. Check the store and complete desktop authorization first."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
ls_AccessToken = loo_Tokens.StringOf("access_token")
if ls_AccessToken = "" then
Write-Debug "Run the Schwab authorization or refresh example to save an access token."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = ls_AccessToken
// Read the hash saved by the authorized-account selection example.
loo_Entry.UpdateString("service","SelectedAccount")
loo_Selection = create oleobject
li_rc = loo_Selection.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Selection)
ls_AccountHash = loo_Selection.StringOf("hashValue")
if (li_Success <> 1) OR (ls_AccountHash = "") then
Write-Debug "Select an authorized account first."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
return
end if
loo_Http.SetUrlVar("account",ls_AccountHash)
// Use UTC endpoints for a rolling seven-day window.
loo_Clock = create oleobject
li_rc = loo_Clock.ConnectToNewObject("Chilkat.CkDateTime")
loo_Clock.SetFromCurrentSystemTime()
loo_EndTime = create oleobject
li_rc = loo_EndTime.ConnectToNewObject("Chilkat.StringBuilder")
loo_EndTime.Append(loo_Clock.GetAsTimestamp(0))
li_Replacements = loo_EndTime.Replace("Z",".000Z")
loo_Clock.AddDays(-7)
loo_StartTime = create oleobject
li_rc = loo_StartTime.ConnectToNewObject("Chilkat.StringBuilder")
loo_StartTime.Append(loo_Clock.GetAsTimestamp(0))
li_Replacements = loo_StartTime.Replace("Z",".000Z")
ls_StatusFilter = ""
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Query = create oleobject
li_rc = loo_Query.ConnectToNewObject("Chilkat.HttpRequest")
loo_Query.HttpVerb = "GET"
loo_Query.AddParam("fromEnteredTime",loo_StartTime.GetAsString())
loo_Query.AddParam("toEnteredTime",loo_EndTime.GetAsString())
loo_Query.AddParam("maxResults","100")
if ls_StatusFilter <> "" then
loo_Query.AddParam("status",ls_StatusFilter)
end if
li_Success = loo_Http.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}/orders",loo_Query,loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response. Check connectivity."
else
Write-Debug "Schwab returned HTTP " + string(loo_Resp.StatusCode)
if loo_Resp.StatusCode = 401 then
Write-Debug "Refresh the access token or repeat desktop authorization."
end if
end if
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
destroy loo_Clock
destroy loo_EndTime
destroy loo_StartTime
destroy loo_Resp
destroy loo_Query
return
end if
loo_Orders = create oleobject
li_rc = loo_Orders.ConnectToNewObject("Chilkat.JsonArray")
li_Success = loo_Orders.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Schwab returned unexpected JSON."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
destroy loo_Clock
destroy loo_EndTime
destroy loo_StartTime
destroy loo_Resp
destroy loo_Query
destroy loo_Orders
return
end if
if loo_Orders.Size = 0 then
Write-Debug "No matching orders entered during this interval."
end if
if loo_Orders.Size = 100 then
Write-Debug "Reached maxResults. Narrow the interval to check for additional orders."
end if
loo_Order = create oleobject
li_rc = loo_Order.ConnectToNewObject("Chilkat.JsonObject")
i = 0
do while (i < loo_Orders.Size)
loo_Orders.ObjectAt2(i,loo_Order)
Write-Debug "Order " + loo_Order.StringOf("orderId") + " " + loo_Order.StringOf("enteredTime") + " " + loo_Order.StringOf("orderType") + " " + loo_Order.StringOf("status")
Write-Debug " filled " + loo_Order.StringOf("filledQuantity") + " remaining " + loo_Order.StringOf("remainingQuantity")
i = i + 1
loop
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
destroy loo_Clock
destroy loo_EndTime
destroy loo_StartTime
destroy loo_Resp
destroy loo_Query
destroy loo_Orders
destroy loo_Order