Lean  $LEAN_TAG$
OrderCommand.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using QuantConnect.Orders;
19 
20 namespace QuantConnect.Commands
21 {
22  /// <summary>
23  /// Represents a command to submit an order to the algorithm
24  /// </summary>
25  public class OrderCommand : BaseCommand
26  {
27  /// <summary>
28  /// Gets or sets the symbol to be ordered
29  /// </summary>
30  public Symbol Symbol { get; set; }
31 
32  /// <summary>
33  /// Gets or sets the string ticker symbol
34  /// </summary>
35  public string Ticker { get; set; }
36 
37  /// <summary>
38  /// Gets or sets the security type of the ticker.
39  /// </summary>
40  public SecurityType SecurityType { get; set; }
41 
42  /// <summary>
43  /// Gets or sets the market the ticker resides in
44  /// </summary>
45  public string Market { get; set; }
46 
47  /// <summary>
48  /// Gets or sets the order type to be submted
49  /// </summary>
50  public OrderType OrderType { get; set; }
51 
52  /// <summary>
53  /// Gets or sets the number of units to be ordered (directional)
54  /// </summary>
55  public decimal Quantity { get; set; }
56 
57  /// <summary>
58  /// Gets or sets the limit price. Only applies to <see cref="QuantConnect.Orders.OrderType.Limit"/> and <see cref="QuantConnect.Orders.OrderType.StopLimit"/>
59  /// </summary>
60  public decimal LimitPrice { get; set; }
61 
62  /// <summary>
63  /// Gets or sets the stop price. Only applies to <see cref="QuantConnect.Orders.OrderType.StopLimit"/> and <see cref="QuantConnect.Orders.OrderType.StopMarket"/>
64  /// </summary>
65  public decimal StopPrice { get; set; }
66 
67  /// <summary>
68  /// Gets or sets an arbitrary tag to be attached to the order
69  /// </summary>
70  public string Tag { get; set; }
71 
72  /// <summary>
73  /// Runs this command against the specified algorithm instance
74  /// </summary>
75  /// <param name="algorithm">The algorithm to run this command against</param>
76  public override CommandResultPacket Run(IAlgorithm algorithm)
77  {
79  var request = new SubmitOrderRequest(OrderType, Symbol.SecurityType, Symbol, Quantity, StopPrice, LimitPrice, DateTime.UtcNow, Tag);
80  var ticket = algorithm.SubmitOrderRequest(request);
81  var response = ticket.GetMostRecentOrderResponse();
82  var message = Messages.OrderCommand.CommandInfo(OrderType, Symbol, Quantity, response);
83 
84  if (response.IsError)
85  {
86  algorithm.Error(message);
87  }
88  else
89  {
90  algorithm.Debug(message);
91  }
92 
93  return new CommandResultPacket(this, success: !response.IsError);
94  }
95 
96  /// <summary>
97  /// Returns a string that represents the current object.
98  /// </summary>
99  /// <returns>
100  /// A string that represents the current object.
101  /// </returns>
102  /// <filterpriority>2</filterpriority>
103  public override string ToString()
104  {
106  // delegate to the order request
108  }
109  }
110 }