Sample code for 30+ languages & platforms
Java

ETrade Cancel Order

See more ETrade Examples

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

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // This requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        http.setOAuth1(true);
        http.setOAuthVerifier("");
        http.setOAuthConsumerKey("ETRADE_CONSUMER_KEY");
        http.setOAuthConsumerSecret("ETRADE_CONSUMER_SECRET");

        // Load the access token previously obtained via the OAuth1 Authorization
        JsonObject jsonToken = new JsonObject();
        try {
            jsonToken.loadFile("qa_data/tokens/etrade.json");
        } catch (ChilkatException e) {
            System.out.println("Failed to load OAuth1 token");
            return;
        }

        http.setOAuthToken(jsonToken.stringOf("oauth_token"));
        http.setOAuthTokenSecret(jsonToken.stringOf("oauth_token_secret"));

        String sandboxUrl = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders/cancel";
        String liveUrl = "https://api.etrade.com/v1/accounts/{$accountIdKey}/orders/cancel";

        http.setUrlVar("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>

        Xml xml = new Xml();
        xml.setTag("CancelOrderRequest");
        xml.updateChildContent("orderId", "11");
        xml.setEmitCompact(true);

        String httpRequestBody = xml.getXml();
        HttpResponse resp = new HttpResponse();
        try {
            http.httpStr("PUT", sandboxUrl, httpRequestBody, "utf-8", "application/xml", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Make sure a successful response was received.
        if (resp.getStatusCode() > 200) {
            System.out.println(resp.getStatusLine());
            System.out.println(resp.getHeader());
            System.out.println(resp.getBodyStr());
            return;
        }

        // 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.loadXml(resp.getBodyStr());
        System.out.println(xml.getXml());

        int accountId = xml.getChildIntValue("accountId");
        int orderId = xml.getChildIntValue("orderId");
        String cancelTime = xml.getChildContent("cancelTime");
        int code = xml.getChildIntValue("Messages|Message|code");
        String description = xml.getChildContent("Messages|Message|description");
        String v_type = xml.getChildContent("Messages|Message|type");

        System.out.println("Success.");
    }
}