Sample code for 30+ languages & platforms
Unicode C

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 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 json;
    HCkXmlW xml;
    const wchar_t *xmlContent;
    const wchar_t *endpointUrl;
    HCkHttpResponseW resp;
    int statusCode;
    const wchar_t *tagPath;
    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 *marginBuyingPower;

    success = FALSE;

    // This example 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 3-Legged Authorization examples Step1 and Step2.
    json = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(json,L"qa_data/tokens/etrade.json");
    if (success != TRUE) {
        wprintf(L"Failed to load OAuth1 token\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    CkHttpW_putOAuthToken(http,CkJsonObjectW_stringOf(json,L"oauth_token"));
    CkHttpW_putOAuthTokenSecret(http,CkJsonObjectW_stringOf(json,L"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>

    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");

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

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpStr(http,L"POST",endpointUrl,xmlContent,L"utf-8",L"application/xml",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkXmlW_Dispose(xml);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    // A 200 status code indicates success.
    statusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"statusCode = %d\n",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...

    CkXmlW_LoadXml(xml,CkHttpResponseW_bodyStr(resp));

    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");
    marginBuyingPower = CkXmlW_getChildContent(xml,L"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>


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

    }