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
(Tcl) 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.
load ./chilkat.dll # This example assumes the Chilkat HTTP API to have been previously unlocked. # See Global Unlock Sample for sample code. set http [new_CkHttp] CkHttp_put_OAuth1 $http 1 CkHttp_put_OAuthVerifier $http "" CkHttp_put_OAuthConsumerKey $http "ETRADE_CONSUMER_KEY" CkHttp_put_OAuthConsumerSecret $http "ETRADE_CONSUMER_SECRET" # Load the access token previously obtained via the OAuth1 3-Legged Authorization set jsonToken [new_CkJsonObject] set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/etrade.json"] if {$success != 1} then { puts "Failed to load OAuth1 token" delete_CkHttp $http delete_CkJsonObject $jsonToken exit } CkHttp_put_OAuthToken $http [CkJsonObject_stringOf $jsonToken "oauth_token"] CkHttp_put_OAuthTokenSecret $http [CkJsonObject_stringOf $jsonToken "oauth_token_secret"] # Build the JSON request body set json [new_CkJsonObject] CkJsonObject_UpdateString $json "PlaceEquityOrder.-xmlns" "http://order.etws.etrade.com" # The accountId should be an 8-digit number such as "83405188" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.accountId" "MY_ETRADE_ACCOUNT_ID" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.clientOrderId" "45" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.limitPrice" "3" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.quantity" "4" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.symbol" "ETFC" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.orderAction" "BUY" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.priceType" "LIMIT" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.marketSession" "REGULAR" CkJsonObject_UpdateString $json "PlaceEquityOrder.EquityOrderRequest.orderTerm" "GOOD_FOR_DAY" CkJsonObject_put_EmitCompact $json 0 puts [CkJsonObject_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. CkJsonObject_put_EmitCompact $json 1 # Set the Accept property to get a JSON response. CkHttp_put_Accept $http "application/json" # This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder" # resp is a CkHttpResponse set resp [CkHttp_PostJson2 $http "https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder" "application/json" [CkJsonObject_emit $json]] if {[CkHttp_get_LastMethodSuccess $http] != 1} then { puts [CkHttp_lastErrorText $http] delete_CkHttp $http delete_CkJsonObject $jsonToken delete_CkJsonObject $json exit } # Examine the response status code. set statusCode [CkHttpResponse_get_StatusCode $resp] puts "Status Code = $statusCode" # Load the JSON response body: CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp] CkJsonObject_put_EmitCompact $json 0 # If the status code was not 200, then it was an error.. if {$statusCode != 200} then { puts [CkJsonObject_emit $json] puts "Equity order failed." delete_CkHttp $http delete_CkJsonObject $jsonToken delete_CkJsonObject $json exit } puts [CkJsonObject_emit $json] delete_CkHttpResponse $resp # To examine some information from the JSON: puts "quantity: [CkJsonObject_IntOf $json PlaceEquityOrderResponse.EquityOrderResponse.quantity]" puts "msgDesc: [CkJsonObject_stringOf $json 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" # } # } # } # delete_CkHttp $http delete_CkJsonObject $jsonToken delete_CkJsonObject $json |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.