Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) ETrade - Place Equity Order (XML version)Shows how to place an equity order using ETrade with OAuth1 authorization. This example demonstrates sending an XML request and getting an XML response (rather than JSON). See https://developer.etrade.com/ctnt/dev-portal/getDetail?contentUri=V0_Documentation-OrderAPI-PlaceEquityOrder for more information.
#include <CkHttp.h> #include <CkJsonObject.h> #include <CkXml.h> #include <CkHttpResponse.h> void ChilkatSample(void) { CkString strOut; // This example assumes the Chilkat HTTP API to have been previously unlocked. // See Global Unlock Sample for sample code. CkHttp http; http.put_OAuth1(true); http.put_OAuthVerifier(""); http.put_OAuthConsumerKey("ETRADE_CONSUMER_KEY"); http.put_OAuthConsumerSecret("ETRADE_CONSUMER_SECRET"); // Load the access token previously obtained via the OAuth1 3-Legged Authorization CkJsonObject jsonToken; bool success = jsonToken.LoadFile("qa_data/tokens/etrade.json"); if (success != true) { strOut.append("Failed to load OAuth1 token"); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } http.put_OAuthToken(jsonToken.stringOf("oauth_token")); http.put_OAuthTokenSecret(jsonToken.stringOf("oauth_token_secret")); // Build the XML request body CkXml xml; xml.put_Tag("PlaceEquityOrder"); xml.AddAttribute("xmlns","http://order.etws.etrade.com"); // The accountId should be an 8-digit number such as "83405188" xml.UpdateChildContent("EquityOrderRequest|accountId","MY_ETRADE_ACCOUNT_ID"); xml.UpdateChildContent("EquityOrderRequest|clientOrderId","45"); xml.UpdateChildContent("EquityOrderRequest|limitPrice","3"); xml.UpdateChildContent("EquityOrderRequest|previewId",""); xml.UpdateChildContent("EquityOrderRequest|stopPrice",""); xml.UpdateChildContent("EquityOrderRequest|allOrNone",""); xml.UpdateChildContent("EquityOrderRequest|quantity","4"); xml.UpdateChildContent("EquityOrderRequest|reserveOrder",""); xml.UpdateChildContent("EquityOrderRequest|reserveQuantity",""); xml.UpdateChildContent("EquityOrderRequest|symbol","ETFC"); xml.UpdateChildContent("EquityOrderRequest|orderAction","BUY"); xml.UpdateChildContent("EquityOrderRequest|priceType","LIMIT"); xml.UpdateChildContent("EquityOrderRequest|routingDestination",""); xml.UpdateChildContent("EquityOrderRequest|marketSession","REGULAR"); xml.UpdateChildContent("EquityOrderRequest|orderTerm","GOOD_FOR_DAY"); xml.put_EmitXmlDecl(false); strOut.append(xml.getXml()); strOut.append("\r\n"); // The above code builds the following XML: // <PlaceEquityOrder xmlns="http://order.etws.etrade.com"> // <EquityOrderRequest> // <accountId>83405188</accountId> // <clientOrderId>45</clientOrderId> // <limitPrice>3</limitPrice> // <previewId /> // <stopPrice /> // <allOrNone /> // <quantity>4</quantity> // <reserveOrder /> // <reserveQuantity /> // <symbol>ETFC</symbol> // <orderAction>BUY</orderAction> // <priceType>LIMIT</priceType> // <routingDestination /> // <marketSession>REGULAR</marketSession> // <orderTerm>GOOD_FOR_DAY</orderTerm> // </EquityOrderRequest> // </PlaceEquityOrder> // POST the XML and get the response. xml.put_EmitCompact(true); // Set the Accept property to get any response. http.put_Accept("application/xml"); // The PostXml method will (by default) set the Content-Type request header to "text/xml". // ETrade does not like it. Use "application/xml" instead. http.SetRequestHeader("Content-Type","application/xml"); http.SetRequestHeader("Content-Type","application/xml"); // This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder" CkHttpResponse *resp = http.PostXml("https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder",xml.getXml(),"utf-8"); if (http.get_LastMethodSuccess() != true) { strOut.append(http.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Examine the response status code. int statusCode = resp->get_StatusCode(); strOut.append("Status Code = "); strOut.appendInt(statusCode); strOut.append("\r\n"); // Load the XML response body: CkXml xmlResp; xmlResp.LoadXml(resp->bodyStr()); // If the status code was not 200, then it was an error.. if (statusCode != 200) { strOut.append(resp->bodyStr()); strOut.append("\r\n"); strOut.append("Equity order failed."); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } strOut.append(xmlResp.getXml()); strOut.append("\r\n"); delete resp; // To examine some information from the XML response: strOut.append("quantity: "); strOut.append(xmlResp.getChildContent("equityOrderResponse|quantity")); strOut.append("\r\n"); strOut.append("msgDesc: "); strOut.append(xmlResp.getChildContent("equityOrderResponse|messageList|message|msgDesc")); strOut.append("\r\n"); // etc .. // This is a sample response: // <PlaceEquityOrderResponse> // <equityOrderResponse> // <accountId>83310056</accountId> // <allOrNone>false</allOrNone> // <estimatedCommission>9.99</estimatedCommission> // <estimatedTotalAmount>1909.99</estimatedTotalAmount> // <messageList> // <message> // <msgDesc>Your order was successfully entered during market hours.</msgDesc> // <msgCode>1026</msgCode> // </message> // </messageList> // <orderNum>277</orderNum> // <orderTime>1240982042179</orderTime> // <quantity>100</quantity> // <reserveOrder>false</reserveOrder> // <reserveQuantity>0</reserveQuantity> // <orderTerm>GOOD_UNTIL_CANCEL</orderTerm> // <limitPrice>18</limitPrice> // <stopPrice>0</stopPrice> // <symbolDesc>CISCO SYS INC COM</symbolDesc> // <symbol>CSCO</symbol> // <orderAction>BUY</orderAction> // <priceType>LIMIT</priceType> // </equityOrderResponse> // </PlaceEquityOrderResponse> SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.