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

Xbase++
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL cAccessToken
LOCAL oHttp
LOCAL oSelection
LOCAL cAccountHash
LOCAL cOrderId
LOCAL oResp
LOCAL oOrder
LOCAL oSymbols
LOCAL i
LOCAL cLegId
LOCAL cSymbol
LOCAL nFills
LOCAL j
LOCAL cFilledLeg

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)

//  Supply the ID as text so large numeric identifiers keep all their digits.
cOrderId := "ORDER_ID_FROM_LIST"
oHttp:SetUrlVar("orderId", cOrderId)
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", "https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}", 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()
    oResp:destroy()
    RETURN
ENDIF

oOrder := CreateObject("Chilkat.JsonObject")
nSuccess := oOrder:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Schwab returned unexpected JSON."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oResp:destroy()
    oOrder:destroy()
    RETURN
ENDIF

? "Order " + oOrder:StringOf("orderId") + "  " + oOrder:StringOf("status")
oSymbols := CreateObject("Chilkat.JsonObject")
i := 0
DO WHILE (i < oOrder:SizeOfArray("orderLegCollection"))
    oOrder:I := i
    cLegId := oOrder:StringOf("orderLegCollection[i].legId")
    cSymbol := oOrder:StringOf("orderLegCollection[i].instrument.symbol")
    oSymbols:UpdateString(cLegId, cSymbol)
    ? "Leg " + cLegId + "  " + oOrder:StringOf("orderLegCollection[i].instruction") + " " + oOrder:StringOf("orderLegCollection[i].quantity") + " " + cSymbol
    i := i + 1
ENDDO
nFills := 0
i := 0
DO WHILE (i < oOrder:SizeOfArray("orderActivityCollection"))
    oOrder:I := i
    IF (oOrder:StringOfEquals("orderActivityCollection[i].executionType", "FILL", 1) == 1)
        j := 0
        DO WHILE (j < oOrder:SizeOfArray("orderActivityCollection[i].executionLegs"))
            oOrder:J := j
            cFilledLeg := oOrder:StringOf("orderActivityCollection[i].executionLegs[j].legId")
            ? "Fill on leg " + cFilledLeg + "  " + oSymbols:StringOf(cFilledLeg) + "  quantity " + oOrder:StringOf("orderActivityCollection[i].executionLegs[j].quantity") + "  price " + oOrder:StringOf("orderActivityCollection[i].executionLegs[j].price") + "  time " + oOrder:StringOf("orderActivityCollection[i].executionLegs[j].time")
            nFills := nFills + 1
            j := j + 1
        ENDDO
    ENDIF

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

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oSelection:destroy()
oResp:destroy()
oOrder:destroy()
oSymbols:destroy()