Lean  $LEAN_TAG$
GroupOrderManager.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 Newtonsoft.Json;
18 using System.Collections.Generic;
19 
20 namespace QuantConnect.Orders
21 {
22  /// <summary>
23  /// Manager of a group of orders
24  /// </summary>
25  public class GroupOrderManager
26  {
27  /// <summary>
28  /// The unique order group Id
29  /// </summary>
30  [JsonProperty(PropertyName = "id")]
31  public long Id { get; }
32 
33  /// <summary>
34  /// The group order quantity
35  /// </summary>
36  [JsonProperty(PropertyName = "quantity")]
37  public decimal Quantity { get; internal set; }
38 
39  /// <summary>
40  /// The total order count associated with this order group
41  /// </summary>
42  [JsonProperty(PropertyName = "count")]
43  public int Count { get; }
44 
45  /// <summary>
46  /// The limit price associated with this order group if any
47  /// </summary>
48  [JsonProperty(PropertyName = "limitPrice")]
49  public decimal LimitPrice { get; set; }
50 
51  /// <summary>
52  /// The order Ids in this group
53  /// </summary>
54  /// <remarks>In live trading we process orders in a dedicated thread so we need to be thread safe</remarks>
55  [JsonProperty(PropertyName = "orderIds")]
56  public HashSet<int> OrderIds { get; }
57 
58  /// <summary>
59  /// Order Direction Property based off Quantity.
60  /// </summary>
61  [JsonProperty(PropertyName = "direction")]
63  {
64  get
65  {
66  if (Quantity > 0)
67  {
68  return OrderDirection.Buy;
69  }
70  if (Quantity < 0)
71  {
72  return OrderDirection.Sell;
73  }
74  return OrderDirection.Hold;
75  }
76  }
77 
78  /// <summary>
79  /// Get the absolute quantity for this combo order
80  /// </summary>
81  [JsonIgnore]
82  public decimal AbsoluteQuantity => Math.Abs(Quantity);
83 
84  /// <summary>
85  /// Creates a new empty instance
86  /// </summary>
88  {
89  }
90 
91  /// <summary>
92  /// Creates a new instance
93  /// </summary>
94  /// <param name="id">This order group unique Id</param>
95  /// <param name="legCount">The order leg count</param>
96  /// <param name="quantity">The group order quantity</param>
97  /// <param name="limitPrice">The limit price associated with this order group if any</param>
98  public GroupOrderManager(int id, int legCount, decimal quantity, decimal limitPrice = 0)
99  {
100  Id = id;
101  Count = legCount;
102  Quantity = quantity;
103  LimitPrice = limitPrice;
104  OrderIds = new (capacity: legCount);
105  }
106  }
107 }