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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = $false

$vault = New-Object Chilkat.Secrets
$vault.Location = "local_manager"
$vault.AutoSplit = $true
$entry = New-Object Chilkat.JsonObject
$entry.UpdateString("appName","SchwabTrader")
$entry.UpdateString("service","OAuth2")
$entry.UpdateString("username","personal-desktop")
$tokens = New-Object Chilkat.JsonObject
$success = $vault.GetSecretJson($entry,$tokens)
if ($success -ne $true) {
    $("Could not read the token record. Check the store and complete desktop authorization first.")
    exit
}

$accessToken = $tokens.StringOf("access_token")
if ($accessToken -eq "") {
    $("Run the Schwab authorization or refresh example to save an access token.")
    exit
}

$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.AuthToken = $accessToken

#  Read the hash saved by the authorized-account selection example.
$entry.UpdateString("service","SelectedAccount")
$selection = New-Object Chilkat.JsonObject
$success = $vault.GetSecretJson($entry,$selection)
$accountHash = $selection.StringOf("hashValue")
if (($success -ne $true) -or ($accountHash -eq "")) {
    $("Select an authorized account first.")
    exit
}

$http.SetUrlVar("account",$accountHash)

#  These are illustrative preview inputs. Set the symbol, quantity and limit price.
$order = New-Object Chilkat.JsonObject
#  The PreviewOrder schema puts orderLegs inside orderStrategy.
$order.UpdateString("orderStrategy.session","NORMAL")
$order.UpdateString("orderStrategy.duration","DAY")
$order.UpdateString("orderStrategy.orderType","LIMIT")
$order.UpdateString("orderStrategy.orderStrategyType","SINGLE")
$order.UpdateNumber("orderStrategy.price","100.00")
$order.UpdateInt("orderStrategy.quantity",1)
$order.UpdateString("orderStrategy.orderLegs[0].instruction","BUY")
$order.UpdateInt("orderStrategy.orderLegs[0].quantity",1)
$order.UpdateString("orderStrategy.orderLegs[0].finalSymbol","AAPL")
$order.UpdateString("orderStrategy.orderLegs[0].assetType","EQUITY")
$resp = New-Object Chilkat.HttpResponse
$success = $http.HttpJson("POST","https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",$order,"application/json",$resp)
if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
    if ($resp.StatusCode -eq 0) {
        $("No HTTP response. Check connectivity.")
    }
    else {
        $("Schwab returned HTTP " + $resp.StatusCode)
        if ($resp.StatusCode -eq 401) {
            $("Refresh the access token or repeat desktop authorization.")
        }

    }

    exit
}

$reply = New-Object Chilkat.JsonObject
$success = $reply.Load($resp.BodyStr)
if ($success -ne $true) {
    $("Schwab returned unexpected JSON.")
    exit
}

$("Preview only. No order was submitted.")
if ($reply.HasMember("orderStrategy.orderType") -eq $true) {
    $("Projected order: " + $reply.StringOf("orderStrategy.orderType") + "  price " + $reply.StringOf("orderStrategy.price"))
}

if ($reply.HasMember("orderStrategy.orderBalance.projectedBuyingPower") -eq $true) {
    $("Projected buying power: " + $reply.StringOf("orderStrategy.orderBalance.projectedBuyingPower"))
}

$validation = New-Object Chilkat.JsonObject
$success = $reply.ObjectOf2("orderValidationResult",$validation)
if ($success -ne $true) {
    $("Schwab supplied no validation result.")
    exit
}

#  Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
$rules = New-Object Chilkat.JsonArray
$detail = New-Object Chilkat.JsonObject
$messages = 0
$i = 0
while (($i -lt $validation.Size)) {
    $category = $validation.NameAt($i)
    $success = $validation.ArrayAt2($i,$rules)
    if ($success -eq $true) {
        $j = 0
        while (($j -lt $rules.Size)) {
            $rules.ObjectAt2($j,$detail)
            $($category + "  " + $detail.StringOf("validationRuleName") + "  " + $detail.StringOf("message") + "  " + $detail.StringOf("activityMessage"))
            $messages = $messages + 1
            $j = $j + 1
        }

    }

    $i = $i + 1
}

if ($messages -eq 0) {
    $("No validation messages returned.")
}