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

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.

Schwab API specification

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL cAccessToken
LOCAL oHttp
LOCAL oSelection
LOCAL cAccountHash
LOCAL oClock
LOCAL oEndTime
LOCAL nReplacements
LOCAL oStartTime
LOCAL cStatusFilter
LOCAL oResp
LOCAL oQuery
LOCAL oOrders
LOCAL oOrder
LOCAL i

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "SchwabTrader")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "personal-desktop")
oTokens := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oTokens)
IF (nSuccess != 1)
    ? "Could not read the token record. Check the store and complete desktop authorization first."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

cAccessToken := oTokens:StringOf("access_token")
IF (cAccessToken == "")
    ? "Run the Schwab authorization or refresh example to save an access token."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := cAccessToken

//  Read the hash saved by the authorized-account selection example.
oEntry:UpdateString("service", "SelectedAccount")
oSelection := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oSelection)
cAccountHash := oSelection:StringOf("hashValue")
IF ((nSuccess != 1) .OR. (cAccountHash == ""))
    ? "Select an authorized account first."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    RETURN
ENDIF

oHttp:SetUrlVar("account", cAccountHash)

//  Use UTC endpoints for a rolling seven-day window.
oClock := CreateObject("Chilkat.CkDateTime")
oClock:SetFromCurrentSystemTime()
oEndTime := CreateObject("Chilkat.StringBuilder")
oEndTime:Append(oClock:GetAsTimestamp(0))
nReplacements := oEndTime:Replace("Z", ".000Z")
oClock:AddDays(-7)
oStartTime := CreateObject("Chilkat.StringBuilder")
oStartTime:Append(oClock:GetAsTimestamp(0))
nReplacements := oStartTime:Replace("Z", ".000Z")
cStatusFilter := ""
oResp := CreateObject("Chilkat.HttpResponse")
oQuery := CreateObject("Chilkat.HttpRequest")
oQuery:HttpVerb := "GET"
oQuery:AddParam("fromEnteredTime", oStartTime:GetAsString())
oQuery:AddParam("toEnteredTime", oEndTime:GetAsString())
oQuery:AddParam("maxResults", "100")
IF (cStatusFilter != "")
    oQuery:AddParam("status", cStatusFilter)
ENDIF

nSuccess := oHttp:HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}/orders", oQuery, oResp)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
    IF (oResp:StatusCode == 0)
        ? "No HTTP response. Check connectivity."
    ELSE
        ? "Schwab returned HTTP " + Str(oResp:StatusCode)
        IF (oResp:StatusCode == 401)
            ? "Refresh the access token or repeat desktop authorization."
        ENDIF

    ENDIF

    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oClock:destroy()
    oEndTime:destroy()
    oStartTime:destroy()
    oResp:destroy()
    oQuery:destroy()
    RETURN
ENDIF

oOrders := CreateObject("Chilkat.JsonArray")
nSuccess := oOrders:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Schwab returned unexpected JSON."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oClock:destroy()
    oEndTime:destroy()
    oStartTime:destroy()
    oResp:destroy()
    oQuery:destroy()
    oOrders:destroy()
    RETURN
ENDIF

IF (oOrders:Size == 0)
    ? "No matching orders entered during this interval."
ENDIF

IF (oOrders:Size == 100)
    ? "Reached maxResults. Narrow the interval to check for additional orders."
ENDIF

oOrder := CreateObject("Chilkat.JsonObject")
i := 0
DO WHILE (i < oOrders:Size)
    oOrders:ObjectAt2(i, oOrder)
    ? "Order " + oOrder:StringOf("orderId") + "  " + oOrder:StringOf("enteredTime") + "  " + oOrder:StringOf("orderType") + "  " + oOrder:StringOf("status")
    ? "  filled " + oOrder:StringOf("filledQuantity") + "  remaining " + oOrder:StringOf("remainingQuantity")
    i := i + 1
ENDDO

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oSelection:destroy()
oClock:destroy()
oEndTime:destroy()
oStartTime:destroy()
oResp:destroy()
oQuery:destroy()
oOrders:destroy()
oOrder:destroy()