Rust
Rust
ETrade v1 Preview Order
See more HTTP Misc Examples
Gets the order details for a selected brokerage account based on the search criteria provided.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
http.set_o_auth1(true);
http.set_o_auth_verifier("");
http.set_o_auth_consumer_key("ETRADE_CONSUMER_KEY");
http.set_o_auth_consumer_secret("ETRADE_CONSUMER_SECRET");
// Load the access token previously obtained via the OAuth1 3-Legged Authorization examples Step1 and Step2.
let json = chilkat::JsonObject::new();
if json.load_file("qa_data/tokens/etrade.json").is_err() {
println!("Failed to load OAuth1 token");
return;
}
http.set_o_auth_token(&json.string_of("oauth_token").unwrap_or_default());
http.set_o_auth_token_secret(&json.string_of("oauth_token_secret").unwrap_or_default());
// See the ETrade v1 API documentation HERE.
// Sample XML Request
// Use this online tool to generate the code from sample XML:
// Generate Code to Create XML
// <?xml version="1.0" encoding="UTF-8"?>
// <PreviewOrderRequest>
// <orderType>EQ</orderType>
// <clientOrderId>sdfer333</clientOrderId>
// <Order>
// <allOrNone>false</allOrNone>
// <priceType>LIMIT</priceType>
// <orderTerm>GOOD_FOR_DAY</orderTerm>
// <marketSession>REGULAR</marketSession>
// <stopPrice />
// <limitPrice>188.51</limitPrice>
// <Instrument>
// <Product>
// <securityType>EQ</securityType>
// <symbol>FB</symbol>
// </Product>
// <orderAction>BUY</orderAction>
// <quantityType>QUANTITY</quantityType>
// <quantity>10</quantity>
// </Instrument>
// </Order>
// </PreviewOrderRequest>
let xml = chilkat::Xml::new();
xml.set_tag("PreviewOrderRequest");
xml.update_child_content("orderType", "EQ");
xml.update_child_content("clientOrderId", "sdfer333");
xml.update_child_content("Order|allOrNone", "false");
xml.update_child_content("Order|priceType", "LIMIT");
xml.update_child_content("Order|orderTerm", "GOOD_FOR_DAY");
xml.update_child_content("Order|marketSession", "REGULAR");
xml.update_child_content("Order|stopPrice", "");
xml.update_child_content("Order|limitPrice", "188.51");
xml.update_child_content("Order|Instrument|Product|securityType", "EQ");
xml.update_child_content("Order|Instrument|Product|symbol", "FB");
xml.update_child_content("Order|Instrument|orderAction", "BUY");
xml.update_child_content("Order|Instrument|quantityType", "QUANTITY");
xml.update_child_content("Order|Instrument|quantity", "10");
let xml_content = xml.get_xml().unwrap_or_default();
let endpoint_url = "https://apisb.etrade.com/v1/accounts/{accountIdKey}/orders/preview".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_str("POST", &endpoint_url, &xml_content, "utf-8", "application/xml", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
// A 200 status code indicates success.
let status_code = resp.status_code();
println!("statusCode = {}", status_code);
// Use the following online tool to generate parsing code from sample XML:
// Generate Parsing Code from XML
// A sample XML response is shown below...
let _ = xml.load_xml(&resp.body_str());
let mut i: i32 = 0;
let order_type = xml.get_child_content("orderType").unwrap_or_default();
let total_order_value = xml.get_child_content("totalOrderValue").unwrap_or_default();
let order_term = xml.get_child_content("Order|orderTerm").unwrap_or_default();
let price_type = xml.get_child_content("Order|priceType").unwrap_or_default();
let limit_price = xml.get_child_content("Order|limitPrice").unwrap_or_default();
let stop_price = xml.get_child_int_value("Order|stopPrice");
let market_session = xml.get_child_content("Order|marketSession").unwrap_or_default();
let all_or_none = xml.get_child_content("Order|allOrNone").unwrap_or_default();
let symbol = xml.get_child_content("Order|Instrument|Product|symbol").unwrap_or_default();
let security_type = xml.get_child_content("Order|Instrument|Product|securityType").unwrap_or_default();
let symbol_description = xml.get_child_content("Order|Instrument|symbolDescription").unwrap_or_default();
let order_action = xml.get_child_content("Order|Instrument|orderAction").unwrap_or_default();
let quantity_type = xml.get_child_content("Order|Instrument|quantityType").unwrap_or_default();
let quantity = xml.get_child_int_value("Order|Instrument|quantity");
let cancel_quantity = xml.get_child_content("Order|Instrument|cancelQuantity").unwrap_or_default();
let reserve_order = xml.get_child_content("Order|Instrument|reserveOrder").unwrap_or_default();
let reserve_quantity = xml.get_child_content("Order|Instrument|reserveQuantity").unwrap_or_default();
i = 0;
let count_i = xml.num_children_having_tag("Order|messages|Message");
while i < count_i {
xml.set_i(i);
let _ = xml.get_child_int_value("Order|messages|Message[i]|code");
let _ = xml.get_child_content("Order|messages|Message[i]|description").unwrap_or_default();
let _ = xml.get_child_content("Order|messages|Message[i]|type").unwrap_or_default();
i = i + 1;
}
let eg_qual = xml.get_child_content("Order|egQual").unwrap_or_default();
let estimated_commission = xml.get_child_content("Order|estimatedCommission").unwrap_or_default();
let estimated_total_amount = xml.get_child_content("Order|estimatedTotalAmount").unwrap_or_default();
let net_price = xml.get_child_int_value("Order|netPrice");
let net_bid = xml.get_child_int_value("Order|netBid");
let net_ask = xml.get_child_int_value("Order|netAsk");
let gcd = xml.get_child_int_value("Order|gcd");
let preview_id = xml.get_child_int_value("PreviewIds|previewId");
let preview_time = xml.get_child_content("previewTime").unwrap_or_default();
let dst_flag = xml.get_child_content("dstFlag").unwrap_or_default();
let account_id = xml.get_child_int_value("accountId");
let option_level_cd = xml.get_child_int_value("optionLevelCd");
let margin_level_cd = xml.get_child_content("marginLevelCd").unwrap_or_default();
let ah_disclosure_flag = xml.get_child_content("Disclosure|ahDisclosureFlag").unwrap_or_default();
let ao_disclosure_flag = xml.get_child_content("Disclosure|aoDisclosureFlag").unwrap_or_default();
let conditional_disclosure_flag = xml.get_child_content("Disclosure|conditionalDisclosureFlag").unwrap_or_default();
let eh_disclosure_flag = xml.get_child_content("Disclosure|ehDisclosureFlag").unwrap_or_default();
let margin_buying_power = xml.get_child_content("marginBuyingPower").unwrap_or_default();
// Sample XML Response
// <?xml version="1.0" encoding="UTF-8"?>
// <PreviewOrderResponse>
// <orderType>EQ</orderType>
// <totalOrderValue>1892.05</totalOrderValue>
// <Order>
// <orderTerm>GOOD_FOR_DAY</orderTerm>
// <priceType>LIMIT</priceType>
// <limitPrice>188.51</limitPrice>
// <stopPrice>0</stopPrice>
// <marketSession>REGULAR</marketSession>
// <allOrNone>false</allOrNone>
// <Instrument>
// <Product>
// <symbol>FB</symbol>
// <securityType>EQ</securityType>
// </Product>
// <symbolDescription>FACEBOOK INC CL A</symbolDescription>
// <orderAction>BUY</orderAction>
// <quantityType>QUANTITY</quantityType>
// <quantity>10</quantity>
// <cancelQuantity>0.0</cancelQuantity>
// <reserveOrder>true</reserveOrder>
// <reserveQuantity>0.0</reserveQuantity>
// </Instrument>
// <messages>
// <Message>
// <code>1042</code>
// <description>200|You have an existing open order for this security on the same side of the market. If you did not intend to place a second order for this security, please modify your order now.</description>
// <type>WARNING</type>
// </Message>
// <Message>
// <code>3093</code>
// <description>Position Concentrated.</description>
// <type>WARNING</type>
// </Message>
// </messages>
// <egQual>EG_QUAL_NOT_A_MARKET_ORDER</egQual>
// <estimatedCommission>6.95</estimatedCommission>
// <estimatedTotalAmount>1892.05</estimatedTotalAmount>
// <netPrice>0</netPrice>
// <netBid>0</netBid>
// <netAsk>0</netAsk>
// <gcd>0</gcd>
// <ratio />
// </Order>
// <PreviewIds>
// <previewId>1020563279</previewId>
// </PreviewIds>
// <previewTime>1529018458516</previewTime>
// <dstFlag>true</dstFlag>
// <accountId>84246841</accountId>
// <optionLevelCd>4</optionLevelCd>
// <marginLevelCd>MARGIN_TRADING_ALLOWED</marginLevelCd>
// <Disclosure>
// <ahDisclosureFlag>false</ahDisclosureFlag>
// <aoDisclosureFlag>false</aoDisclosureFlag>
// <conditionalDisclosureFlag>true</conditionalDisclosureFlag>
// <ehDisclosureFlag>false</ehDisclosureFlag>
// </Disclosure>
// <marginBuyingPower>86758.05</marginBuyingPower>
// </PreviewOrderResponse>