Sample code for 30+ languages & platforms
Unicode C

ETrade Cancel Order

See more ETrade Examples

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkXmlW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW jsonToken;
    const wchar_t *sandboxUrl;
    const wchar_t *liveUrl;
    HCkXmlW xml;
    const wchar_t *httpRequestBody;
    HCkHttpResponseW resp;
    int accountId;
    int orderId;
    const wchar_t *cancelTime;
    int code;
    const wchar_t *description;
    const wchar_t *v_type;

    success = FALSE;

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    CkHttpW_putOAuth1(http,TRUE);
    CkHttpW_putOAuthVerifier(http,L"");
    CkHttpW_putOAuthConsumerKey(http,L"ETRADE_CONSUMER_KEY");
    CkHttpW_putOAuthConsumerSecret(http,L"ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 Authorization
    jsonToken = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/etrade.json");
    if (success != TRUE) {
        wprintf(L"Failed to load OAuth1 token\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(jsonToken);
        return;
    }

    CkHttpW_putOAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"oauth_token"));
    CkHttpW_putOAuthTokenSecret(http,CkJsonObjectW_stringOf(jsonToken,L"oauth_token_secret"));

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

    CkHttpW_SetUrlVar(http,L"accountIdKey",L"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 = CkXmlW_Create();
    CkXmlW_putTag(xml,L"CancelOrderRequest");
    CkXmlW_UpdateChildContent(xml,L"orderId",L"11");
    CkXmlW_putEmitCompact(xml,TRUE);

    httpRequestBody = CkXmlW_getXml(xml);
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpStr(http,L"PUT",sandboxUrl,httpRequestBody,L"utf-8",L"application/xml",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(jsonToken);
        CkXmlW_Dispose(xml);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    // Make sure a successful response was received.
    if (CkHttpResponseW_getStatusCode(resp) > 200) {
        wprintf(L"%s\n",CkHttpResponseW_statusLine(resp));
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(jsonToken);
        CkXmlW_Dispose(xml);
        CkHttpResponseW_Dispose(resp);
        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>

    CkXmlW_LoadXml(xml,CkHttpResponseW_bodyStr(resp));
    wprintf(L"%s\n",CkXmlW_getXml(xml));

    accountId = CkXmlW_GetChildIntValue(xml,L"accountId");
    orderId = CkXmlW_GetChildIntValue(xml,L"orderId");
    cancelTime = CkXmlW_getChildContent(xml,L"cancelTime");
    code = CkXmlW_GetChildIntValue(xml,L"Messages|Message|code");
    description = CkXmlW_getChildContent(xml,L"Messages|Message|description");
    v_type = CkXmlW_getChildContent(xml,L"Messages|Message|type");

    wprintf(L"Success.\n");


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(jsonToken);
    CkXmlW_Dispose(xml);
    CkHttpResponseW_Dispose(resp);

    }