/* * 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. */ using System; using QuantConnect.Interfaces; using QuantConnect.Securities; namespace QuantConnect.Algorithm.Framework.Portfolio { /// /// Provides an implementation of that specifies a /// specified quantity of a security to be held by the algorithm /// public class PortfolioTarget : IPortfolioTarget { /// /// Gets the symbol of this target /// public Symbol Symbol { get; } /// /// Gets the target quantity for the symbol /// public decimal Quantity { get; } /// /// Initializes a new instance of the class /// /// The symbol this target is for /// The target quantity public PortfolioTarget(Symbol symbol, decimal quantity) { Symbol = symbol; Quantity = quantity; } /// /// Creates a new target for the specified percent /// /// The algorithm instance, used for getting total portfolio value and current security price /// The symbol the target is for /// The requested target percent of total portfolio value /// A portfolio target for the specified symbol/percent public static IPortfolioTarget Percent(IAlgorithm algorithm, Symbol symbol, double percent) { return Percent(algorithm, symbol, percent.SafeDecimalCast()); } /// /// Creates a new target for the specified percent /// /// The algorithm instance, used for getting total portfolio value and current security price /// The symbol the target is for /// The requested target percent of total portfolio value /// True, result quantity will be the Delta required to reach target percent. /// False, the result quantity will be the Total quantity to reach the target percent, including current holdings /// A portfolio target for the specified symbol/percent public static IPortfolioTarget Percent(IAlgorithm algorithm, Symbol symbol, decimal percent, bool returnDeltaQuantity = false) { var absolutePercentage = Math.Abs(percent); if (absolutePercentage > algorithm.Settings.MaxAbsolutePortfolioTargetPercentage || absolutePercentage != 0 && absolutePercentage < algorithm.Settings.MinAbsolutePortfolioTargetPercentage) { algorithm.Error($"The portfolio target percent: {percent}, does not comply with the current " + $"'Algorithm.Settings' 'MaxAbsolutePortfolioTargetPercentage': {algorithm.Settings.MaxAbsolutePortfolioTargetPercentage}" + $" or 'MinAbsolutePortfolioTargetPercentage': {algorithm.Settings.MinAbsolutePortfolioTargetPercentage}. Skipping"); return null; } var security = algorithm.Securities[symbol]; if (security.Price == 0) { algorithm.Error($"The order quantity for {symbol.Value} cannot be calculated: the price of the security is zero."); return null; } // Factoring in FreePortfolioValuePercentage. var adjustedPercent = percent * (1 - algorithm.Settings.FreePortfolioValuePercentage); var result = security.BuyingPowerModel.GetMaximumOrderQuantityForTargetValue( new GetMaximumOrderQuantityForTargetValueParameters(algorithm.Portfolio, security, adjustedPercent) ); if (result.IsError) { algorithm.Error($"Unable to compute order quantity of {symbol}. Reason: {result.Reason} Returning null."); return null; } // be sure to back out existing holdings quantity since the buying power model yields // the required delta quantity to reach a final target portfolio value for a symbol var quantity = result.Quantity + (returnDeltaQuantity ? 0 : security.Holdings.Quantity); return new PortfolioTarget(symbol, quantity); } /// Returns a string that represents the current object. /// A string that represents the current object. /// 2 public override string ToString() { return $"{Symbol}: {Quantity.Normalize()}"; } } }