/* * 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 System.Collections.Generic; using QuantConnect.Algorithm.Framework.Portfolio; namespace QuantConnect.Algorithm.Framework.Risk { /// /// Provides an implementation of that limits the drawdown of the portfolio /// to the specified percentage. Once this is triggered the algorithm will need to be manually restarted. /// public class MaximumDrawdownPercentPortfolio : RiskManagementModel { private readonly decimal _maximumDrawdownPercent; private decimal _portfolioHigh; private bool _initialised = false; private bool _isTrailing; /// /// Initializes a new instance of the class /// /// The maximum percentage drawdown allowed for algorithm portfolio /// compared with starting value, defaults to 5% drawdown /// If "false", the drawdown will be relative to the starting value of the portfolio. /// If "true", the drawdown will be relative the last maximum portfolio value public MaximumDrawdownPercentPortfolio(decimal maximumDrawdownPercent = 0.05m, bool isTrailing = false) { _maximumDrawdownPercent = -Math.Abs(maximumDrawdownPercent); _isTrailing = isTrailing; } /// /// Manages the algorithm's risk at each time step /// /// The algorithm instance /// The current portfolio targets to be assessed for risk public override IEnumerable ManageRisk(QCAlgorithm algorithm, IPortfolioTarget[] targets) { var currentValue = algorithm.Portfolio.TotalPortfolioValue; if (!_initialised) { _portfolioHigh = currentValue; // Set initial portfolio value _initialised = true; } // Update trailing high value if in trailing mode if (_isTrailing && (_portfolioHigh < currentValue)) { _portfolioHigh = currentValue; yield break; // return if new high reached } var pnl = GetTotalDrawdownPercent(currentValue); if (pnl < _maximumDrawdownPercent && targets.Length != 0) { // reset the trailing high value for restart investing on next rebalcing period _initialised = false; foreach (var target in targets) { var symbol = target.Symbol; // Cancel insights algorithm.Insights.Cancel(new[] { symbol }); // liquidate yield return new PortfolioTarget(symbol, 0); } } } private decimal GetTotalDrawdownPercent(decimal currentValue) { return (currentValue / _portfolioHigh) - 1.0m; } } }