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
(PureBasic) 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.
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkXml.pb" Procedure ChilkatExample() ; This example assumes the Chilkat HTTP API to have been previously unlocked. ; See Global Unlock Sample for sample code. http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttp::setCkOAuth1(http, 1) CkHttp::setCkOAuthVerifier(http, "") CkHttp::setCkOAuthConsumerKey(http, "ETRADE_CONSUMER_KEY") CkHttp::setCkOAuthConsumerSecret(http, "ETRADE_CONSUMER_SECRET") ; Load the access token previously obtained via the OAuth1 3-Legged Authorization jsonToken.i = CkJsonObject::ckCreate() If jsonToken.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/etrade.json") If success <> 1 Debug "Failed to load OAuth1 token" CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonToken) ProcedureReturn EndIf CkHttp::setCkOAuthToken(http, CkJsonObject::ckStringOf(jsonToken,"oauth_token")) CkHttp::setCkOAuthTokenSecret(http, CkJsonObject::ckStringOf(jsonToken,"oauth_token_secret")) ; Build the XML request body xml.i = CkXml::ckCreate() If xml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xml, "PlaceEquityOrder") CkXml::ckAddAttribute(xml,"xmlns","http://order.etws.etrade.com") ; The accountId should be an 8-digit number such as "83405188" CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|accountId","MY_ETRADE_ACCOUNT_ID") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|clientOrderId","45") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|limitPrice","3") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|previewId","") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|stopPrice","") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|allOrNone","") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|quantity","4") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|reserveOrder","") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|reserveQuantity","") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|symbol","ETFC") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|orderAction","BUY") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|priceType","LIMIT") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|routingDestination","") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|marketSession","REGULAR") CkXml::ckUpdateChildContent(xml,"EquityOrderRequest|orderTerm","GOOD_FOR_DAY") CkXml::setCkEmitXmlDecl(xml, 0) Debug CkXml::ckGetXml(xml) ; 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. CkXml::setCkEmitCompact(xml, 1) ; Set the Accept property to get any response. CkHttp::setCkAccept(http, "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. CkHttp::ckSetRequestHeader(http,"Content-Type","application/xml") CkHttp::ckSetRequestHeader(http,"Content-Type","application/xml") ; This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder" resp.i = CkHttp::ckPostXml(http,"https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder",CkXml::ckGetXml(xml),"utf-8") If CkHttp::ckLastMethodSuccess(http) <> 1 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonToken) CkXml::ckDispose(xml) ProcedureReturn EndIf ; Examine the response status code. statusCode.i = CkHttpResponse::ckStatusCode(resp) Debug "Status Code = " + Str(statusCode) ; Load the XML response body: xmlResp.i = CkXml::ckCreate() If xmlResp.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::ckLoadXml(xmlResp,CkHttpResponse::ckBodyStr(resp)) ; If the status code was not 200, then it was an error.. If statusCode <> 200 Debug CkHttpResponse::ckBodyStr(resp) Debug "Equity order failed." CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonToken) CkXml::ckDispose(xml) CkXml::ckDispose(xmlResp) ProcedureReturn EndIf Debug CkXml::ckGetXml(xmlResp) CkHttpResponse::ckDispose(resp) ; To examine some information from the XML response: Debug "quantity: " + CkXml::ckGetChildContent(xmlResp,"equityOrderResponse|quantity") Debug "msgDesc: " + CkXml::ckGetChildContent(xmlResp,"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> CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonToken) CkXml::ckDispose(xml) CkXml::ckDispose(xmlResp) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.