Lua
Lua
ETrade Place Order
See more ETrade Examples
The Place Order API is used to submit an order after it has been successfully previewed.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
http.oauth1 = true
http.oauth_verifier = ""
http.oauth_consumer_key = "ETRADE_CONSUMER_KEY"
http.oauth_consumer_secret = "ETRADE_CONSUMER_SECRET"
-- Load the access token previously obtained via the OAuth1 Authorization
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/etrade.json")
if not success then
print("Failed to load OAuth1 token")
return
end
http.oauth_token = jsonToken:string_of("oauth_token")
http.oauth_token_secret = jsonToken:string_of("oauth_token_secret")
local sandboxUrl = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders/place"
local liveUrl = "https://api.etrade.com/v1/accounts/{$accountIdKey}/orders/place"
http:set_url_var("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A")
-- Send a POST with the following XML body
-- Use this online tool to generate the code from sample XML:
-- Generate Code to Create XML
-- <PlaceOrderRequest>
-- <orderType>EQ</orderType>
-- <clientOrderId>sd464333</clientOrderId>
-- <PreviewIds>
-- <previewId>730206520</previewId>
-- </PreviewIds>
-- <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>150</quantity>
-- </Instrument>
-- </Order>
-- </PlaceOrderRequest>
local xml = chilkat.Xml.new()
xml.tag = "PlaceOrderRequest"
xml:update_child_content("orderType", "EQ")
xml:update_child_content("clientOrderId", "sd464333")
xml:update_child_content("PreviewIds|previewId", "730206520")
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", "150")
xml.emit_compact = true
local resp = chilkat.HttpResponse.new()
success = http:http_str("POST", sandboxUrl, xml:get_xml(), "utf-8", "application/xml", resp)
if not success then
print(http.last_error_text)
return
end
-- Make sure a successful response was received.
if resp.status_code > 200 then
print(resp.status_line)
print(resp.header)
print(resp.body_str)
return
end
-- Sample XML response:
-- Use this online tool to generate parsing code from sample XML:
-- Generate Parsing Code from XML
-- <PlaceOrderResponse>
-- <orderType>EQ</orderType>
-- <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>150</quantity>
-- <cancelQuantity>0.0</cancelQuantity>
-- <reserveOrder>true</reserveOrder>
-- <reserveQuantity>0.0</reserveQuantity>
-- </Instrument>
-- <messages>
-- <Message>
-- <code>1027</code>
-- <description>200|The market was closed when we received your order. It has been entered into our system and will be reviewed prior to market open on the next regular trading day. After market open, please check to make sure your order was accepted.</description>
-- <type>WARNING</type>
-- </Message>
-- </messages>
-- <egQual>EG_QUAL_NOT_A_MARKET_ORDER</egQual>
-- <estimatedCommission>6.95</estimatedCommission>
-- <estimatedTotalAmount>28283.45</estimatedTotalAmount>
-- <netPrice>0</netPrice>
-- <netBid>0</netBid>
-- <netAsk>0</netAsk>
-- <gcd>0</gcd>
-- <ratio />
-- </Order>
-- <dstFlag>true</dstFlag>
-- <optionLevelCd>4</optionLevelCd>
-- <marginLevelCd>MARGIN_TRADING_ALLOWED</marginLevelCd>
-- <OrderIds>
-- <orderId>5</orderId>
-- </OrderIds>
-- <placedTime>1528764717641</placedTime>
-- <accountId>84312767</accountId>
-- </PlaceOrderResponse>
xml:load_xml(resp.body_str)
print(xml:get_xml())
local orderType = xml:get_child_content("orderType")
local orderTerm = xml:get_child_content("Order|orderTerm")
local priceType = xml:get_child_content("Order|priceType")
local limitPrice = xml:get_child_content("Order|limitPrice")
local stopPrice = xml:get_child_int_value("Order|stopPrice")
local marketSession = xml:get_child_content("Order|marketSession")
local allOrNone = xml:get_child_content("Order|allOrNone")
local symbol = xml:get_child_content("Order|Instrument|Product|symbol")
local securityType = xml:get_child_content("Order|Instrument|Product|securityType")
local symbolDescription = xml:get_child_content("Order|Instrument|symbolDescription")
local orderAction = xml:get_child_content("Order|Instrument|orderAction")
local quantityType = xml:get_child_content("Order|Instrument|quantityType")
local quantity = xml:get_child_int_value("Order|Instrument|quantity")
local cancelQuantity = xml:get_child_content("Order|Instrument|cancelQuantity")
local reserveOrder = xml:get_child_content("Order|Instrument|reserveOrder")
local reserveQuantity = xml:get_child_content("Order|Instrument|reserveQuantity")
local code = xml:get_child_int_value("Order|messages|Message|code")
local description = xml:get_child_content("Order|messages|Message|description")
local v_type = xml:get_child_content("Order|messages|Message|type")
local egQual = xml:get_child_content("Order|egQual")
local estimatedCommission = xml:get_child_content("Order|estimatedCommission")
local estimatedTotalAmount = xml:get_child_content("Order|estimatedTotalAmount")
local netPrice = xml:get_child_int_value("Order|netPrice")
local netBid = xml:get_child_int_value("Order|netBid")
local netAsk = xml:get_child_int_value("Order|netAsk")
local gcd = xml:get_child_int_value("Order|gcd")
local dstFlag = xml:get_child_content("dstFlag")
local optionLevelCd = xml:get_child_int_value("optionLevelCd")
local marginLevelCd = xml:get_child_content("marginLevelCd")
local orderId = xml:get_child_int_value("OrderIds|orderId")
local placedTime = xml:get_child_content("placedTime")
local accountId = xml:get_child_int_value("accountId")
print("Success.")