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
(SQL Server) 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.
// Important: See this note about string length limitations for strings returned by sp_OAMethod calls. // CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int DECLARE @sTmp0 nvarchar(4000) -- This example assumes the Chilkat HTTP API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @http int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OASetProperty @http, 'OAuth1', 1 EXEC sp_OASetProperty @http, 'OAuthVerifier', '' EXEC sp_OASetProperty @http, 'OAuthConsumerKey', 'ETRADE_CONSUMER_KEY' EXEC sp_OASetProperty @http, 'OAuthConsumerSecret', 'ETRADE_CONSUMER_SECRET' -- Load the access token previously obtained via the OAuth1 3-Legged Authorization DECLARE @jsonToken int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonToken OUT DECLARE @success int EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/etrade.json' IF @success <> 1 BEGIN PRINT 'Failed to load OAuth1 token' EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @jsonToken RETURN END EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'oauth_token' EXEC sp_OASetProperty @http, 'OAuthToken', @sTmp0 EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'oauth_token_secret' EXEC sp_OASetProperty @http, 'OAuthTokenSecret', @sTmp0 -- Build the XML request body DECLARE @xml int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xml OUT EXEC sp_OASetProperty @xml, 'Tag', 'PlaceEquityOrder' EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns', 'http://order.etws.etrade.com' -- The accountId should be an 8-digit number such as "83405188" EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|accountId', 'MY_ETRADE_ACCOUNT_ID' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|clientOrderId', '45' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|limitPrice', '3' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|previewId', '' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|stopPrice', '' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|allOrNone', '' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|quantity', '4' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|reserveOrder', '' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|reserveQuantity', '' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|symbol', 'ETFC' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|orderAction', 'BUY' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|priceType', 'LIMIT' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|routingDestination', '' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|marketSession', 'REGULAR' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'EquityOrderRequest|orderTerm', 'GOOD_FOR_DAY' EXEC sp_OASetProperty @xml, 'EmitXmlDecl', 0 EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT PRINT @sTmp0 -- 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. EXEC sp_OASetProperty @xml, 'EmitCompact', 1 -- Set the Accept property to get any response. EXEC sp_OASetProperty @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. EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/xml' EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/xml' -- This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder" DECLARE @resp int EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT EXEC sp_OAMethod @http, 'PostXml', @resp OUT, 'https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder', @sTmp0, 'utf-8' EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @xml RETURN END -- Examine the response status code. DECLARE @statusCode int EXEC sp_OAGetProperty @resp, 'StatusCode', @statusCode OUT PRINT 'Status Code = ' + @statusCode -- Load the XML response body: DECLARE @xmlResp int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xmlResp OUT EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT EXEC sp_OAMethod @xmlResp, 'LoadXml', @success OUT, @sTmp0 -- If the status code was not 200, then it was an error.. IF @statusCode <> 200 BEGIN EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT PRINT @sTmp0 PRINT 'Equity order failed.' EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @xml EXEC @hr = sp_OADestroy @xmlResp RETURN END EXEC sp_OAMethod @xmlResp, 'GetXml', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @resp -- To examine some information from the XML response: EXEC sp_OAMethod @xmlResp, 'GetChildContent', @sTmp0 OUT, 'equityOrderResponse|quantity' PRINT 'quantity: ' + @sTmp0 EXEC sp_OAMethod @xmlResp, 'GetChildContent', @sTmp0 OUT, 'equityOrderResponse|messageList|message|msgDesc' PRINT 'msgDesc: ' + @sTmp0 -- 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> EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @xml EXEC @hr = sp_OADestroy @xmlResp END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.