Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) ETrade Place OrderThe Place Order API is used to submit an order after it has been successfully previewed. For more information, see https://apisb.etrade.com/docs/api/order/api-order-v1.html#/definition/orderPlace
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkXmlW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkHttpW http; HCkJsonObjectW jsonToken; BOOL success; const wchar_t *sandboxUrl; const wchar_t *liveUrl; HCkXmlW xml; HCkHttpResponseW resp; const wchar_t *orderType; 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 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; const wchar_t *dstFlag; int optionLevelCd; const wchar_t *marginLevelCd; int orderId; const wchar_t *placedTime; int accountId; // 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/place"; liveUrl = L"https://api.etrade.com/v1/accounts/{$accountIdKey}/orders/place"; 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 // <PlaceOrderRequest> // <orderType>EQ</orderType> // <clientOrderId>sd464333</clientOrderId> // <PreviewIds> // <previewId>730206520</previewId> // </PreviewIds> // <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>150</quantity> // </Instrument> // </Order> // </PlaceOrderRequest> xml = CkXmlW_Create(); CkXmlW_putTag(xml,L"PlaceOrderRequest"); CkXmlW_UpdateChildContent(xml,L"orderType",L"EQ"); CkXmlW_UpdateChildContent(xml,L"clientOrderId",L"sd464333"); CkXmlW_UpdateChildContent(xml,L"PreviewIds|previewId",L"730206520"); 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"150"); CkXmlW_putEmitCompact(xml,TRUE); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/xml"); resp = CkHttpW_PostXml(http,sandboxUrl,CkXmlW_getXml(xml),L"utf-8"); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkXmlW_Dispose(xml); 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); return; } // Sample XML response: // Use this online tool to generate parsing code from sample XML: // Generate Parsing Code from XML // <PlaceOrderResponse> // <orderType>EQ</orderType> // <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>150</quantity> // <cancelQuantity>0.0</cancelQuantity> // <reserveOrder>true</reserveOrder> // <reserveQuantity>0.0</reserveQuantity> // </Instrument> // <messages> // <Message> // <code>1027</code> // <description>200|The market was closed when we received your order. It has been entered into our system and will be reviewed prior to market open on the next regular trading day. After market open, please check to make sure your order was accepted.</description> // <type>WARNING</type> // </Message> // </messages> // <egQual>EG_QUAL_NOT_A_MARKET_ORDER</egQual> // <estimatedCommission>6.95</estimatedCommission> // <estimatedTotalAmount>28283.45</estimatedTotalAmount> // <netPrice>0</netPrice> // <netBid>0</netBid> // <netAsk>0</netAsk> // <gcd>0</gcd> // <ratio /> // </Order> // <dstFlag>true</dstFlag> // <optionLevelCd>4</optionLevelCd> // <marginLevelCd>MARGIN_TRADING_ALLOWED</marginLevelCd> // <OrderIds> // <orderId>5</orderId> // </OrderIds> // <placedTime>1528764717641</placedTime> // <accountId>84312767</accountId> // </PlaceOrderResponse> CkXmlW_LoadXml(xml,CkHttpResponseW_bodyStr(resp)); wprintf(L"%s\n",CkXmlW_getXml(xml)); orderType = CkXmlW_getChildContent(xml,L"orderType"); 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"); code = CkXmlW_GetChildIntValue(xml,L"Order|messages|Message|code"); description = CkXmlW_getChildContent(xml,L"Order|messages|Message|description"); v_type = CkXmlW_getChildContent(xml,L"Order|messages|Message|type"); 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"); dstFlag = CkXmlW_getChildContent(xml,L"dstFlag"); optionLevelCd = CkXmlW_GetChildIntValue(xml,L"optionLevelCd"); marginLevelCd = CkXmlW_getChildContent(xml,L"marginLevelCd"); orderId = CkXmlW_GetChildIntValue(xml,L"OrderIds|orderId"); placedTime = CkXmlW_getChildContent(xml,L"placedTime"); accountId = CkXmlW_GetChildIntValue(xml,L"accountId"); wprintf(L"Success.\n"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkXmlW_Dispose(xml); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.