/* * 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 { /// /// Provides an implementation of that uses an absurdly low margin /// requirement to ensure all orders have sufficient margin provided the portfolio is not underwater. /// public class ConstantBuyingPowerModel : BuyingPowerModel { private readonly decimal _marginRequiredPerUnitInAccountCurrency; /// /// Initializes a new instance of the class /// /// The constant amount of margin required per single unit /// of an asset. Each unit is defined as a quantity of 1 and NOT based on the lot size. public ConstantBuyingPowerModel(decimal marginRequiredPerUnitInAccountCurrency) { _marginRequiredPerUnitInAccountCurrency = marginRequiredPerUnitInAccountCurrency; } /// /// Sets the leverage for the applicable securities, i.e, equities /// /// /// This is added to maintain backwards compatibility with the old margin/leverage system /// /// /// The new leverage public override void SetLeverage(Security security, decimal leverage) { // ignored -- reasoning is user has an algorithm that has margin issues and so they quickly swap // this impl in, but their code calls set leverage, they would need to comment that out and such // said another way -- user made the decision to ignore margin/leverage by selecting this model } /// /// The margin that must be held in order to increase the position by the provided quantity /// /// An object containing the security and quantity of shares /// The initial margin required for the provided security and quantity public override InitialMargin GetInitialMarginRequirement(InitialMarginParameters parameters) { return parameters.Quantity * _marginRequiredPerUnitInAccountCurrency; } /// /// Gets the margin currently allocated to the specified holding /// /// An object containing the security /// The maintenance margin required for the provided holdings quantity/cost/value public override MaintenanceMargin GetMaintenanceMargin(MaintenanceMarginParameters parameters) { return parameters.AbsoluteQuantity * _marginRequiredPerUnitInAccountCurrency; } } }