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 Equity Order (JSON version)Shows how to place an equity order using ETrade with OAuth1 authorization. See https://developer.etrade.com/ctnt/dev-portal/getDetail?contentUri=V0_Documentation-OrderAPI-PlaceEquityOrder for more information.
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkHttpW http; HCkJsonObjectW jsonToken; BOOL success; HCkJsonObjectW json; HCkHttpResponseW resp; int statusCode; // This example assumes the Chilkat HTTP 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 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")); // Build the JSON request body json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.-xmlns",L"http://order.etws.etrade.com"); // The accountId should be an 8-digit number such as "83405188" CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.accountId",L"MY_ETRADE_ACCOUNT_ID"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.clientOrderId",L"45"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.limitPrice",L"3"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.quantity",L"4"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.symbol",L"ETFC"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.orderAction",L"BUY"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.priceType",L"LIMIT"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.marketSession",L"REGULAR"); CkJsonObjectW_UpdateString(json,L"PlaceEquityOrder.EquityOrderRequest.orderTerm",L"GOOD_FOR_DAY"); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // The above code builds the following JSON: // { // "PlaceEquityOrder": { // "-xmlns": "http://order.etws.etrade.com", // "EquityOrderRequest": { // "accountId": "83405188", // "clientOrderId": "45", // "limitPrice": "3", // "quantity": "4", // "symbol": "ETFC", // "orderAction": "BUY", // "priceType": "LIMIT", // "marketSession": "REGULAR", // "orderTerm": "GOOD_FOR_DAY" // } // } // } // POST the JSON and get the response. CkJsonObjectW_putEmitCompact(json,TRUE); // Set the Accept property to get a JSON response. CkHttpW_putAccept(http,L"application/json"); // This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder" resp = CkHttpW_PostJson2(http,L"https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder",L"application/json",CkJsonObjectW_emit(json)); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkJsonObjectW_Dispose(json); return; } // Examine the response status code. statusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Status Code = %d\n",statusCode); // Load the JSON response body: CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp)); CkJsonObjectW_putEmitCompact(json,FALSE); // If the status code was not 200, then it was an error.. if (statusCode != 200) { wprintf(L"%s\n",CkJsonObjectW_emit(json)); wprintf(L"Equity order failed.\n"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkJsonObjectW_Dispose(json); return; } wprintf(L"%s\n",CkJsonObjectW_emit(json)); CkHttpResponseW_Dispose(resp); // To examine some information from the JSON: wprintf(L"quantity: %d\n",CkJsonObjectW_IntOf(json,L"PlaceEquityOrderResponse.EquityOrderResponse.quantity")); wprintf(L"msgDesc: %s\n",CkJsonObjectW_stringOf(json,L"PlaceEquityOrderResponse.EquityOrderResponse.messageList.msgDesc")); // etc .. // This is a sample response: // { // "PlaceEquityOrderResponse": { // "EquityOrderResponse": { // "accountId": 83310056, // "allOrNone": false, // "estimatedCommission": 9.99, // "estimatedTotalAmount": 1909.99, // "messageList": { // "msgDesc": "Your order was successfully entered during market hours.", // "msgCode": 1026 // }, // "orderNum": 277, // "orderTime": 1240982042179, // "quantity": 100, // "reserveOrder": false, // "reserveQuantity": 0, // "orderTerm": "GOOD_UNTIL_CANCEL", // "limitPrice": 18, // "stopPrice": 0, // "symbolDesc": "CISCO SYS INC COM", // "symbol": "CSCO", // "orderAction": "BUY", // "priceType": "LIMIT" // } // } // } // CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkJsonObjectW_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.