Lean  $LEAN_TAG$
HasSufficientPositionGroupBuyingPowerForOrderParameters.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 using System.Linq;
18 using QuantConnect.Orders;
19 
21 {
22  /// <summary>
23  /// Defines the parameters for <see cref="IPositionGroupBuyingPowerModel.HasSufficientBuyingPowerForOrder"/>
24  /// </summary>
26  {
27  /// <summary>
28  /// The orders associated with this request
29  /// </summary>
30  public List<Order> Orders { get; }
31 
32  /// <summary>
33  /// Gets the position group representing the holdings changes contemplated by the order
34  /// </summary>
35  public IPositionGroup PositionGroup { get; }
36 
37  /// <summary>
38  /// Gets the algorithm's portfolio manager
39  /// </summary>
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="HasSufficientPositionGroupBuyingPowerForOrderParameters"/> class
44  /// </summary>
45  /// <param name="portfolio">The algorithm's portfolio manager</param>
46  /// <param name="positionGroup">The position group</param>
47  /// <param name="orders">The orders</param>
49  SecurityPortfolioManager portfolio,
50  IPositionGroup positionGroup,
51  List<Order> orders
52  )
53  {
54  Orders = orders;
55  Portfolio = portfolio;
56  PositionGroup = positionGroup;
57  }
58 
59  /// <summary>
60  /// This may be called for non-combo type orders where the position group is guaranteed to have exactly one position
61  /// </summary>
62  public static implicit operator HasSufficientBuyingPowerForOrderParameters(
64  )
65  {
66  var position = parameters.PositionGroup.Single();
67  var security = parameters.Portfolio.Securities[position.Symbol];
68  return new HasSufficientBuyingPowerForOrderParameters(parameters.Portfolio, security, parameters.Orders.Single());
69  }
70 
71  /// <summary>
72  /// Creates a new result indicating that there is sufficient buying power for the contemplated order
73  /// </summary>
75  {
77  }
78 
79  /// <summary>
80  /// Creates a new result indicating that there is insufficient buying power for the contemplated order
81  /// </summary>
83  {
84  return new HasSufficientBuyingPowerForOrderResult(false, reason);
85  }
86 
87  /// <summary>
88  /// Creates a new result indicating that there was an error
89  /// </summary>
91  {
92  return new HasSufficientBuyingPowerForOrderResult(false, reason);
93  }
94  }
95 }