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

Visual FoxPro
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lcAccessToken
LOCAL loHttp
LOCAL loSelection
LOCAL lcAccountHash
LOCAL lcOrderId
LOCAL loResp
LOCAL loOrder
LOCAL loSymbols
LOCAL i
LOCAL lcLegId
LOCAL lcSymbol
LOCAL lnFills
LOCAL j
LOCAL lcFilledLeg

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)

*  Supply the ID as text so large numeric identifiers keep all their digits.
lcOrderId = "ORDER_ID_FROM_LIST"
loHttp.SetUrlVar("orderId",lcOrderId)
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",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 loResp
    CANCEL
ENDIF

loOrder = CreateObject('Chilkat.JsonObject')
lnSuccess = loOrder.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Schwab returned unexpected JSON."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loSelection
    RELEASE loResp
    RELEASE loOrder
    CANCEL
ENDIF

? "Order " + loOrder.StringOf("orderId") + "  " + loOrder.StringOf("status")
loSymbols = CreateObject('Chilkat.JsonObject')
i = 0
DO WHILE (i < loOrder.SizeOfArray("orderLegCollection"))
    loOrder.I = i
    lcLegId = loOrder.StringOf("orderLegCollection[i].legId")
    lcSymbol = loOrder.StringOf("orderLegCollection[i].instrument.symbol")
    loSymbols.UpdateString(lcLegId,lcSymbol)
    ? "Leg " + lcLegId + "  " + loOrder.StringOf("orderLegCollection[i].instruction") + " " + loOrder.StringOf("orderLegCollection[i].quantity") + " " + lcSymbol
    i = i + 1
ENDDO
lnFills = 0
i = 0
DO WHILE (i < loOrder.SizeOfArray("orderActivityCollection"))
    loOrder.I = i
    IF (loOrder.StringOfEquals("orderActivityCollection[i].executionType","FILL",1) = 1) THEN
        j = 0
        DO WHILE (j < loOrder.SizeOfArray("orderActivityCollection[i].executionLegs"))
            loOrder.J = j
            lcFilledLeg = loOrder.StringOf("orderActivityCollection[i].executionLegs[j].legId")
            ? "Fill on leg " + lcFilledLeg + "  " + loSymbols.StringOf(lcFilledLeg) + "  quantity " + loOrder.StringOf("orderActivityCollection[i].executionLegs[j].quantity") + "  price " + loOrder.StringOf("orderActivityCollection[i].executionLegs[j].price") + "  time " + loOrder.StringOf("orderActivityCollection[i].executionLegs[j].time")
            lnFills = lnFills + 1
            j = j + 1
        ENDDO
    ENDIF

    i = i + 1
ENDDO
IF (lnFills = 0) THEN
    ? "No fills reported for this order."
ENDIF

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loResp
RELEASE loOrder
RELEASE loSymbols