/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace QuantConnect.Securities { /// /// Represents a simple margining model where margin/leverage depends on market state (open or close). /// During regular market hours, leverage is 4x, otherwise 2x /// public class PatternDayTradingMarginModel : SecurityMarginModel { private readonly decimal _closedMarginCorrectionFactor; /// /// Initializes a new instance of the /// public PatternDayTradingMarginModel() : this(2.0m, 4.0m) { } /// /// Initializes a new instance of the /// /// Leverage used outside regular market hours /// Leverage used during regular market hours public PatternDayTradingMarginModel(decimal closedMarketLeverage, decimal openMarketLeverage) : base(openMarketLeverage) { _closedMarginCorrectionFactor = openMarketLeverage/closedMarketLeverage; } /// /// Sets the leverage for the applicable securities, i.e, equities /// /// /// Do nothing, we use a constant leverage for this model /// /// The security to set leverage to /// The new leverage public override void SetLeverage(Security security, decimal leverage) { } /// /// Gets the current leverage of the security /// /// The security to get leverage for /// The current leverage in the security public override decimal GetLeverage(Security security) { return base.GetLeverage(security) * (1 / GetMarginCorrectionFactor(security)); } /// /// The percentage of an order's absolute cost that must be held in free cash in order to place the order /// public override InitialMargin GetInitialMarginRequirement(InitialMarginParameters parameters) { return new InitialMargin(base.GetInitialMarginRequirement(parameters).Value * GetMarginCorrectionFactor(parameters.Security) ); } /// /// The percentage of the holding's absolute cost that must be held in free cash in order to avoid a margin call /// public override MaintenanceMargin GetMaintenanceMargin(MaintenanceMarginParameters parameters) { return base.GetMaintenanceMargin(parameters) * GetMarginCorrectionFactor(parameters.Security); } /// /// Get margin correction factor if not in regular market hours /// /// The security to apply conditional leverage to /// The margin correction factor private decimal GetMarginCorrectionFactor(Security security) { // when the market is open the base type returns the correct values // when the market is closed or when its closing soon, we need to multiply by a correction factor return security.Exchange.ExchangeOpen && !security.Exchange.ClosingSoon ? 1m :_closedMarginCorrectionFactor; } } }