Lean  $LEAN_TAG$
MaintenanceMarginParameters.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 
19 {
20  /// <summary>
21  /// Parameters for <see cref="IBuyingPowerModel.GetMaintenanceMargin"/>
22  /// </summary>
24  {
25  /// <summary>
26  /// Gets the security
27  /// </summary>
28  public Security Security { get; }
29 
30  /// <summary>
31  /// Gets the quantity of the security
32  /// </summary>
33  public decimal Quantity { get; }
34 
35  /// <summary>
36  /// Gets the absolute quantity of the security
37  /// </summary>
38  public decimal AbsoluteQuantity => Math.Abs(Quantity);
39 
40  /// <summary>
41  /// Gets the holdings cost of the security
42  /// </summary>
43  public decimal HoldingsCost { get; }
44 
45  /// <summary>
46  /// Gets the absolute holdings cost of the security
47  /// </summary>
48  public decimal AbsoluteHoldingsCost => Math.Abs(HoldingsCost);
49 
50  /// <summary>
51  /// Gets the holdings value of the security
52  /// </summary>
53  public decimal HoldingsValue { get; }
54 
55  /// <summary>
56  /// Gets the absolute holdings value of the security
57  /// </summary>
58  public decimal AbsoluteHoldingsValue => Math.Abs(HoldingsValue);
59 
60  /// <summary>
61  /// Initializes a new instance of the <see cref="MaintenanceMarginParameters"/> class
62  /// </summary>
63  /// <param name="security">The security</param>
64  /// <param name="quantity">The quantity</param>
65  /// <param name="holdingsCost">The holdings cost</param>
66  /// <param name="holdingsValue">The holdings value</param>
68  Security security,
69  decimal quantity,
70  decimal holdingsCost,
71  decimal holdingsValue
72  )
73  {
74  Security = security;
75  Quantity = quantity;
76  HoldingsCost = holdingsCost;
77  HoldingsValue = holdingsValue;
78  }
79 
80  /// <summary>
81  /// Creates a new instance of the <see cref="MaintenanceMarginParameters"/> class to compute the maintenance margin
82  /// required to support the algorithm's current holdings
83  /// </summary>
85  {
86  return new MaintenanceMarginParameters(security,
87  security.Holdings.Quantity,
88  security.Holdings.HoldingsCost,
89  security.Holdings.HoldingsValue
90  );
91  }
92 
93  /// <summary>
94  /// Creates a new instance of the <see cref="MaintenanceMarginParameters"/> class to compute the maintenance margin
95  /// required to support the specified quantity of holdings at current market prices
96  /// </summary>
97  public static MaintenanceMarginParameters ForQuantityAtCurrentPrice(Security security, decimal quantity)
98  {
99  var value = security.Holdings.GetQuantityValue(quantity).InAccountCurrency;
100  return new MaintenanceMarginParameters(security, quantity, value, value);
101  }
102 
103  /// <summary>
104  /// Creates a new instance of <see cref="MaintenanceMarginParameters"/> for the security's underlying
105  /// </summary>
107  {
108  var derivative = Security as IDerivativeSecurity;
109  if (derivative == null)
110  {
111  throw new InvalidOperationException(Messages.MaintenanceMarginParameters.ForUnderlyingOnlyInvokableForIDerivativeSecurity);
112  }
113 
114  return ForQuantityAtCurrentPrice(derivative.Underlying, quantity);
115  }
116  }
117 }