Ruby Requires Chilkat v11.0.0+
Ruby
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 Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
vault = Chilkat::CkSecrets.new()
vault.put_Location("local_manager")
vault.put_AutoSplit(true)
entry = Chilkat::CkJsonObject.new()
entry.UpdateString("appName","SchwabTrader")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","personal-desktop")
tokens = Chilkat::CkJsonObject.new()
success = vault.GetSecretJson(entry,tokens)
if (success != true)
print "Could not read the token record. Check the store and complete desktop authorization first." + "\n";
exit
end
accessToken = tokens.stringOf("access_token")
if (accessToken == "")
print "Run the Schwab authorization or refresh example to save an access token." + "\n";
exit
end
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_AuthToken(accessToken)
# Read the hash saved by the authorized-account selection example.
entry.UpdateString("service","SelectedAccount")
selection = Chilkat::CkJsonObject.new()
success = vault.GetSecretJson(entry,selection)
accountHash = selection.stringOf("hashValue")
if ((success != true) or (accountHash == ""))
print "Select an authorized account first." + "\n";
exit
end
http.SetUrlVar("account",accountHash)
# These are illustrative preview inputs. Set the symbol, quantity and limit price.
order = Chilkat::CkJsonObject.new()
# 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 = Chilkat::CkHttpResponse.new()
success = http.HttpJson("POST","https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",order,"application/json",resp)
if ((success != true) or (resp.get_StatusCode() != 200))
if (resp.get_StatusCode() == 0)
print "No HTTP response. Check connectivity." + "\n";
else
print "Schwab returned HTTP " + resp.get_StatusCode().to_s() + "\n";
if (resp.get_StatusCode() == 401)
print "Refresh the access token or repeat desktop authorization." + "\n";
end
end
exit
end
reply = Chilkat::CkJsonObject.new()
success = reply.Load(resp.bodyStr())
if (success != true)
print "Schwab returned unexpected JSON." + "\n";
exit
end
print "Preview only. No order was submitted." + "\n";
if (reply.HasMember("orderStrategy.orderType") == true)
print "Projected order: " + reply.stringOf("orderStrategy.orderType") + " price " + reply.stringOf("orderStrategy.price") + "\n";
end
if (reply.HasMember("orderStrategy.orderBalance.projectedBuyingPower") == true)
print "Projected buying power: " + reply.stringOf("orderStrategy.orderBalance.projectedBuyingPower") + "\n";
end
validation = Chilkat::CkJsonObject.new()
success = reply.ObjectOf2("orderValidationResult",validation)
if (success != true)
print "Schwab supplied no validation result." + "\n";
exit
end
# Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
rules = Chilkat::CkJsonArray.new()
detail = Chilkat::CkJsonObject.new()
messages = 0
i = 0
while (i < validation.get_Size())
category = validation.nameAt(i)
success = validation.ArrayAt2(i,rules)
if (success == true)
j = 0
while (j < rules.get_Size())
rules.ObjectAt2(j,detail)
print category + " " + detail.stringOf("validationRuleName") + " " + detail.stringOf("message") + " " + detail.stringOf("activityMessage") + "\n";
messages = messages + 1
j = j + 1
end
end
i = i + 1
end
if (messages == 0)
print "No validation messages returned." + "\n";
end