Lean  $LEAN_TAG$
OrderResponse.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 namespace QuantConnect.Orders
17 {
18  /// <summary>
19  /// Represents a response to an <see cref="OrderRequest"/>. See <see cref="OrderRequest.Response"/> property for
20  /// a specific request's response value
21  /// </summary>
22  public class OrderResponse
23  {
24  /// <summary>
25  /// Gets the order id
26  /// </summary>
27  public int OrderId
28  {
29  get; private set;
30  }
31 
32  /// <summary>
33  /// Gets the error message if the <see cref="ErrorCode"/> does not equal <see cref="OrderResponseErrorCode.None"/>, otherwise
34  /// gets <see cref="string.Empty"/>
35  /// </summary>
36  public string ErrorMessage
37  {
38  get; private set;
39  }
40 
41  /// <summary>
42  /// Gets the error code for this response.
43  /// </summary>
45  {
46  get; private set;
47  }
48 
49  /// <summary>
50  /// Gets true if this response represents a successful request, false otherwise
51  /// If this is an unprocessed response, IsSuccess will return false.
52  /// </summary>
53  public bool IsSuccess
54  {
55  get { return IsProcessed && !IsError; }
56  }
57 
58  /// <summary>
59  /// Gets true if this response represents an error, false otherwise
60  /// </summary>
61  public bool IsError
62  {
63  get { return IsProcessed && ErrorCode != OrderResponseErrorCode.None; }
64  }
65 
66  /// <summary>
67  /// Gets true if this response has been processed, false otherwise
68  /// </summary>
69  public bool IsProcessed
70  {
71  get { return this != Unprocessed; }
72  }
73 
74  /// <summary>
75  /// Initializes a new instance of the <see cref="OrderResponse"/> class
76  /// </summary>
77  /// <param name="orderId">The order id</param>
78  /// <param name="errorCode">The error code of the response, specify <see cref="OrderResponseErrorCode.None"/> for no error</param>
79  /// <param name="errorMessage">The error message, applies only if the <paramref name="errorCode"/> does not equal <see cref="OrderResponseErrorCode.None"/></param>
80  private OrderResponse(int orderId, OrderResponseErrorCode errorCode, string errorMessage)
81  {
82  OrderId = orderId;
83  ErrorCode = errorCode;
84  if (errorCode != OrderResponseErrorCode.None)
85  {
86  ErrorMessage = errorMessage ?? Messages.OrderResponse.DefaultErrorMessage;
87  }
88  }
89 
90  /// <summary>
91  /// Returns a string that represents the current object.
92  /// </summary>
93  /// <returns>
94  /// A string that represents the current object.
95  /// </returns>
96  /// <filterpriority>2</filterpriority>
97  public override string ToString()
98  {
99  return Messages.OrderResponse.ToString(this);
100  }
101 
102  #region Statics - implicit(int), Unprocessed constant, response factory methods
103 
104  /// <summary>
105  /// Gets an <see cref="OrderResponse"/> for a request that has not yet been processed
106  /// </summary>
107  public static readonly OrderResponse Unprocessed = new OrderResponse(int.MinValue, OrderResponseErrorCode.None,
108  Messages.OrderResponse.UnprocessedOrderResponseErrorMessage);
109 
110  /// <summary>
111  /// Helper method to create a successful response from a request
112  /// </summary>
113  public static OrderResponse Success(OrderRequest request)
114  {
115  return new OrderResponse(request.OrderId, OrderResponseErrorCode.None, null);
116  }
117 
118  /// <summary>
119  /// Helper method to create an error response from a request
120  /// </summary>
121  public static OrderResponse Error(OrderRequest request, OrderResponseErrorCode errorCode, string errorMessage)
122  {
123  return new OrderResponse(request.OrderId, errorCode, errorMessage);
124  }
125 
126  /// <summary>
127  /// Helper method to create an error response due to an invalid order status
128  /// </summary>
129  public static OrderResponse InvalidStatus(OrderRequest request, Order order)
130  {
131  return Error(request, OrderResponseErrorCode.InvalidOrderStatus, Messages.OrderResponse.InvalidStatus(request, order));
132  }
133 
134  /// <summary>
135  /// Helper method to create an error response due to the "New" order status
136  /// </summary>
137  public static OrderResponse InvalidNewStatus(OrderRequest request, Order order)
138  {
139  return Error(request, OrderResponseErrorCode.InvalidNewOrderStatus, Messages.OrderResponse.InvalidNewStatus(request, order));
140  }
141 
142  /// <summary>
143  /// Helper method to create an error response due to a bad order id
144  /// </summary>
146  {
147  return Error(request, OrderResponseErrorCode.UnableToFindOrder, Messages.OrderResponse.UnableToFindOrder(request));
148  }
149 
150  /// <summary>
151  /// Helper method to create an error response due to a zero order quantity
152  /// </summary>
153  public static OrderResponse ZeroQuantity(OrderRequest request)
154  {
155  return Error(request, OrderResponseErrorCode.OrderQuantityZero, Messages.OrderResponse.ZeroQuantity(request));
156  }
157 
158  /// <summary>
159  /// Helper method to create an error response due to a missing security
160  /// </summary>
162  {
163  return Error(request, OrderResponseErrorCode.MissingSecurity, Messages.OrderResponse.MissingSecurity(request));
164  }
165 
166  /// <summary>
167  /// Helper method to create an error response due to algorithm still in warmup mode
168  /// </summary>
169  public static OrderResponse WarmingUp(OrderRequest request)
170  {
171  return Error(request, OrderResponseErrorCode.AlgorithmWarmingUp, Messages.OrderResponse.WarmingUp(request));
172  }
173 
174  #endregion
175  }
176 }