Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example 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 3-Legged Authorization examples Step1 and Step2.
local json = chilkat.JsonObject.new()
local success = json:load_file("qa_data/tokens/etrade.json")
if not success then
    print("Failed to load OAuth1 token")
    return
end

http.oauth_token = json:string_of("oauth_token")
http.oauth_token_secret = json:string_of("oauth_token_secret")

-- 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>

local xml = chilkat.Xml.new()
xml.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")

local xmlContent = xml:get_xml()
local endpointUrl = "https://apisb.etrade.com/v1/accounts/{accountIdKey}/orders/preview"

local resp = chilkat.HttpResponse.new()
success = http:http_str("POST", endpointUrl, xmlContent, "utf-8", "application/xml", resp)
if not success then
    print(http.last_error_text)
    return
end

-- A 200 status code indicates success.
local statusCode = resp.status_code
print("statusCode = " .. statusCode)

-- Use the following online tool to generate parsing code from sample XML:
-- Generate Parsing Code from XML

-- A sample XML response is shown below...

xml:load_xml(resp.body_str)

local tagPath = ""
local code = 0
local description = ""
local v_type = ""

local orderType = xml:get_child_content("orderType")
local totalOrderValue = xml:get_child_content("totalOrderValue")
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 i = 0
local count_i = xml:num_children_having_tag("Order|messages|Message")
while i < count_i do
    xml.i = i
    code = xml:get_child_int_value("Order|messages|Message[i]|code")
    description = xml:get_child_content("Order|messages|Message[i]|description")
    v_type = xml:get_child_content("Order|messages|Message[i]|type")
    i = i + 1
end

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 previewId = xml:get_child_int_value("PreviewIds|previewId")
local previewTime = xml:get_child_content("previewTime")
local dstFlag = xml:get_child_content("dstFlag")
local accountId = xml:get_child_int_value("accountId")
local optionLevelCd = xml:get_child_int_value("optionLevelCd")
local marginLevelCd = xml:get_child_content("marginLevelCd")
local ahDisclosureFlag = xml:get_child_content("Disclosure|ahDisclosureFlag")
local aoDisclosureFlag = xml:get_child_content("Disclosure|aoDisclosureFlag")
local conditionalDisclosureFlag = xml:get_child_content("Disclosure|conditionalDisclosureFlag")
local ehDisclosureFlag = xml:get_child_content("Disclosure|ehDisclosureFlag")
local marginBuyingPower = xml:get_child_content("marginBuyingPower")

-- 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>