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

Schwab: Preview a single-leg equity limit order

See more Schwab Examples

Builds a day limit order for one share and posts it to previewOrder. Reads the projected order and the validation messages, including warnings and rejection reasons. A successful HTTP response means the preview completed. A preview does not submit an order, reserve buying power, or guarantee later acceptance.

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 oOrder
LOCAL oResp
LOCAL oReply
LOCAL oValidation
LOCAL oRules
LOCAL oDetail
LOCAL nMessages
LOCAL i
LOCAL cCategory
LOCAL j

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)

//  These are illustrative preview inputs. Set the symbol, quantity and limit price.
oOrder := CreateObject("Chilkat.JsonObject")
//  The PreviewOrder schema puts orderLegs inside orderStrategy.
oOrder:UpdateString("orderStrategy.session", "NORMAL")
oOrder:UpdateString("orderStrategy.duration", "DAY")
oOrder:UpdateString("orderStrategy.orderType", "LIMIT")
oOrder:UpdateString("orderStrategy.orderStrategyType", "SINGLE")
oOrder:UpdateNumber("orderStrategy.price", "100.00")
oOrder:UpdateInt("orderStrategy.quantity", 1)
oOrder:UpdateString("orderStrategy.orderLegs[0].instruction", "BUY")
oOrder:UpdateInt("orderStrategy.orderLegs[0].quantity", 1)
oOrder:UpdateString("orderStrategy.orderLegs[0].finalSymbol", "AAPL")
oOrder:UpdateString("orderStrategy.orderLegs[0].assetType", "EQUITY")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder", oOrder, "application/json", 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()
    oOrder:destroy()
    oResp:destroy()
    RETURN
ENDIF

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

? "Preview only. No order was submitted."
IF (oReply:HasMember("orderStrategy.orderType") == 1)
    ? "Projected order: " + oReply:StringOf("orderStrategy.orderType") + "  price " + oReply:StringOf("orderStrategy.price")
ENDIF

IF (oReply:HasMember("orderStrategy.orderBalance.projectedBuyingPower") == 1)
    ? "Projected buying power: " + oReply:StringOf("orderStrategy.orderBalance.projectedBuyingPower")
ENDIF

oValidation := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:ObjectOf2("orderValidationResult", oValidation)
IF (nSuccess != 1)
    ? "Schwab supplied no validation result."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oSelection:destroy()
    oOrder:destroy()
    oResp:destroy()
    oReply:destroy()
    oValidation:destroy()
    RETURN
ENDIF

//  Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
oRules := CreateObject("Chilkat.JsonArray")
oDetail := CreateObject("Chilkat.JsonObject")
nMessages := 0
i := 0
DO WHILE (i < oValidation:Size)
    cCategory := oValidation:NameAt(i)
    nSuccess := oValidation:ArrayAt2(i, oRules)
    IF (nSuccess == 1)
        j := 0
        DO WHILE (j < oRules:Size)
            oRules:ObjectAt2(j, oDetail)
            ? cCategory + "  " + oDetail:StringOf("validationRuleName") + "  " + oDetail:StringOf("message") + "  " + oDetail:StringOf("activityMessage")
            nMessages := nMessages + 1
            j := j + 1
        ENDDO
    ENDIF

    i := i + 1
ENDDO
IF (nMessages == 0)
    ? "No validation messages returned."
ENDIF

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oSelection:destroy()
oOrder:destroy()
oResp:destroy()
oReply:destroy()
oValidation:destroy()
oRules:destroy()
oDetail:destroy()