SQL Server
SQL Server
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.
Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- 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.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.JsonObject', @jsonToken OUT
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 JSON request body
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.-xmlns', 'http://order.etws.etrade.com'
-- The accountId should be an 8-digit number such as "83405188"
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.accountId', 'MY_ETRADE_ACCOUNT_ID'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.clientOrderId', '45'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.limitPrice', '3'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.quantity', '4'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.symbol', 'ETFC'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.orderAction', 'BUY'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.priceType', 'LIMIT'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.marketSession', 'REGULAR'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PlaceEquityOrder.EquityOrderRequest.orderTerm', 'GOOD_FOR_DAY'
EXEC sp_OASetProperty @json, 'EmitCompact', 0
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- 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.
EXEC sp_OASetProperty @json, 'EmitCompact', 1
-- Set the Accept property to get a JSON response.
EXEC sp_OASetProperty @http, 'Accept', 'application/json'
-- This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder"
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder', @json, 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
-- Examine the response status code.
DECLARE @statusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @statusCode OUT
PRINT 'Status Code = ' + @statusCode
-- Load the JSON response body:
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0
EXEC sp_OASetProperty @json, 'EmitCompact', 0
-- If the status code was not 200, then it was an error..
IF @statusCode <> 200
BEGIN
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Equity order failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- To examine some information from the JSON:
EXEC sp_OAMethod @json, 'IntOf', @iTmp0 OUT, 'PlaceEquityOrderResponse.EquityOrderResponse.quantity'
PRINT 'quantity: ' + @iTmp0
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'PlaceEquityOrderResponse.EquityOrderResponse.messageList.msgDesc'
PRINT 'msgDesc: ' + @sTmp0
-- 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"
-- }
-- }
-- }
--
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
END
GO