Lean  $LEAN_TAG$
OrderExtensions.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.Collections.Generic;
17 
18 namespace QuantConnect.Orders
19 {
20  /// <summary>
21  /// Provides extension methods for the <see cref="Order"/> class and for the <see cref="OrderStatus"/> enumeration
22  /// </summary>
23  public static class OrderExtensions
24  {
25  /// <summary>
26  /// Determines if the specified status is in a closed state.
27  /// </summary>
28  /// <param name="status">The status to check</param>
29  /// <returns>True if the status is <see cref="OrderStatus.Filled"/>, <see cref="OrderStatus.Canceled"/>, or <see cref="OrderStatus.Invalid"/></returns>
30  public static bool IsClosed(this OrderStatus status)
31  {
32  return status == OrderStatus.Filled
33  || status == OrderStatus.Canceled
34  || status == OrderStatus.Invalid;
35  }
36 
37  /// <summary>
38  /// Determines if the specified status is in an open state.
39  /// </summary>
40  /// <param name="status">The status to check</param>
41  /// <returns>True if the status is not <see cref="OrderStatus.Filled"/>, <see cref="OrderStatus.Canceled"/>, or <see cref="OrderStatus.Invalid"/></returns>
42  public static bool IsOpen(this OrderStatus status)
43  {
44  return !status.IsClosed();
45  }
46 
47  /// <summary>
48  /// Determines if the specified status is a fill, that is, <see cref="OrderStatus.Filled"/>
49  /// order <see cref="OrderStatus.PartiallyFilled"/>
50  /// </summary>
51  /// <param name="status">The status to check</param>
52  /// <returns>True if the status is <see cref="OrderStatus.Filled"/> or <see cref="OrderStatus.PartiallyFilled"/>, false otherwise</returns>
53  public static bool IsFill(this OrderStatus status)
54  {
55  return status == OrderStatus.Filled || status == OrderStatus.PartiallyFilled;
56  }
57 
58  /// <summary>
59  /// Determines whether or not the specified order is a limit order
60  /// </summary>
61  /// <param name="orderType">The order to check</param>
62  /// <returns>True if the order is a limit order, false otherwise</returns>
63  public static bool IsLimitOrder(this OrderType orderType)
64  {
65  return orderType == OrderType.Limit
66  || orderType == OrderType.StopLimit
67  || orderType == OrderType.LimitIfTouched;
68  }
69 
70  /// <summary>
71  /// Determines whether or not the specified order is a stop order
72  /// </summary>
73  /// <param name="orderType">The order to check</param>
74  /// <returns>True if the order is a stop order, false otherwise</returns>
75  public static bool IsStopOrder(this OrderType orderType)
76  {
77  return orderType == OrderType.StopMarket || orderType == OrderType.StopLimit || orderType == OrderType.TrailingStop;
78  }
79  }
80 }