PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
string ls_AccessToken
oleobject loo_Http
oleobject loo_Selection
string ls_AccountHash
oleobject loo_Order
oleobject loo_Resp
oleobject loo_Reply
oleobject loo_Validation
oleobject loo_Rules
oleobject loo_Detail
long li_Messages
long i
string ls_Category
long j
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
destroy loo_Vault
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")
loo_Entry.UpdateString("appName","SchwabTrader")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","personal-desktop")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Success <> 1 then
Write-Debug "Could not read the token record. Check the store and complete desktop authorization first."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
ls_AccessToken = loo_Tokens.StringOf("access_token")
if ls_AccessToken = "" then
Write-Debug "Run the Schwab authorization or refresh example to save an access token."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = ls_AccessToken
// Read the hash saved by the authorized-account selection example.
loo_Entry.UpdateString("service","SelectedAccount")
loo_Selection = create oleobject
li_rc = loo_Selection.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Selection)
ls_AccountHash = loo_Selection.StringOf("hashValue")
if (li_Success <> 1) OR (ls_AccountHash = "") then
Write-Debug "Select an authorized account first."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
return
end if
loo_Http.SetUrlVar("account",ls_AccountHash)
// These are illustrative preview inputs. Set the symbol, quantity and limit price.
loo_Order = create oleobject
li_rc = loo_Order.ConnectToNewObject("Chilkat.JsonObject")
// The PreviewOrder schema puts orderLegs inside orderStrategy.
loo_Order.UpdateString("orderStrategy.session","NORMAL")
loo_Order.UpdateString("orderStrategy.duration","DAY")
loo_Order.UpdateString("orderStrategy.orderType","LIMIT")
loo_Order.UpdateString("orderStrategy.orderStrategyType","SINGLE")
loo_Order.UpdateNumber("orderStrategy.price","100.00")
loo_Order.UpdateInt("orderStrategy.quantity",1)
loo_Order.UpdateString("orderStrategy.orderLegs[0].instruction","BUY")
loo_Order.UpdateInt("orderStrategy.orderLegs[0].quantity",1)
loo_Order.UpdateString("orderStrategy.orderLegs[0].finalSymbol","AAPL")
loo_Order.UpdateString("orderStrategy.orderLegs[0].assetType","EQUITY")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpJson("POST","https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",loo_Order,"application/json",loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response. Check connectivity."
else
Write-Debug "Schwab returned HTTP " + string(loo_Resp.StatusCode)
if loo_Resp.StatusCode = 401 then
Write-Debug "Refresh the access token or repeat desktop authorization."
end if
end if
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
destroy loo_Order
destroy loo_Resp
return
end if
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Schwab returned unexpected JSON."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
destroy loo_Order
destroy loo_Resp
destroy loo_Reply
return
end if
Write-Debug "Preview only. No order was submitted."
if loo_Reply.HasMember("orderStrategy.orderType") = 1 then
Write-Debug "Projected order: " + loo_Reply.StringOf("orderStrategy.orderType") + " price " + loo_Reply.StringOf("orderStrategy.price")
end if
if loo_Reply.HasMember("orderStrategy.orderBalance.projectedBuyingPower") = 1 then
Write-Debug "Projected buying power: " + loo_Reply.StringOf("orderStrategy.orderBalance.projectedBuyingPower")
end if
loo_Validation = create oleobject
li_rc = loo_Validation.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Reply.ObjectOf2("orderValidationResult",loo_Validation)
if li_Success <> 1 then
Write-Debug "Schwab supplied no validation result."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
destroy loo_Order
destroy loo_Resp
destroy loo_Reply
destroy loo_Validation
return
end if
// Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
loo_Rules = create oleobject
li_rc = loo_Rules.ConnectToNewObject("Chilkat.JsonArray")
loo_Detail = create oleobject
li_rc = loo_Detail.ConnectToNewObject("Chilkat.JsonObject")
li_Messages = 0
i = 0
do while (i < loo_Validation.Size)
ls_Category = loo_Validation.NameAt(i)
li_Success = loo_Validation.ArrayAt2(i,loo_Rules)
if li_Success = 1 then
j = 0
do while (j < loo_Rules.Size)
loo_Rules.ObjectAt2(j,loo_Detail)
Write-Debug ls_Category + " " + loo_Detail.StringOf("validationRuleName") + " " + loo_Detail.StringOf("message") + " " + loo_Detail.StringOf("activityMessage")
li_Messages = li_Messages + 1
j = j + 1
loop
end if
i = i + 1
loop
if li_Messages = 0 then
Write-Debug "No validation messages returned."
end if
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Selection
destroy loo_Order
destroy loo_Resp
destroy loo_Reply
destroy loo_Validation
destroy loo_Rules
destroy loo_Detail