Lean  $LEAN_TAG$
PatternDayTradingMarginModel.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  /// Represents a simple margining model where margin/leverage depends on market state (open or close).
20  /// During regular market hours, leverage is 4x, otherwise 2x
21  /// </summary>
23  {
24  private readonly decimal _closedMarginCorrectionFactor;
25 
26  /// <summary>
27  /// Initializes a new instance of the <see cref="PatternDayTradingMarginModel" />
28  /// </summary>
30  : this(2.0m, 4.0m)
31  {
32  }
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="PatternDayTradingMarginModel" />
36  /// </summary>
37  /// <param name="closedMarketLeverage">Leverage used outside regular market hours</param>
38  /// <param name="openMarketLeverage">Leverage used during regular market hours</param>
39  public PatternDayTradingMarginModel(decimal closedMarketLeverage, decimal openMarketLeverage)
40  : base(openMarketLeverage)
41  {
42  _closedMarginCorrectionFactor = openMarketLeverage/closedMarketLeverage;
43  }
44 
45  /// <summary>
46  /// Sets the leverage for the applicable securities, i.e, equities
47  /// </summary>
48  /// <remarks>
49  /// Do nothing, we use a constant leverage for this model
50  /// </remarks>
51  /// <param name="security">The security to set leverage to</param>
52  /// <param name="leverage">The new leverage</param>
53  public override void SetLeverage(Security security, decimal leverage)
54  {
55  }
56 
57  /// <summary>
58  /// Gets the current leverage of the security
59  /// </summary>
60  /// <param name="security">The security to get leverage for</param>
61  /// <returns>The current leverage in the security</returns>
62  public override decimal GetLeverage(Security security)
63  {
64  return base.GetLeverage(security) * (1 / GetMarginCorrectionFactor(security));
65  }
66 
67  /// <summary>
68  /// The percentage of an order's absolute cost that must be held in free cash in order to place the order
69  /// </summary>
71  {
72  return new InitialMargin(base.GetInitialMarginRequirement(parameters).Value
73  * GetMarginCorrectionFactor(parameters.Security)
74  );
75  }
76 
77  /// <summary>
78  /// The percentage of the holding's absolute cost that must be held in free cash in order to avoid a margin call
79  /// </summary>
81  {
82  return base.GetMaintenanceMargin(parameters) * GetMarginCorrectionFactor(parameters.Security);
83  }
84 
85  /// <summary>
86  /// Get margin correction factor if not in regular market hours
87  /// </summary>
88  /// <param name="security">The security to apply conditional leverage to</param>
89  /// <returns>The margin correction factor</returns>
90  private decimal GetMarginCorrectionFactor(Security security)
91  {
92  // when the market is open the base type returns the correct values
93  // when the market is closed or when its closing soon, we need to multiply by a correction factor
94  return security.Exchange.ExchangeOpen && !security.Exchange.ClosingSoon ? 1m :_closedMarginCorrectionFactor;
95  }
96  }
97 }