Xojo Plugin Requires Chilkat v11.0.0+
Xojo Plugin
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 Xojo Plugin Downloads
Dim success As Boolean
success = False
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim vault As New Chilkat.Secrets
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As New Chilkat.JsonObject
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
Dim tokens As New Chilkat.JsonObject
success = vault.GetSecretJson(entry,tokens)
If (success <> True) Then
System.DebugLog("Could not read the token record. Check the store and complete desktop authorization first.")
Return
End If
Dim accessToken As String
accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
System.DebugLog("Run the Schwab authorization or refresh example to save an access token.")
Return
End If
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = accessToken
// Read the hash saved by the authorized-account selection example.
success = entry.UpdateString("service","SelectedAccount")
Dim selection As New Chilkat.JsonObject
success = vault.GetSecretJson(entry,selection)
Dim accountHash As String
accountHash = selection.StringOf("hashValue")
If ((success <> True) Or (accountHash = "")) Then
System.DebugLog("Select an authorized account first.")
Return
End If
success = http.SetUrlVar("account",accountHash)
// These are illustrative preview inputs. Set the symbol, quantity and limit price.
Dim order As New Chilkat.JsonObject
// The PreviewOrder schema puts orderLegs inside orderStrategy.
success = order.UpdateString("orderStrategy.session","NORMAL")
success = order.UpdateString("orderStrategy.duration","DAY")
success = order.UpdateString("orderStrategy.orderType","LIMIT")
success = order.UpdateString("orderStrategy.orderStrategyType","SINGLE")
success = order.UpdateNumber("orderStrategy.price","100.00")
success = order.UpdateInt("orderStrategy.quantity",1)
success = order.UpdateString("orderStrategy.orderLegs[0].instruction","BUY")
success = order.UpdateInt("orderStrategy.orderLegs[0].quantity",1)
success = order.UpdateString("orderStrategy.orderLegs[0].finalSymbol","AAPL")
success = order.UpdateString("orderStrategy.orderLegs[0].assetType","EQUITY")
Dim resp As New Chilkat.HttpResponse
success = http.HttpJson("POST","https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",order,"application/json",resp)
If ((success <> True) Or (resp.StatusCode <> 200)) Then
If (resp.StatusCode = 0) Then
System.DebugLog("No HTTP response. Check connectivity.")
Else
System.DebugLog("Schwab returned HTTP " + Str(resp.StatusCode))
If (resp.StatusCode = 401) Then
System.DebugLog("Refresh the access token or repeat desktop authorization.")
End If
End If
Return
End If
Dim reply As New Chilkat.JsonObject
success = reply.Load(resp.BodyStr)
If (success <> True) Then
System.DebugLog("Schwab returned unexpected JSON.")
Return
End If
System.DebugLog("Preview only. No order was submitted.")
If (reply.HasMember("orderStrategy.orderType") = True) Then
System.DebugLog("Projected order: " + reply.StringOf("orderStrategy.orderType") + " price " + reply.StringOf("orderStrategy.price"))
End If
If (reply.HasMember("orderStrategy.orderBalance.projectedBuyingPower") = True) Then
System.DebugLog("Projected buying power: " + reply.StringOf("orderStrategy.orderBalance.projectedBuyingPower"))
End If
Dim validation As New Chilkat.JsonObject
success = reply.ObjectOf2("orderValidationResult",validation)
If (success <> True) Then
System.DebugLog("Schwab supplied no validation result.")
Return
End If
// Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
Dim rules As New Chilkat.JsonArray
Dim detail As New Chilkat.JsonObject
Dim messages As Int32
messages = 0
Dim i As Int32
i = 0
While (i < validation.Size)
Dim category As String
category = validation.NameAt(i)
success = validation.ArrayAt2(i,rules)
If (success = True) Then
Dim j As Int32
j = 0
While (j < rules.Size)
success = rules.ObjectAt2(j,detail)
System.DebugLog(category + " " + detail.StringOf("validationRuleName") + " " + detail.StringOf("message") + " " + detail.StringOf("activityMessage"))
messages = messages + 1
j = j + 1
Wend
End If
i = i + 1
Wend
If (messages = 0) Then
System.DebugLog("No validation messages returned.")
End If