Lean  $LEAN_TAG$
ConstantBuyingPowerModel.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 
17 {
18  /// <summary>
19  /// Provides an implementation of <see cref="IBuyingPowerModel"/> that uses an absurdly low margin
20  /// requirement to ensure all orders have sufficient margin provided the portfolio is not underwater.
21  /// </summary>
23  {
24  private readonly decimal _marginRequiredPerUnitInAccountCurrency;
25 
26  /// <summary>
27  /// Initializes a new instance of the <see cref="ConstantBuyingPowerModel"/> class
28  /// </summary>
29  /// <param name="marginRequiredPerUnitInAccountCurrency">The constant amount of margin required per single unit
30  /// of an asset. Each unit is defined as a quantity of 1 and NOT based on the lot size.</param>
31  public ConstantBuyingPowerModel(decimal marginRequiredPerUnitInAccountCurrency)
32  {
33  _marginRequiredPerUnitInAccountCurrency = marginRequiredPerUnitInAccountCurrency;
34  }
35 
36 
37  /// <summary>
38  /// Sets the leverage for the applicable securities, i.e, equities
39  /// </summary>
40  /// <remarks>
41  /// This is added to maintain backwards compatibility with the old margin/leverage system
42  /// </remarks>
43  /// <param name="security"></param>
44  /// <param name="leverage">The new leverage</param>
45  public override void SetLeverage(Security security, decimal leverage)
46  {
47  // ignored -- reasoning is user has an algorithm that has margin issues and so they quickly swap
48  // this impl in, but their code calls set leverage, they would need to comment that out and such
49  // said another way -- user made the decision to ignore margin/leverage by selecting this model
50  }
51 
52  /// <summary>
53  /// The margin that must be held in order to increase the position by the provided quantity
54  /// </summary>
55  /// <param name="parameters">An object containing the security and quantity of shares</param>
56  /// <returns>The initial margin required for the provided security and quantity</returns>
58  {
59  return parameters.Quantity * _marginRequiredPerUnitInAccountCurrency;
60  }
61 
62  /// <summary>
63  /// Gets the margin currently allocated to the specified holding
64  /// </summary>
65  /// <param name="parameters">An object containing the security</param>
66  /// <returns>The maintenance margin required for the provided holdings quantity/cost/value</returns>
68  {
69  return parameters.AbsoluteQuantity * _marginRequiredPerUnitInAccountCurrency;
70  }
71  }
72 }