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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False

$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","SchwabTrader")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","personal-desktop")
$oTokens = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read the token record. Check the store and complete desktop authorization first." & @CRLF)
    Exit
EndIf

Local $sAccessToken = $oTokens.StringOf("access_token")
If ($sAccessToken = "") Then
    ConsoleWrite("Run the Schwab authorization or refresh example to save an access token." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $sAccessToken

;  Read the hash saved by the authorized-account selection example.
$oEntry.UpdateString("service","SelectedAccount")
$oSelection = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oSelection)
Local $sAccountHash = $oSelection.StringOf("hashValue")
If (($bSuccess <> True) Or ($sAccountHash = "")) Then
    ConsoleWrite("Select an authorized account first." & @CRLF)
    Exit
EndIf

$oHttp.SetUrlVar("account",$sAccountHash)

;  These are illustrative preview inputs. Set the symbol, quantity and limit price.
$oOrder = ObjCreate("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 = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpJson("POST","https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",$oOrder,"application/json",$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    If ($oResp.StatusCode = 0) Then
        ConsoleWrite("No HTTP response. Check connectivity." & @CRLF)
    Else
        ConsoleWrite("Schwab returned HTTP " & $oResp.StatusCode & @CRLF)
        If ($oResp.StatusCode = 401) Then
            ConsoleWrite("Refresh the access token or repeat desktop authorization." & @CRLF)
        EndIf

    EndIf

    Exit
EndIf

$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Schwab returned unexpected JSON." & @CRLF)
    Exit
EndIf

ConsoleWrite("Preview only. No order was submitted." & @CRLF)
If ($oReply.HasMember("orderStrategy.orderType") = True) Then
    ConsoleWrite("Projected order: " & $oReply.StringOf("orderStrategy.orderType") & "  price " & $oReply.StringOf("orderStrategy.price") & @CRLF)
EndIf

If ($oReply.HasMember("orderStrategy.orderBalance.projectedBuyingPower") = True) Then
    ConsoleWrite("Projected buying power: " & $oReply.StringOf("orderStrategy.orderBalance.projectedBuyingPower") & @CRLF)
EndIf

$oValidation = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.ObjectOf2("orderValidationResult",$oValidation)
If ($bSuccess <> True) Then
    ConsoleWrite("Schwab supplied no validation result." & @CRLF)
    Exit
EndIf

;  Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
$oRules = ObjCreate("Chilkat.JsonArray")
$oDetail = ObjCreate("Chilkat.JsonObject")
Local $iMessages = 0
Local $i = 0
While ($i < $oValidation.Size)
Local $sCategory = $oValidation.NameAt($i)
    $bSuccess = $oValidation.ArrayAt2($i,$oRules)
    If ($bSuccess = True) Then
Local $iJ = 0
        While ($iJ < $oRules.Size)
            $oRules.ObjectAt2($iJ,$oDetail)
            ConsoleWrite($sCategory & "  " & $oDetail.StringOf("validationRuleName") & "  " & $oDetail.StringOf("message") & "  " & $oDetail.StringOf("activityMessage") & @CRLF)
            $iMessages = $iMessages + 1
            $iJ = $iJ + 1
        Wend
    EndIf

    $i = $i + 1
Wend
If ($iMessages = 0) Then
    ConsoleWrite("No validation messages returned." & @CRLF)
EndIf