Lean  $LEAN_TAG$
MaximumSectorExposureRiskManagementModel.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 */
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
22 
24 {
25  /// <summary>
26  /// Provides an implementation of <see cref="IRiskManagementModel"/> that limits
27  /// the sector exposure to the specified percentage
28  /// </summary>
30  {
31  private readonly decimal _maximumSectorExposure;
32  private readonly PortfolioTargetCollection _targetsCollection;
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="MaximumSectorExposureRiskManagementModel"/> class
36  /// </summary>
37  /// <param name="maximumSectorExposure">The maximum exposure for any sector, defaults to 20% sector exposure.</param>
39  decimal maximumSectorExposure = 0.20m
40  )
41  {
42  if (maximumSectorExposure <= 0)
43  {
44  throw new ArgumentOutOfRangeException("MaximumSectorExposureRiskManagementModel: the maximum sector exposure cannot be a non-positive value.");
45  }
46 
47  _maximumSectorExposure = maximumSectorExposure;
48  _targetsCollection = new PortfolioTargetCollection();
49  }
50 
51  /// <summary>
52  /// Manages the algorithm's risk at each time step
53  /// </summary>
54  /// <param name="algorithm">The algorithm instance</param>
55  /// <param name="targets">The current portfolio targets to be assessed for risk</param>
56  public override IEnumerable<IPortfolioTarget> ManageRisk(QCAlgorithm algorithm, IPortfolioTarget[] targets)
57  {
58  var maximumSectorExposureValue = algorithm.Portfolio.TotalPortfolioValue * _maximumSectorExposure;
59 
60  _targetsCollection.AddRange(targets);
61 
62  // Group the securities by their sector
63  var groupBySector = algorithm.UniverseManager.ActiveSecurities
64  .Where(x => x.Value.Fundamentals != null && x.Value.Fundamentals.HasFundamentalData)
65  .GroupBy(x => x.Value.Fundamentals.CompanyReference.IndustryTemplateCode);
66 
67  foreach (var securities in groupBySector)
68  {
69  // Compute the sector absolute holdings value
70  // If the construction model has created a target, we consider that
71  // value to calculate the security absolute holding value
72  var sectorAbsoluteHoldingsValue = 0m;
73 
74  foreach (var security in securities)
75  {
76  var absoluteHoldingsValue = security.Value.Holdings.AbsoluteHoldingsValue;
77 
78  IPortfolioTarget target;
79  if (_targetsCollection.TryGetValue(security.Value.Symbol, out target))
80  {
81  absoluteHoldingsValue = security.Value.Price * Math.Abs(target.Quantity) *
82  security.Value.SymbolProperties.ContractMultiplier *
83  security.Value.QuoteCurrency.ConversionRate;
84  }
85 
86  sectorAbsoluteHoldingsValue += absoluteHoldingsValue;
87  }
88 
89  // If the ratio between the sector absolute holdings value and the maximum sector exposure value
90  // exceeds the unity, it means we need to reduce each security of that sector by that ratio
91  // Otherwise, it means that the sector exposure is below the maximum and there is nothing to do.
92  var ratio = sectorAbsoluteHoldingsValue / maximumSectorExposureValue;
93  if (ratio > 1)
94  {
95  foreach (var security in securities)
96  {
97  var quantity = security.Value.Holdings.Quantity;
98  var symbol = security.Value.Symbol;
99 
100  IPortfolioTarget target;
101  if (_targetsCollection.TryGetValue(symbol, out target))
102  {
103  quantity = target.Quantity;
104  }
105 
106  if (quantity != 0)
107  {
108  yield return new PortfolioTarget(symbol, quantity / ratio);
109  }
110  }
111  }
112  }
113  }
114 
115  /// <summary>
116  /// Event fired each time the we add/remove securities from the data feed
117  /// </summary>
118  /// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
119  /// <param name="changes">The security additions and removals from the algorithm</param>
120  public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
121  {
122  var anyFundamentalData = algorithm.ActiveSecurities
123  .Any(kvp => kvp.Value.Fundamentals != null && kvp.Value.Fundamentals.HasFundamentalData);
124 
125  if (!anyFundamentalData)
126  {
127  throw new Exception("MaximumSectorExposureRiskManagementModel.OnSecuritiesChanged: Please select a portfolio selection model that selects securities with fundamental data.");
128  }
129  }
130  }
131 }