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
(Objective-C) 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.
#import <CkoHttp.h> #import <CkoJsonObject.h> #import <CkoXml.h> #import <CkoHttpResponse.h> // This example assumes the Chilkat HTTP API to have been previously unlocked. // See Global Unlock Sample for sample code. CkoHttp *http = [[CkoHttp alloc] init]; http.OAuth1 = YES; http.OAuthVerifier = @""; http.OAuthConsumerKey = @"ETRADE_CONSUMER_KEY"; http.OAuthConsumerSecret = @"ETRADE_CONSUMER_SECRET"; // Load the access token previously obtained via the OAuth1 3-Legged Authorization CkoJsonObject *jsonToken = [[CkoJsonObject alloc] init]; BOOL success = [jsonToken LoadFile: @"qa_data/tokens/etrade.json"]; if (success != YES) { NSLog(@"%@",@"Failed to load OAuth1 token"); return; } http.OAuthToken = [jsonToken StringOf: @"oauth_token"]; http.OAuthTokenSecret = [jsonToken StringOf: @"oauth_token_secret"]; // Build the XML request body CkoXml *xml = [[CkoXml alloc] init]; xml.Tag = @"PlaceEquityOrder"; [xml AddAttribute: @"xmlns" value: @"http://order.etws.etrade.com"]; // The accountId should be an 8-digit number such as "83405188" [xml UpdateChildContent: @"EquityOrderRequest|accountId" value: @"MY_ETRADE_ACCOUNT_ID"]; [xml UpdateChildContent: @"EquityOrderRequest|clientOrderId" value: @"45"]; [xml UpdateChildContent: @"EquityOrderRequest|limitPrice" value: @"3"]; [xml UpdateChildContent: @"EquityOrderRequest|previewId" value: @""]; [xml UpdateChildContent: @"EquityOrderRequest|stopPrice" value: @""]; [xml UpdateChildContent: @"EquityOrderRequest|allOrNone" value: @""]; [xml UpdateChildContent: @"EquityOrderRequest|quantity" value: @"4"]; [xml UpdateChildContent: @"EquityOrderRequest|reserveOrder" value: @""]; [xml UpdateChildContent: @"EquityOrderRequest|reserveQuantity" value: @""]; [xml UpdateChildContent: @"EquityOrderRequest|symbol" value: @"ETFC"]; [xml UpdateChildContent: @"EquityOrderRequest|orderAction" value: @"BUY"]; [xml UpdateChildContent: @"EquityOrderRequest|priceType" value: @"LIMIT"]; [xml UpdateChildContent: @"EquityOrderRequest|routingDestination" value: @""]; [xml UpdateChildContent: @"EquityOrderRequest|marketSession" value: @"REGULAR"]; [xml UpdateChildContent: @"EquityOrderRequest|orderTerm" value: @"GOOD_FOR_DAY"]; xml.EmitXmlDecl = NO; NSLog(@"%@",[xml GetXml]); // 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.EmitCompact = YES; // Set the Accept property to get any response. http.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" value: @"application/xml"]; [http SetRequestHeader: @"Content-Type" value: @"application/xml"]; // This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder" CkoHttpResponse *resp = [http PostXml: @"https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder" xmlDoc: [xml GetXml] charset: @"utf-8"]; if (http.LastMethodSuccess != YES) { NSLog(@"%@",http.LastErrorText); return; } // Examine the response status code. int statusCode = [resp.StatusCode intValue]; NSLog(@"%@%d",@"Status Code = ",statusCode); // Load the XML response body: CkoXml *xmlResp = [[CkoXml alloc] init]; [xmlResp LoadXml: resp.BodyStr]; // If the status code was not 200, then it was an error.. if (statusCode != 200) { NSLog(@"%@",resp.BodyStr); NSLog(@"%@",@"Equity order failed."); return; } NSLog(@"%@",[xmlResp GetXml]); // To examine some information from the XML response: NSLog(@"%@%@",@"quantity: ",[xmlResp GetChildContent: @"equityOrderResponse|quantity"]); NSLog(@"%@%@",@"msgDesc: ",[xmlResp GetChildContent: @"equityOrderResponse|messageList|message|msgDesc"]); // 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> |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.