/* * 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 Python.Runtime; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Scheduling; namespace QuantConnect.Algorithm.Framework.Portfolio { /// /// Provides an implementation of that generates percent targets based on the /// . The target percent holdings of each Symbol is given by the /// from the last active for that symbol. /// For insights of direction , long targets are returned and for insights of direction /// , short targets are returned. /// If the sum of all the last active per symbol is bigger than 1, it will factor down each target /// percent holdings proportionally so the sum is 1. /// It will ignore that have no value. /// public class ConfidenceWeightedPortfolioConstructionModel : InsightWeightingPortfolioConstructionModel { /// /// Initialize a new instance of /// /// The date rules used to define the next expected rebalance time /// in UTC /// Specifies the bias of the portfolio (Short, Long/Short, Long) public ConfidenceWeightedPortfolioConstructionModel(IDateRule rebalancingDateRules, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingDateRules, portfolioBias) { } /// /// Initialize a new instance of /// /// Rebalancing func or if a date rule, timedelta will be converted into func. /// For a given algorithm UTC DateTime the func returns the next expected rebalance time /// or null if unknown, in which case the function will be called again in the next loop. Returning current time /// will trigger rebalance. If null will be ignored /// Specifies the bias of the portfolio (Short, Long/Short, Long) /// This is required since python net can not convert python methods into func nor resolve the correct /// constructor for the date rules parameter. /// For performance we prefer python algorithms using the C# implementation public ConfidenceWeightedPortfolioConstructionModel(PyObject rebalance, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalance, portfolioBias) { } /// /// Initialize a new instance of /// /// For a given algorithm UTC DateTime returns the next expected rebalance time /// or null if unknown, in which case the function will be called again in the next loop. Returning current time /// will trigger rebalance. If null will be ignored /// Specifies the bias of the portfolio (Short, Long/Short, Long) public ConfidenceWeightedPortfolioConstructionModel(Func rebalancingFunc, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingFunc, portfolioBias) { } /// /// Initialize a new instance of /// /// For a given algorithm UTC DateTime returns the next expected rebalance UTC time. /// Returning current time will trigger rebalance. If null will be ignored /// Specifies the bias of the portfolio (Short, Long/Short, Long) public ConfidenceWeightedPortfolioConstructionModel(Func rebalancingFunc, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingFunc, portfolioBias) { } /// /// Initialize a new instance of /// /// Rebalancing frequency /// Specifies the bias of the portfolio (Short, Long/Short, Long) public ConfidenceWeightedPortfolioConstructionModel(TimeSpan timeSpan, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(timeSpan, portfolioBias) { } /// /// Initialize a new instance of /// /// Rebalancing frequency /// Specifies the bias of the portfolio (Short, Long/Short, Long) public ConfidenceWeightedPortfolioConstructionModel(Resolution resolution = Resolution.Daily, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(resolution, portfolioBias) { } /// /// Method that will determine if the portfolio construction model should create a /// target for this insight /// /// The insight to create a target for /// True if the portfolio should create a target for the insight protected override bool ShouldCreateTargetForInsight(Insight insight) { return insight.Confidence.HasValue; } /// /// Method that will determine which member will be used to compute the weights and gets its value /// /// The insight to create a target for /// The value of the selected insight member protected override double GetValue(Insight insight) => insight.Confidence ?? 0; } }