Lean  $LEAN_TAG$
InsightWeightingPortfolioConstructionModel.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 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 using Python.Runtime;
22 
24 {
25  /// <summary>
26  /// Provides an implementation of <see cref="IPortfolioConstructionModel"/> that generates percent targets based on the
27  /// <see cref="Insight.Weight"/>. The target percent holdings of each Symbol is given by the <see cref="Insight.Weight"/>
28  /// from the last active <see cref="Insight"/> for that symbol.
29  /// For insights of direction <see cref="InsightDirection.Up"/>, long targets are returned and for insights of direction
30  /// <see cref="InsightDirection.Down"/>, short targets are returned.
31  /// If the sum of all the last active <see cref="Insight"/> per symbol is bigger than 1, it will factor down each target
32  /// percent holdings proportionally so the sum is 1.
33  /// It will ignore <see cref="Insight"/> that have no <see cref="Insight.Weight"/> value.
34  /// </summary>
36  {
37  /// <summary>
38  /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/>
39  /// </summary>
40  /// <param name="rebalancingDateRules">The date rules used to define the next expected rebalance time
41  /// in UTC</param>
42  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
44  PortfolioBias portfolioBias = PortfolioBias.LongShort)
45  : base(rebalancingDateRules, portfolioBias)
46  {
47  }
48 
49  /// <summary>
50  /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/>
51  /// </summary>
52  /// <param name="rebalance">Rebalancing func or if a date rule, timedelta will be converted into func.
53  /// For a given algorithm UTC DateTime the func returns the next expected rebalance time
54  /// or null if unknown, in which case the function will be called again in the next loop. Returning current time
55  /// will trigger rebalance. If null will be ignored</param>
56  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
57  /// <remarks>This is required since python net can not convert python methods into func nor resolve the correct
58  /// constructor for the date rules parameter.
59  /// For performance we prefer python algorithms using the C# implementation</remarks>
60  public InsightWeightingPortfolioConstructionModel(PyObject rebalance,
61  PortfolioBias portfolioBias = PortfolioBias.LongShort)
62  : base(rebalance, portfolioBias)
63  {
64  }
65 
66  /// <summary>
67  /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/>
68  /// </summary>
69  /// <param name="rebalancingFunc">For a given algorithm UTC DateTime returns the next expected rebalance time
70  /// or null if unknown, in which case the function will be called again in the next loop. Returning current time
71  /// will trigger rebalance.</param>
72  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
73  public InsightWeightingPortfolioConstructionModel(Func<DateTime, DateTime?> rebalancingFunc,
74  PortfolioBias portfolioBias = PortfolioBias.LongShort)
75  : base(rebalancingFunc, portfolioBias)
76  {
77  }
78 
79  /// <summary>
80  /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/>
81  /// </summary>
82  /// <param name="rebalancingFunc">For a given algorithm UTC DateTime returns the next expected rebalance UTC time.
83  /// Returning current time will trigger rebalance. If null will be ignored</param>
84  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
85  public InsightWeightingPortfolioConstructionModel(Func<DateTime, DateTime> rebalancingFunc,
86  PortfolioBias portfolioBias = PortfolioBias.LongShort)
87  : base(rebalancingFunc, portfolioBias)
88  {
89  }
90 
91  /// <summary>
92  /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/>
93  /// </summary>
94  /// <param name="timeSpan">Rebalancing frequency</param>
95  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
97  PortfolioBias portfolioBias = PortfolioBias.LongShort)
98  : base(timeSpan, portfolioBias)
99  {
100  }
101 
102  /// <summary>
103  /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/>
104  /// </summary>
105  /// <param name="resolution">Rebalancing frequency</param>
106  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
108  PortfolioBias portfolioBias = PortfolioBias.LongShort)
109  : base(resolution, portfolioBias)
110  {
111  }
112 
113  /// <summary>
114  /// Method that will determine if the portfolio construction model should create a
115  /// target for this insight
116  /// </summary>
117  /// <param name="insight">The insight to create a target for</param>
118  /// <returns>True if the portfolio should create a target for the insight</returns>
119  protected override bool ShouldCreateTargetForInsight(Insight insight)
120  {
121  return insight.Weight.HasValue;
122  }
123 
124  /// <summary>
125  /// Will determine the target percent for each insight
126  /// </summary>
127  /// <param name="activeInsights">The active insights to generate a target for</param>
128  /// <returns>A target percent for each insight</returns>
129  protected override Dictionary<Insight, double> DetermineTargetPercent(List<Insight> activeInsights)
130  {
131  var result = new Dictionary<Insight, double>();
132  // We will adjust weights proportionally in case the sum is > 1 so it sums to 1.
133  var weightSums = activeInsights.Where(RespectPortfolioBias).Sum(insight => GetValue(insight));
134  var weightFactor = 1.0;
135  if (weightSums > 1)
136  {
137  weightFactor = 1 / weightSums;
138  }
139  foreach (var insight in activeInsights)
140  {
141  result[insight] = (int)(RespectPortfolioBias(insight) ? insight.Direction : InsightDirection.Flat)
142  * GetValue(insight)
143  * weightFactor;
144  }
145  return result;
146  }
147 
148  /// <summary>
149  /// Method that will determine which member will be used to compute the weights and gets its value
150  /// </summary>
151  /// <param name="insight">The insight to create a target for</param>
152  /// <returns>The value of the selected insight member</returns>
153  protected virtual double GetValue(Insight insight) => insight.Weight != null ? Math.Abs((double)insight.Weight) : 0;
154  }
155 }