Delphi ActiveX
Delphi ActiveX
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.
Chilkat Delphi ActiveX Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
http: TChilkatHttp;
jsonToken: TChilkatJsonObject;
xml: TChilkatXml;
resp: TChilkatHttpResponse;
statusCode: Integer;
xmlResp: TChilkatXml;
begin
success := 0;
// This example assumes the Chilkat HTTP API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := TChilkatHttp.Create(Self);
http.OAuth1 := 1;
http.OAuthVerifier := '';
http.OAuthConsumerKey := 'ETRADE_CONSUMER_KEY';
http.OAuthConsumerSecret := 'ETRADE_CONSUMER_SECRET';
// Load the access token previously obtained via the OAuth1 3-Legged Authorization
jsonToken := TChilkatJsonObject.Create(Self);
success := jsonToken.LoadFile('qa_data/tokens/etrade.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Failed to load OAuth1 token');
Exit;
end;
http.OAuthToken := jsonToken.StringOf('oauth_token');
http.OAuthTokenSecret := jsonToken.StringOf('oauth_token_secret');
// Build the XML request body
xml := TChilkatXml.Create(Self);
xml.Tag := 'PlaceEquityOrder';
xml.AddAttribute('xmlns','http://order.etws.etrade.com');
// The accountId should be an 8-digit number such as "83405188"
xml.UpdateChildContent('EquityOrderRequest|accountId','MY_ETRADE_ACCOUNT_ID');
xml.UpdateChildContent('EquityOrderRequest|clientOrderId','45');
xml.UpdateChildContent('EquityOrderRequest|limitPrice','3');
xml.UpdateChildContent('EquityOrderRequest|previewId','');
xml.UpdateChildContent('EquityOrderRequest|stopPrice','');
xml.UpdateChildContent('EquityOrderRequest|allOrNone','');
xml.UpdateChildContent('EquityOrderRequest|quantity','4');
xml.UpdateChildContent('EquityOrderRequest|reserveOrder','');
xml.UpdateChildContent('EquityOrderRequest|reserveQuantity','');
xml.UpdateChildContent('EquityOrderRequest|symbol','ETFC');
xml.UpdateChildContent('EquityOrderRequest|orderAction','BUY');
xml.UpdateChildContent('EquityOrderRequest|priceType','LIMIT');
xml.UpdateChildContent('EquityOrderRequest|routingDestination','');
xml.UpdateChildContent('EquityOrderRequest|marketSession','REGULAR');
xml.UpdateChildContent('EquityOrderRequest|orderTerm','GOOD_FOR_DAY');
xml.EmitXmlDecl := 0;
Memo1.Lines.Add(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 := 1;
// Set the Accept property to get any response.
http.Accept := 'application/xml';
// This example uses the sandbox URL. The live URL is "https://etws.etrade.com/order/rest/placeequityorder"
resp := TChilkatHttpResponse.Create(Self);
success := http.HttpStr('POST','https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder',xml.GetXml(),'utf-8','application/xml',resp.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
// Examine the response status code.
statusCode := resp.StatusCode;
Memo1.Lines.Add('Status Code = ' + IntToStr(statusCode));
// Load the XML response body:
xmlResp := TChilkatXml.Create(Self);
xmlResp.LoadXml(resp.BodyStr);
// If the status code was not 200, then it was an error..
if (statusCode <> 200) then
begin
Memo1.Lines.Add(resp.BodyStr);
Memo1.Lines.Add('Equity order failed.');
Exit;
end;
Memo1.Lines.Add(xmlResp.GetXml());
// To examine some information from the XML response:
Memo1.Lines.Add('quantity: ' + xmlResp.GetChildContent('equityOrderResponse|quantity'));
Memo1.Lines.Add('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>
end;