Sample code for 30+ languages & platforms
Unicode C

ETrade Preview Order

See more ETrade Examples

The Preview Order API is used to submit an order request for preview before placing it.

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;
    HCkHttpResponseW resp;
    const wchar_t *orderType;
    const wchar_t *totalOrderValue;
    const wchar_t *orderTerm;
    const wchar_t *priceType;
    const wchar_t *limitPrice;
    int stopPrice;
    const wchar_t *marketSession;
    const wchar_t *allOrNone;
    const wchar_t *symbol;
    const wchar_t *securityType;
    const wchar_t *symbolDescription;
    const wchar_t *orderAction;
    const wchar_t *quantityType;
    int quantity;
    const wchar_t *cancelQuantity;
    const wchar_t *reserveOrder;
    const wchar_t *reserveQuantity;
    int i;
    int count_i;
    int code;
    const wchar_t *description;
    const wchar_t *v_type;
    const wchar_t *egQual;
    const wchar_t *estimatedCommission;
    const wchar_t *estimatedTotalAmount;
    int netPrice;
    int netBid;
    int netAsk;
    int gcd;
    int previewId;
    const wchar_t *previewTime;
    const wchar_t *dstFlag;
    int accountId;
    int optionLevelCd;
    const wchar_t *marginLevelCd;
    const wchar_t *ahDisclosureFlag;
    const wchar_t *aoDisclosureFlag;
    const wchar_t *conditionalDisclosureFlag;
    const wchar_t *ehDisclosureFlag;
    const wchar_t *currentBp;
    const wchar_t *currentNetBp;
    const wchar_t *currentOor;
    const wchar_t *currentOrderImpact;
    const wchar_t *netBp;

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

    CkHttpW_SetUrlVar(http,L"accountIdKey",L"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

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

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"PreviewOrderRequest");
    CkXmlW_UpdateChildContent(xml,L"orderType",L"EQ");
    CkXmlW_UpdateChildContent(xml,L"clientOrderId",L"sdfer333");
    CkXmlW_UpdateChildContent(xml,L"Order|allOrNone",L"false");
    CkXmlW_UpdateChildContent(xml,L"Order|priceType",L"LIMIT");
    CkXmlW_UpdateChildContent(xml,L"Order|orderTerm",L"GOOD_FOR_DAY");
    CkXmlW_UpdateChildContent(xml,L"Order|marketSession",L"REGULAR");
    CkXmlW_UpdateChildContent(xml,L"Order|stopPrice",L"");
    CkXmlW_UpdateChildContent(xml,L"Order|limitPrice",L"188.51");
    CkXmlW_UpdateChildContent(xml,L"Order|Instrument|Product|securityType",L"EQ");
    CkXmlW_UpdateChildContent(xml,L"Order|Instrument|Product|symbol",L"FB");
    CkXmlW_UpdateChildContent(xml,L"Order|Instrument|orderAction",L"BUY");
    CkXmlW_UpdateChildContent(xml,L"Order|Instrument|quantityType",L"QUANTITY");
    CkXmlW_UpdateChildContent(xml,L"Order|Instrument|quantity",L"10");
    CkXmlW_putEmitCompact(xml,TRUE);

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpStr(http,L"POST",sandboxUrl,CkXmlW_getXml(xml),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

    // <?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>
    //    <cashBpDetails>
    //      <settled>
    //        <currentBp>5000.00</currentBp>
    //        <currentNetBp>5000.00</currentNetBp>
    //        <currentOor>0.00</currentOor>
    //        <currentOrderImpact>64.95</currentOrderImpact>
    //        <netBp>4935.05</netBp>
    //     </settled>
    //     <settledUnsettled>
    //       <currentBp>5000.00</currentBp>
    //       <currentNetBp>5000.00</currentNetBp>
    //       <currentOor>0.00</currentOor>
    //       <currentOrderImpact>64.95</currentOrderImpact>
    //       <netBp>4935.05</netBp>
    //       </settledUnsettled>
    //    </cashBpDetails>
    // </PreviewOrderResponse>

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

    orderType = CkXmlW_getChildContent(xml,L"orderType");
    totalOrderValue = CkXmlW_getChildContent(xml,L"totalOrderValue");
    orderTerm = CkXmlW_getChildContent(xml,L"Order|orderTerm");
    priceType = CkXmlW_getChildContent(xml,L"Order|priceType");
    limitPrice = CkXmlW_getChildContent(xml,L"Order|limitPrice");
    stopPrice = CkXmlW_GetChildIntValue(xml,L"Order|stopPrice");
    marketSession = CkXmlW_getChildContent(xml,L"Order|marketSession");
    allOrNone = CkXmlW_getChildContent(xml,L"Order|allOrNone");
    symbol = CkXmlW_getChildContent(xml,L"Order|Instrument|Product|symbol");
    securityType = CkXmlW_getChildContent(xml,L"Order|Instrument|Product|securityType");
    symbolDescription = CkXmlW_getChildContent(xml,L"Order|Instrument|symbolDescription");
    orderAction = CkXmlW_getChildContent(xml,L"Order|Instrument|orderAction");
    quantityType = CkXmlW_getChildContent(xml,L"Order|Instrument|quantityType");
    quantity = CkXmlW_GetChildIntValue(xml,L"Order|Instrument|quantity");
    cancelQuantity = CkXmlW_getChildContent(xml,L"Order|Instrument|cancelQuantity");
    reserveOrder = CkXmlW_getChildContent(xml,L"Order|Instrument|reserveOrder");
    reserveQuantity = CkXmlW_getChildContent(xml,L"Order|Instrument|reserveQuantity");
    i = 0;
    count_i = CkXmlW_NumChildrenHavingTag(xml,L"Order|messages|Message");
    while (i < count_i) {
        CkXmlW_putI(xml,i);
        code = CkXmlW_GetChildIntValue(xml,L"Order|messages|Message[i]|code");
        description = CkXmlW_getChildContent(xml,L"Order|messages|Message[i]|description");
        v_type = CkXmlW_getChildContent(xml,L"Order|messages|Message[i]|type");
        i = i + 1;
    }

    egQual = CkXmlW_getChildContent(xml,L"Order|egQual");
    estimatedCommission = CkXmlW_getChildContent(xml,L"Order|estimatedCommission");
    estimatedTotalAmount = CkXmlW_getChildContent(xml,L"Order|estimatedTotalAmount");
    netPrice = CkXmlW_GetChildIntValue(xml,L"Order|netPrice");
    netBid = CkXmlW_GetChildIntValue(xml,L"Order|netBid");
    netAsk = CkXmlW_GetChildIntValue(xml,L"Order|netAsk");
    gcd = CkXmlW_GetChildIntValue(xml,L"Order|gcd");
    previewId = CkXmlW_GetChildIntValue(xml,L"PreviewIds|previewId");
    previewTime = CkXmlW_getChildContent(xml,L"previewTime");
    dstFlag = CkXmlW_getChildContent(xml,L"dstFlag");
    accountId = CkXmlW_GetChildIntValue(xml,L"accountId");
    optionLevelCd = CkXmlW_GetChildIntValue(xml,L"optionLevelCd");
    marginLevelCd = CkXmlW_getChildContent(xml,L"marginLevelCd");
    ahDisclosureFlag = CkXmlW_getChildContent(xml,L"Disclosure|ahDisclosureFlag");
    aoDisclosureFlag = CkXmlW_getChildContent(xml,L"Disclosure|aoDisclosureFlag");
    conditionalDisclosureFlag = CkXmlW_getChildContent(xml,L"Disclosure|conditionalDisclosureFlag");
    ehDisclosureFlag = CkXmlW_getChildContent(xml,L"Disclosure|ehDisclosureFlag");
    currentBp = CkXmlW_getChildContent(xml,L"cashBpDetails|settled|currentBp");
    currentNetBp = CkXmlW_getChildContent(xml,L"cashBpDetails|settled|currentNetBp");
    currentOor = CkXmlW_getChildContent(xml,L"cashBpDetails|settled|currentOor");
    currentOrderImpact = CkXmlW_getChildContent(xml,L"cashBpDetails|settled|currentOrderImpact");
    netBp = CkXmlW_getChildContent(xml,L"cashBpDetails|settled|netBp");
    currentBp = CkXmlW_getChildContent(xml,L"cashBpDetails|settledUnsettled|currentBp");
    currentNetBp = CkXmlW_getChildContent(xml,L"cashBpDetails|settledUnsettled|currentNetBp");
    currentOor = CkXmlW_getChildContent(xml,L"cashBpDetails|settledUnsettled|currentOor");
    currentOrderImpact = CkXmlW_getChildContent(xml,L"cashBpDetails|settledUnsettled|currentOrderImpact");
    netBp = CkXmlW_getChildContent(xml,L"cashBpDetails|settledUnsettled|netBp");

    wprintf(L"Success.\n");


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

    }