Lean  $LEAN_TAG$
ConfidenceWeightedPortfolioConstructionModel.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 Python.Runtime;
20 
22 {
23  /// <summary>
24  /// Provides an implementation of <see cref="IPortfolioConstructionModel"/> that generates percent targets based on the
25  /// <see cref="Insight.Confidence"/>. The target percent holdings of each Symbol is given by the <see cref="Insight.Confidence"/>
26  /// from the last active <see cref="Insight"/> for that symbol.
27  /// For insights of direction <see cref="InsightDirection.Up"/>, long targets are returned and for insights of direction
28  /// <see cref="InsightDirection.Down"/>, short targets are returned.
29  /// If the sum of all the last active <see cref="Insight"/> per symbol is bigger than 1, it will factor down each target
30  /// percent holdings proportionally so the sum is 1.
31  /// It will ignore <see cref="Insight"/> that have no <see cref="Insight.Confidence"/> value.
32  /// </summary>
34  {
35  /// <summary>
36  /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/>
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  : base(rebalancingDateRules, portfolioBias)
44  {
45  }
46 
47  /// <summary>
48  /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/>
49  /// </summary>
50  /// <param name="rebalance">Rebalancing func or if a date rule, timedelta will be converted into func.
51  /// For a given algorithm UTC DateTime the func returns the next expected rebalance time
52  /// or null if unknown, in which case the function will be called again in the next loop. Returning current time
53  /// will trigger rebalance. If null will be ignored</param>
54  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
55  /// <remarks>This is required since python net can not convert python methods into func nor resolve the correct
56  /// constructor for the date rules parameter.
57  /// For performance we prefer python algorithms using the C# implementation</remarks>
59  PortfolioBias portfolioBias = PortfolioBias.LongShort)
60  : base(rebalance, portfolioBias)
61  {
62  }
63 
64  /// <summary>
65  /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/>
66  /// </summary>
67  /// <param name="rebalancingFunc">For a given algorithm UTC DateTime returns the next expected rebalance time
68  /// or null if unknown, in which case the function will be called again in the next loop. Returning current time
69  /// will trigger rebalance. If null will be ignored</param>
70  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
71  public ConfidenceWeightedPortfolioConstructionModel(Func<DateTime, DateTime?> rebalancingFunc,
72  PortfolioBias portfolioBias = PortfolioBias.LongShort)
73  : base(rebalancingFunc, portfolioBias)
74  {
75  }
76 
77  /// <summary>
78  /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/>
79  /// </summary>
80  /// <param name="rebalancingFunc">For a given algorithm UTC DateTime returns the next expected rebalance UTC time.
81  /// Returning current time will trigger rebalance. If null will be ignored</param>
82  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
83  public ConfidenceWeightedPortfolioConstructionModel(Func<DateTime, DateTime> rebalancingFunc,
84  PortfolioBias portfolioBias = PortfolioBias.LongShort)
85  : base(rebalancingFunc, portfolioBias)
86  {
87  }
88 
89  /// <summary>
90  /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/>
91  /// </summary>
92  /// <param name="timeSpan">Rebalancing frequency</param>
93  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
95  PortfolioBias portfolioBias = PortfolioBias.LongShort)
96  : base(timeSpan, portfolioBias)
97  {
98  }
99 
100  /// <summary>
101  /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/>
102  /// </summary>
103  /// <param name="resolution">Rebalancing frequency</param>
104  /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param>
106  PortfolioBias portfolioBias = PortfolioBias.LongShort)
107  : base(resolution, portfolioBias)
108  {
109  }
110 
111  /// <summary>
112  /// Method that will determine if the portfolio construction model should create a
113  /// target for this insight
114  /// </summary>
115  /// <param name="insight">The insight to create a target for</param>
116  /// <returns>True if the portfolio should create a target for the insight</returns>
117  protected override bool ShouldCreateTargetForInsight(Insight insight)
118  {
119  return insight.Confidence.HasValue;
120  }
121 
122  /// <summary>
123  /// Method that will determine which member will be used to compute the weights and gets its value
124  /// </summary>
125  /// <param name="insight">The insight to create a target for</param>
126  /// <returns>The value of the selected insight member</returns>
127  protected override double GetValue(Insight insight) => insight.Confidence ?? 0;
128  }
129 }