Sample code for 30+ languages & platforms
Lua

ETrade Cancel Order

See more ETrade Examples

The cancel order API is used to cancel an existing order.

Chilkat Lua Downloads

Lua
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/cancel"
local liveUrl = "https://api.etrade.com/v1/accounts/{$accountIdKey}/orders/cancel"

http:set_url_var("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A")

-- Send a PUT with the following XML body

-- Use this online tool to generate the code from sample XML:
-- Generate Code to Create XML

-- <CancelOrderRequest>
--    <orderId>11</orderId>
-- </CancelOrderRequest>

local xml = chilkat.Xml.new()
xml.tag = "CancelOrderRequest"
xml:update_child_content("orderId", "11")
xml.emit_compact = true

local httpRequestBody = xml:get_xml()
local resp = chilkat.HttpResponse.new()
success = http:http_str("PUT", sandboxUrl, httpRequestBody, "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

-- <CancelOrderResponse>
--    <accountId>63438617</accountId>
--    <orderId>11</orderId>
--    <cancelTime>1529563499081</cancelTime>
--    <Messages>
--       <Message>
--          <code>5011</code>
--          <description>200|Your request to cancel your order is being processed.</description>
--          <type>WARNING</type>
--       </Message>
--    </Messages>
-- </CancelOrderResponse>

xml:load_xml(resp.body_str)
print(xml:get_xml())

local accountId = xml:get_child_int_value("accountId")
local orderId = xml:get_child_int_value("orderId")
local cancelTime = xml:get_child_content("cancelTime")
local code = xml:get_child_int_value("Messages|Message|code")
local description = xml:get_child_content("Messages|Message|description")
local v_type = xml:get_child_content("Messages|Message|type")

print("Success.")