Lean  $LEAN_TAG$
BuyingPowerModelPythonWrapper.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 Python.Runtime;
18 
19 namespace QuantConnect.Python
20 {
21  /// <summary>
22  /// Wraps a <see cref="PyObject"/> object that represents a security's model of buying power
23  /// </summary>
25  {
26  /// <summary>
27  /// Constructor for initializing the <see cref="BuyingPowerModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
28  /// </summary>
29  /// <param name="model">Represents a security's model of buying power</param>
30  public BuyingPowerModelPythonWrapper(PyObject model)
31  : base(model)
32  {
33  }
34 
35  /// <summary>
36  /// Gets the buying power available for a trade
37  /// </summary>
38  /// <param name="parameters">A parameters object containing the algorithm's potrfolio, security, and order direction</param>
39  /// <returns>The buying power available for the trade</returns>
41  {
42  return InvokeMethod<BuyingPower>(nameof(GetBuyingPower), parameters);
43  }
44 
45  /// <summary>
46  /// Gets the current leverage of the security
47  /// </summary>
48  /// <param name="security">The security to get leverage for</param>
49  /// <returns>The current leverage in the security</returns>
50  public decimal GetLeverage(Security security)
51  {
52  return InvokeMethod<decimal>(nameof(GetLeverage), security);
53  }
54 
55  /// <summary>
56  /// Get the maximum market order quantity to obtain a position with a given buying power percentage.
57  /// Will not take into account free buying power.
58  /// </summary>
59  /// <param name="parameters">An object containing the portfolio, the security and the target signed buying power percentage</param>
60  /// <returns>Returns the maximum allowed market order quantity and if zero, also the reason</returns>
62  {
63  return InvokeMethod<GetMaximumOrderQuantityResult>(nameof(GetMaximumOrderQuantityForTargetBuyingPower), parameters);
64  }
65 
66  /// <summary>
67  /// Get the maximum market order quantity to obtain a delta in the buying power used by a security.
68  /// The deltas sign defines the position side to apply it to, positive long, negative short.
69  /// </summary>
70  /// <param name="parameters">An object containing the portfolio, the security and the delta buying power</param>
71  /// <returns>Returns the maximum allowed market order quantity and if zero, also the reason</returns>
74  {
75  return InvokeMethod<GetMaximumOrderQuantityResult>(nameof(GetMaximumOrderQuantityForDeltaBuyingPower), parameters);
76  }
77 
78  /// <summary>
79  /// Gets the amount of buying power reserved to maintain the specified position
80  /// </summary>
81  /// <param name="parameters">A parameters object containing the security</param>
82  /// <returns>The reserved buying power in account currency</returns>
84  {
85  return InvokeMethod<ReservedBuyingPowerForPosition>(nameof(GetReservedBuyingPowerForPosition), parameters);
86  }
87 
88  /// <summary>
89  /// Check if there is sufficient buying power to execute this order.
90  /// </summary>
91  /// <param name="parameters">An object containing the portfolio, the security and the order</param>
92  /// <returns>Returns buying power information for an order</returns>
94  {
95  return InvokeMethod<HasSufficientBuyingPowerForOrderResult>(nameof(HasSufficientBuyingPowerForOrder), parameters);
96  }
97 
98  /// <summary>
99  /// Sets the leverage for the applicable securities, i.e, equities
100  /// </summary>
101  /// <remarks>
102  /// This is added to maintain backwards compatibility with the old margin/leverage system
103  /// </remarks>
104  /// <param name="security">The security to set leverage for</param>
105  /// <param name="leverage">The new leverage</param>
106  public void SetLeverage(Security security, decimal leverage)
107  {
108  InvokeMethod(nameof(SetLeverage), security, leverage);
109  }
110 
111  /// <summary>
112  /// Gets the margin currently allocated to the specified holding
113  /// </summary>
114  /// <param name="parameters">An object containing the security</param>
115  /// <returns>The maintenance margin required for the provided holdings quantity/cost/value</returns>
117  {
118  return InvokeMethod<MaintenanceMargin>(nameof(GetMaintenanceMargin), parameters);
119  }
120 
121  /// <summary>
122  /// The margin that must be held in order to increase the position by the provided quantity
123  /// </summary>
124  /// <param name="parameters">An object containing the security and quantity</param>
125  /// <returns>The initial margin required for the provided security and quantity</returns>
127  {
128  return InvokeMethod<InitialMargin>(nameof(GetInitialMarginRequirement), parameters);
129  }
130 
131  /// <summary>
132  /// Gets the total margin required to execute the specified order in units of the account currency including fees
133  /// </summary>
134  /// <param name="parameters">An object containing the portfolio, the security and the order</param>
135  /// <returns>The total margin in terms of the currency quoted in the order</returns>
137  {
138  return InvokeMethod<InitialMargin>(nameof(GetInitialMarginRequiredForOrder), parameters);
139  }
140  }
141 }