Lean  $LEAN_TAG$
GroupOrderExtensions.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 System.Linq;
18 using QuantConnect.Logging;
20 using System.Collections.Generic;
21 
22 namespace QuantConnect.Orders
23 {
24  /// <summary>
25  /// Group (combo) orders extension methods for easiest combo order manipulation
26  /// </summary>
27  public static class GroupOrderExtensions
28  {
29  /// <summary>
30  /// Gets the grouped orders (legs) of a group order
31  /// </summary>
32  /// <param name="order">Target order, which can be any of the legs of the combo</param>
33  /// <param name="orderProvider">Order provider to use to access the existing orders</param>
34  /// <param name="orders">List of orders in the combo</param>
35  /// <returns>False if any of the orders in the combo is not yet found in the order provider. True otherwise</returns>
36  /// <remarks>If the target order is not a combo order, the resulting list will contain that single order alone</remarks>
37  public static bool TryGetGroupOrders(this Order order, Func<int, Order> orderProvider, out List<Order> orders)
38  {
39  orders = new List<Order> { order };
40  if (order.GroupOrderManager != null)
41  {
42  lock (order.GroupOrderManager.OrderIds)
43  {
44  foreach (var otherOrdersId in order.GroupOrderManager.OrderIds.Where(id => id != order.Id))
45  {
46  var otherOrder = orderProvider(otherOrdersId);
47  if (otherOrder != null)
48  {
49  orders.Add(otherOrder);
50  }
51  else
52  {
53  // this will happen while all the orders haven't arrived yet, we will retry
54  return false;
55  }
56  }
57  }
58 
59  if (order.GroupOrderManager.Count != orders.Count)
60  {
62  {
63  Log.Debug($"GroupOrderExtensions.TryGetGroupOrders(): missing orders of group {order.GroupOrderManager.Id}." +
64  $" We have {orders.Count}/{order.GroupOrderManager.Count} orders will skip");
65  }
66  return false;
67  }
68  }
69 
70  orders.Sort((x, y) => x.Id.CompareTo(y.Id));
71 
72  return true;
73  }
74 
75  /// <summary>
76  /// Gets the securities corresponding to each order in the group
77  /// </summary>
78  /// <param name="orders">List of orders to map</param>
79  /// <param name="securityProvider">The security provider to use</param>
80  /// <param name="securities">The resulting map of order to security</param>
81  /// <returns>True if the mapping is successful, false otherwise.</returns>
82  public static bool TryGetGroupOrdersSecurities(this List<Order> orders, ISecurityProvider securityProvider, out Dictionary<Order, Security> securities)
83  {
84  securities = new(orders.Count);
85  for (var i = 0; i < orders.Count; i++)
86  {
87  var order = orders[i];
88  var security = securityProvider.GetSecurity(order.Symbol);
89 
90  if(security == null)
91  {
92  return false;
93  }
94  securities[order] = security;
95  }
96  return true;
97  }
98 
99  public static string GetErrorMessage(this Dictionary<Order, Security> securities, HasSufficientBuyingPowerForOrderResult hasSufficientBuyingPowerResult)
100  {
101  return Messages.GroupOrderExtensions.InsufficientBuyingPowerForOrders(securities, hasSufficientBuyingPowerResult);
102  }
103 
104  /// <summary>
105  /// Gets the combo order leg group quantity, that is, the total number of shares to be bought/sold from this leg,
106  /// from its ratio and the group order quantity
107  /// </summary>
108  /// <param name="legRatio">The leg ratio</param>
109  /// <param name="groupOrderManager">The group order manager</param>
110  /// <returns>The total number of shares to be bought/sold from this leg</returns>
111  public static decimal GetOrderLegGroupQuantity(this decimal legRatio, GroupOrderManager groupOrderManager)
112  {
113  return groupOrderManager != null ? legRatio * groupOrderManager.Quantity : legRatio;
114  }
115 
116  /// <summary>
117  /// Gets the combo order leg ratio from its group quantity and the group order quantity
118  /// </summary>
119  /// <param name="legGroupQuantity">
120  /// The total number of shares to be bought/sold from this leg, that is, the result of the let ratio times the group quantity
121  /// </param>
122  /// <param name="groupOrderManager">The group order manager</param>
123  /// <returns>The ratio of this combo order leg</returns>
124  public static decimal GetOrderLegRatio(this decimal legGroupQuantity, GroupOrderManager groupOrderManager)
125  {
126  return groupOrderManager != null ? legGroupQuantity / groupOrderManager.Quantity : legGroupQuantity;
127  }
128  }
129 }