Ruby Requires Chilkat v11.0.0+
Ruby
Schwab: Inspect an order, its legs, and fills
See more Schwab Examples
Use an order ID from the selected account’s recent orders. The example reads a single order and matches executions to order legs by legId. Each execution can have its own quantity, price, and timestamp. Working orders can have zero or partial fills. The loops cover the returned order’s own legs and activities.
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)
# Supply the ID as text so large numeric identifiers keep all their digits.
orderId = "ORDER_ID_FROM_LIST"
http.SetUrlVar("orderId",orderId)
resp = Chilkat::CkHttpResponse.new()
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",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
order = Chilkat::CkJsonObject.new()
success = order.Load(resp.bodyStr())
if (success != true)
print "Schwab returned unexpected JSON." + "\n";
exit
end
print "Order " + order.stringOf("orderId") + " " + order.stringOf("status") + "\n";
symbols = Chilkat::CkJsonObject.new()
i = 0
while (i < order.SizeOfArray("orderLegCollection"))
order.put_I(i)
legId = order.stringOf("orderLegCollection[i].legId")
symbol = order.stringOf("orderLegCollection[i].instrument.symbol")
symbols.UpdateString(legId,symbol)
print "Leg " + legId + " " + order.stringOf("orderLegCollection[i].instruction") + " " + order.stringOf("orderLegCollection[i].quantity") + " " + symbol + "\n";
i = i + 1
end
fills = 0
i = 0
while (i < order.SizeOfArray("orderActivityCollection"))
order.put_I(i)
if (order.StringOfEquals("orderActivityCollection[i].executionType","FILL",true) == true)
j = 0
while (j < order.SizeOfArray("orderActivityCollection[i].executionLegs"))
order.put_J(j)
filledLeg = order.stringOf("orderActivityCollection[i].executionLegs[j].legId")
print "Fill on leg " + filledLeg + " " + symbols.stringOf(filledLeg) + " quantity " + order.stringOf("orderActivityCollection[i].executionLegs[j].quantity") +
" price " + order.stringOf("orderActivityCollection[i].executionLegs[j].price") + " time " + order.stringOf("orderActivityCollection[i].executionLegs[j].time") + "\n";
fills = fills + 1
j = j + 1
end
end
i = i + 1
end
if (fills == 0)
print "No fills reported for this order." + "\n";
end