Lean  $LEAN_TAG$
EqualWeightingPortfolioConstructionModel.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 gives equal weighting to all
27  /// securities. The target percent holdings of each security is 1/N where N is the number of securities. For
28  /// insights of direction <see cref="InsightDirection.Up"/>, long targets are returned and for insights of direction
29  /// <see cref="InsightDirection.Down"/>, short targets are returned.
30  /// </summary>
32  {
33  private readonly PortfolioBias _portfolioBias;
34 
35  /// <summary>
36  /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/>
37  /// </summary>
38  /// <param name="rebalancingDateRules">The date rules used to define the next expected rebalance time
39  /// in UTC</param>
40  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
42  PortfolioBias portfolioBias = PortfolioBias.LongShort)
43  : this(rebalancingDateRules.ToFunc(), portfolioBias)
44  {
45  }
46 
47  /// <summary>
48  /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/>
49  /// </summary>
50  /// <param name="rebalancingFunc">For a given algorithm UTC DateTime returns the next expected rebalance time
51  /// or null if unknown, in which case the function will be called again in the next loop. Returning current time
52  /// will trigger rebalance. If null will be ignored</param>
53  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
54  public EqualWeightingPortfolioConstructionModel(Func<DateTime, DateTime?> rebalancingFunc,
55  PortfolioBias portfolioBias = PortfolioBias.LongShort)
56  : base(rebalancingFunc)
57  {
58  _portfolioBias = portfolioBias;
59  }
60 
61  /// <summary>
62  /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/>
63  /// </summary>
64  /// <param name="rebalancingFunc">For a given algorithm UTC DateTime returns the next expected rebalance UTC time.
65  /// Returning current time will trigger rebalance. If null will be ignored</param>
66  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
67  public EqualWeightingPortfolioConstructionModel(Func<DateTime, DateTime> rebalancingFunc,
68  PortfolioBias portfolioBias = PortfolioBias.LongShort)
69  : this(rebalancingFunc != null ? (Func<DateTime, DateTime?>)(timeUtc => rebalancingFunc(timeUtc)) : null, portfolioBias)
70  {
71  }
72 
73  /// <summary>
74  /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/>
75  /// </summary>
76  /// <param name="rebalance">Rebalancing func or if a date rule, timedelta will be converted into func.
77  /// For a given algorithm UTC DateTime the func returns the next expected rebalance time
78  /// or null if unknown, in which case the function will be called again in the next loop. Returning current time
79  /// will trigger rebalance. If null will be ignored</param>
80  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
81  /// <remarks>This is required since python net can not convert python methods into func nor resolve the correct
82  /// constructor for the date rules parameter.
83  /// For performance we prefer python algorithms using the C# implementation</remarks>
84  public EqualWeightingPortfolioConstructionModel(PyObject rebalance,
85  PortfolioBias portfolioBias = PortfolioBias.LongShort)
86  : this((Func<DateTime, DateTime?>)null, portfolioBias)
87  {
88  SetRebalancingFunc(rebalance);
89  }
90 
91  /// <summary>
92  /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/>
93  /// </summary>
94  /// <param name="timeSpan">Rebalancing frequency</param>
95  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
96  public EqualWeightingPortfolioConstructionModel(TimeSpan timeSpan,
97  PortfolioBias portfolioBias = PortfolioBias.LongShort)
98  : this(dt => dt.Add(timeSpan), portfolioBias)
99  {
100  }
101 
102  /// <summary>
103  /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/>
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  : this(resolution.ToTimeSpan(), portfolioBias)
110  {
111  }
112 
113  /// <summary>
114  /// Will determine the target percent for each insight
115  /// </summary>
116  /// <param name="activeInsights">The active insights to generate a target for</param>
117  /// <returns>A target percent for each insight</returns>
118  protected override Dictionary<Insight, double> DetermineTargetPercent(List<Insight> activeInsights)
119  {
120  var result = new Dictionary<Insight, double>(activeInsights.Count);
121 
122  // give equal weighting to each security
123  var count = activeInsights.Count(x => x.Direction != InsightDirection.Flat && RespectPortfolioBias(x));
124  var percent = count == 0 ? 0 : 1m / count;
125  foreach (var insight in activeInsights)
126  {
127  result[insight] =
128  (double)((int)(RespectPortfolioBias(insight) ? insight.Direction : InsightDirection.Flat)
129  * percent);
130  }
131  return result;
132  }
133 
134  /// <summary>
135  /// Method that will determine if a given insight respects the portfolio bias
136  /// </summary>
137  /// <param name="insight">The insight to create a target for</param>
138  /// <returns>True if the insight respects the portfolio bias</returns>
139  protected bool RespectPortfolioBias(Insight insight)
140  {
141  return _portfolioBias == PortfolioBias.LongShort || (int)insight.Direction == (int)_portfolioBias;
142  }
143  }
144 }