Visual FoxPro Requires Chilkat v11.0.0+
Visual FoxPro
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.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lcAccessToken
LOCAL loHttp
LOCAL loSelection
LOCAL lcAccountHash
LOCAL loOrder
LOCAL loResp
LOCAL loReply
LOCAL loValidation
LOCAL loRules
LOCAL loDetail
LOCAL lnMessages
LOCAL i
LOCAL lcCategory
LOCAL j
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)
* These are illustrative preview inputs. Set the symbol, quantity and limit price.
loOrder = CreateObject('Chilkat.JsonObject')
* The PreviewOrder schema puts orderLegs inside orderStrategy.
loOrder.UpdateString("orderStrategy.session","NORMAL")
loOrder.UpdateString("orderStrategy.duration","DAY")
loOrder.UpdateString("orderStrategy.orderType","LIMIT")
loOrder.UpdateString("orderStrategy.orderStrategyType","SINGLE")
loOrder.UpdateNumber("orderStrategy.price","100.00")
loOrder.UpdateInt("orderStrategy.quantity",1)
loOrder.UpdateString("orderStrategy.orderLegs[0].instruction","BUY")
loOrder.UpdateInt("orderStrategy.orderLegs[0].quantity",1)
loOrder.UpdateString("orderStrategy.orderLegs[0].finalSymbol","AAPL")
loOrder.UpdateString("orderStrategy.orderLegs[0].assetType","EQUITY")
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpJson("POST","https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",loOrder,"application/json",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 loOrder
RELEASE loResp
CANCEL
ENDIF
loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Schwab returned unexpected JSON."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loOrder
RELEASE loResp
RELEASE loReply
CANCEL
ENDIF
? "Preview only. No order was submitted."
IF (loReply.HasMember("orderStrategy.orderType") = 1) THEN
? "Projected order: " + loReply.StringOf("orderStrategy.orderType") + " price " + loReply.StringOf("orderStrategy.price")
ENDIF
IF (loReply.HasMember("orderStrategy.orderBalance.projectedBuyingPower") = 1) THEN
? "Projected buying power: " + loReply.StringOf("orderStrategy.orderBalance.projectedBuyingPower")
ENDIF
loValidation = CreateObject('Chilkat.JsonObject')
lnSuccess = loReply.ObjectOf2("orderValidationResult",loValidation)
IF (lnSuccess <> 1) THEN
? "Schwab supplied no validation result."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loOrder
RELEASE loResp
RELEASE loReply
RELEASE loValidation
CANCEL
ENDIF
* Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
loRules = CreateObject('Chilkat.JsonArray')
loDetail = CreateObject('Chilkat.JsonObject')
lnMessages = 0
i = 0
DO WHILE (i < loValidation.Size)
lcCategory = loValidation.NameAt(i)
lnSuccess = loValidation.ArrayAt2(i,loRules)
IF (lnSuccess = 1) THEN
j = 0
DO WHILE (j < loRules.Size)
loRules.ObjectAt2(j,loDetail)
? lcCategory + " " + loDetail.StringOf("validationRuleName") + " " + loDetail.StringOf("message") + " " + loDetail.StringOf("activityMessage")
lnMessages = lnMessages + 1
j = j + 1
ENDDO
ENDIF
i = i + 1
ENDDO
IF (lnMessages = 0) THEN
? "No validation messages returned."
ENDIF
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loSelection
RELEASE loOrder
RELEASE loResp
RELEASE loReply
RELEASE loValidation
RELEASE loRules
RELEASE loDetail