Lean  $LEAN_TAG$
HasSufficientBuyingPowerForOrderParameters.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 QuantConnect.Orders;
18 using static QuantConnect.StringExtensions;
19 
21 {
22  /// <summary>
23  /// Defines the parameters for <see cref="IBuyingPowerModel.HasSufficientBuyingPowerForOrder"/>
24  /// </summary>
26  {
27  /// <summary>
28  /// Gets the algorithm's portfolio
29  /// </summary>
31 
32  /// <summary>
33  /// Gets the security
34  /// </summary>
35  public Security Security { get; }
36 
37  /// <summary>
38  /// Gets the order
39  /// </summary>
40  public Order Order { get; }
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="HasSufficientBuyingPowerForOrderParameters"/> class
44  /// </summary>
45  /// <param name="portfolio">The algorithm's portfolio</param>
46  /// <param name="security">The security</param>
47  /// <param name="order">The order</param>
49  {
50  Portfolio = portfolio;
51  Security = security;
52  Order = order;
53  }
54 
55  /// <summary>
56  /// Creates a new <see cref="HasSufficientBuyingPowerForOrderParameters"/> targeting the security's underlying.
57  /// If the security does not implement <see cref="IDerivativeSecurity"/> then an <see cref="InvalidCastException"/>
58  /// will be thrown. If the order's symbol does not match the underlying then an <see cref="ArgumentException"/> will
59  /// be thrown.
60  /// </summary>
61  /// <param name="order">The new order targeting the underlying</param>
62  /// <returns>New parameters instance suitable for invoking the sufficient capital method for the underlying security</returns>
64  {
65  var derivative = (IDerivativeSecurity) Security;
66  return new HasSufficientBuyingPowerForOrderParameters(Portfolio, derivative.Underlying, order);
67  }
68 
69  /// <summary>
70  /// Creates a new result indicating that there is sufficient buying power for the contemplated order
71  /// </summary>
73  {
75  }
76 
77  /// <summary>
78  /// Creates a new result indicating that there is insufficient buying power for the contemplated order
79  /// </summary>
81  {
82  return new HasSufficientBuyingPowerForOrderResult(false, reason);
83  }
84 
85  /// <summary>
86  /// Creates a new result indicating that there is insufficient buying power for the contemplated order
87  /// </summary>
88  public HasSufficientBuyingPowerForOrderResult Insufficient(FormattableString reason)
89  {
90  return new HasSufficientBuyingPowerForOrderResult(false, Invariant(reason));
91  }
92  }
93 }