Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lcAccessToken
LOCAL loHttp
LOCAL loSelection
LOCAL lcAccountHash
LOCAL loClock
LOCAL loEndTime
LOCAL lnReplacements
LOCAL loStartTime
LOCAL lcStatusFilter
LOCAL loResp
LOCAL loQuery
LOCAL loOrders
LOCAL loOrder
LOCAL i

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0

loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","SchwabTrader")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","personal-desktop")
loTokens = CreateObject('Chilkat.JsonObject')
lnSuccess = loVault.GetSecretJson(loEntry,loTokens)
IF (lnSuccess <> 1) THEN
    ? "Could not read the token record. Check the store and complete desktop authorization first."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

lcAccessToken = loTokens.StringOf("access_token")
IF (lcAccessToken = "") THEN
    ? "Run the Schwab authorization or refresh example to save an access token."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = lcAccessToken

*  Read the hash saved by the authorized-account selection example.
loEntry.UpdateString("service","SelectedAccount")
loSelection = CreateObject('Chilkat.JsonObject')
lnSuccess = loVault.GetSecretJson(loEntry,loSelection)
lcAccountHash = loSelection.StringOf("hashValue")
IF ((lnSuccess <> 1) OR (lcAccountHash = "")) THEN
    ? "Select an authorized account first."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loSelection
    CANCEL
ENDIF

loHttp.SetUrlVar("account",lcAccountHash)

*  Use UTC endpoints for a rolling seven-day window.
loClock = CreateObject('Chilkat.CkDateTime')
loClock.SetFromCurrentSystemTime()
loEndTime = CreateObject('Chilkat.StringBuilder')
loEndTime.Append(loClock.GetAsTimestamp(0))
lnReplacements = loEndTime.Replace("Z",".000Z")
loClock.AddDays(-7)
loStartTime = CreateObject('Chilkat.StringBuilder')
loStartTime.Append(loClock.GetAsTimestamp(0))
lnReplacements = loStartTime.Replace("Z",".000Z")
lcStatusFilter = ""
loResp = CreateObject('Chilkat.HttpResponse')
loQuery = CreateObject('Chilkat.HttpRequest')
loQuery.HttpVerb = "GET"
loQuery.AddParam("fromEnteredTime",loStartTime.GetAsString())
loQuery.AddParam("toEnteredTime",loEndTime.GetAsString())
loQuery.AddParam("maxResults","100")
IF (lcStatusFilter <> "") THEN
    loQuery.AddParam("status",lcStatusFilter)
ENDIF

lnSuccess = loHttp.HttpReq("https://api.schwabapi.com/trader/v1/accounts/{$account}/orders",loQuery,loResp)
IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
    IF (loResp.StatusCode = 0) THEN
        ? "No HTTP response. Check connectivity."
    ELSE
        ? "Schwab returned HTTP " + STR(loResp.StatusCode)
        IF (loResp.StatusCode = 401) THEN
            ? "Refresh the access token or repeat desktop authorization."
        ENDIF

    ENDIF

    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loSelection
    RELEASE loClock
    RELEASE loEndTime
    RELEASE loStartTime
    RELEASE loResp
    RELEASE loQuery
    CANCEL
ENDIF

loOrders = CreateObject('Chilkat.JsonArray')
lnSuccess = loOrders.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Schwab returned unexpected JSON."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loSelection
    RELEASE loClock
    RELEASE loEndTime
    RELEASE loStartTime
    RELEASE loResp
    RELEASE loQuery
    RELEASE loOrders
    CANCEL
ENDIF

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

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

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

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loClock
RELEASE loEndTime
RELEASE loStartTime
RELEASE loResp
RELEASE loQuery
RELEASE loOrders
RELEASE loOrder