/// <summary> /// Create portfolio targets from the specified insights /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="insights">The insights to create portoflio targets from</param> /// <returns>An enumerable of portoflio targets to be sent to the execution model</returns> public override IEnumerable <IPortfolioTarget> CreateTargets(QCAlgorithmFramework algorithm, Insight[] insights) { var targets = new List <IPortfolioTarget>(); if (algorithm.UtcTime <= _nextExpiryTime && algorithm.UtcTime <= _rebalancingTime && insights.Length == 0 && _removedSymbols == null) { return(targets); } _insightCollection.AddRange(insights); // Create flatten target for each security that was removed from the universe if (_removedSymbols != null) { var universeDeselectionTargets = _removedSymbols.Select(symbol => new PortfolioTarget(symbol, 0)); targets.AddRange(universeDeselectionTargets); _removedSymbols = null; } // Get insight that haven't expired of each symbol that is still in the universe var activeInsights = _insightCollection.GetActiveInsights(algorithm.UtcTime); // Get the last generated active insight for each symbol var lastActiveInsights = from insight in activeInsights group insight by insight.Symbol into g select g.OrderBy(x => x.GeneratedTimeUtc).Last(); // give equal weighting to each security var count = lastActiveInsights.Count(x => x.Direction != InsightDirection.Flat); var percent = count == 0 ? 0 : 1m / count; foreach (var insight in lastActiveInsights) { targets.Add(PortfolioTarget.Percent(algorithm, insight.Symbol, (int)insight.Direction * percent)); } // Get expired insights and create flatten targets for each symbol var expiredInsights = _insightCollection.RemoveExpiredInsights(algorithm.UtcTime); var expiredTargets = from insight in expiredInsights group insight.Symbol by insight.Symbol into g where !_insightCollection.HasActiveInsights(g.Key, algorithm.UtcTime) select new PortfolioTarget(g.Key, 0); targets.AddRange(expiredTargets); _nextExpiryTime = _insightCollection.GetNextExpiryTime(); _rebalancingTime = algorithm.UtcTime.Add(_rebalancingPeriod); return(targets); }
/// <summary> /// Create portfolio targets from the specified insights /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="insights">The insights to create portfolio targets from</param> /// <returns>An enumerable of portfolio targets to be sent to the execution model</returns> public override IEnumerable <IPortfolioTarget> CreateTargets(QCAlgorithm algorithm, Insight[] insights) { // always add new insights if (insights.Length > 0) { // Validate we should create a target for this insight InsightCollection.AddRange(insights.Where(ShouldCreateTargetForInsight)); } if (!IsRebalanceDue(insights, algorithm.UtcTime)) { return(Enumerable.Empty <IPortfolioTarget>()); } var targets = new List <IPortfolioTarget>(); // Create flatten target for each security that was removed from the universe if (_removedSymbols != null) { var universeDeselectionTargets = _removedSymbols.Select(symbol => new PortfolioTarget(symbol, 0)); targets.AddRange(universeDeselectionTargets); _removedSymbols = null; } // Get insight that haven't expired of each symbol that is still in the universe var activeInsights = InsightCollection.GetActiveInsights(algorithm.UtcTime); // Get the last generated active insight for each symbol var lastActiveInsights = (from insight in activeInsights group insight by insight.Symbol into g select g.OrderBy(x => x.GeneratedTimeUtc).Last()).ToList(); var errorSymbols = new HashSet <Symbol>(); // Determine target percent for the given insights var percents = DetermineTargetPercent(lastActiveInsights); foreach (var insight in lastActiveInsights) { var target = PortfolioTarget.Percent(algorithm, insight.Symbol, percents[insight]); if (target != null) { targets.Add(target); } else { errorSymbols.Add(insight.Symbol); } } // Get expired insights and create flatten targets for each symbol var expiredInsights = InsightCollection.RemoveExpiredInsights(algorithm.UtcTime); var expiredTargets = from insight in expiredInsights group insight.Symbol by insight.Symbol into g where !InsightCollection.HasActiveInsights(g.Key, algorithm.UtcTime) && !errorSymbols.Contains(g.Key) select new PortfolioTarget(g.Key, 0); targets.AddRange(expiredTargets); return(targets); }
/// <summary> /// Create portfolio targets from the specified insights /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="insights">The insights to create portfolio targets from</param> /// <returns>An enumerable of portfolio targets to be sent to the execution model</returns> public virtual IEnumerable <IPortfolioTarget> CreateTargets(QCAlgorithm algorithm, Insight[] insights) { Algorithm = algorithm; // always add new insights if (insights.Length > 0) { // Validate we should create a target for this insight InsightCollection.AddRange(insights .Where(insight => PythonWrapper?.ShouldCreateTargetForInsight(insight) ?? ShouldCreateTargetForInsight(insight))); } if (!(PythonWrapper?.IsRebalanceDue(insights, algorithm.UtcTime) ?? IsRebalanceDue(insights, algorithm.UtcTime))) { return(Enumerable.Empty <IPortfolioTarget>()); } var targets = new List <IPortfolioTarget>(); // Create flatten target for each security that was removed from the universe if (_removedSymbols != null) { var universeDeselectionTargets = _removedSymbols.Select(symbol => new PortfolioTarget(symbol, 0)); targets.AddRange(universeDeselectionTargets); _removedSymbols = null; } var lastActiveInsights = PythonWrapper?.GetTargetInsights() ?? GetTargetInsights(); var errorSymbols = new HashSet <Symbol>(); // Determine target percent for the given insights var percents = PythonWrapper?.DetermineTargetPercent(lastActiveInsights) ?? DetermineTargetPercent(lastActiveInsights); foreach (var insight in lastActiveInsights) { double percent; if (!percents.TryGetValue(insight, out percent)) { continue; } var target = PortfolioTarget.Percent(algorithm, insight.Symbol, percent); if (target != null) { targets.Add(target); } else { errorSymbols.Add(insight.Symbol); } } // Get expired insights and create flatten targets for each symbol var expiredInsights = InsightCollection.RemoveExpiredInsights(algorithm.UtcTime); var expiredTargets = from insight in expiredInsights group insight.Symbol by insight.Symbol into g where !InsightCollection.HasActiveInsights(g.Key, algorithm.UtcTime) && !errorSymbols.Contains(g.Key) select new PortfolioTarget(g.Key, 0); targets.AddRange(expiredTargets); return(targets); }
/// <summary> /// Create portfolio targets from the specified insights /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="insights">The insights to create portfolio targets from</param> /// <returns>An enumerable of portfolio targets to be sent to the execution model</returns> public override IEnumerable <IPortfolioTarget> CreateTargets(QCAlgorithm algorithm, Insight[] insights) { var targets = new List <IPortfolioTarget>(); if (algorithm.UtcTime <= _nextExpiryTime && algorithm.UtcTime <= _rebalancingTime && insights.Length == 0 && _removedSymbols == null) { return(targets); } insights = FilterInvalidInsightMagnitude(algorithm, insights); _insightCollection.AddRange(insights); // Create flatten target for each security that was removed from the universe if (_removedSymbols != null) { var universeDeselectionTargets = _removedSymbols.Select(symbol => new PortfolioTarget(symbol, 0)); targets.AddRange(universeDeselectionTargets); _removedSymbols = null; } // Get insight that haven't expired of each symbol that is still in the universe var activeInsights = _insightCollection.GetActiveInsights(algorithm.UtcTime); // Get the last generated active insight for each symbol var lastActiveInsights = (from insight in activeInsights group insight by new { insight.Symbol, insight.SourceModel } into g select g.OrderBy(x => x.GeneratedTimeUtc).Last()) .OrderBy(x => x.Symbol).ToArray(); double[,] P; double[] Q; if (TryGetViews(lastActiveInsights, out P, out Q)) { // Updates the ReturnsSymbolData with insights foreach (var insight in lastActiveInsights) { ReturnsSymbolData symbolData; if (_symbolDataDict.TryGetValue(insight.Symbol, out symbolData)) { if (insight.Magnitude == null) { algorithm.SetRunTimeError(new ArgumentNullException("BlackLittermanOptimizationPortfolioConstructionModel does not accept \'null\' as Insight.Magnitude. Please make sure your Alpha Model is generating Insights with the Magnitude property set.")); } symbolData.Add(algorithm.Time, insight.Magnitude.Value.SafeDecimalCast()); } } // Get symbols' returns var symbols = lastActiveInsights.Select(x => x.Symbol).Distinct().ToList(); var returns = _symbolDataDict.FormReturnsMatrix(symbols); // Calculate posterior estimate of the mean and uncertainty in the mean double[,] Σ; var Π = GetEquilibriumReturns(returns, out Σ); ApplyBlackLittermanMasterFormula(ref Π, ref Σ, P, Q); // Create portfolio targets from the specified insights var W = _optimizer.Optimize(returns, Π, Σ); var sidx = 0; foreach (var symbol in symbols) { var weight = W[sidx].SafeDecimalCast(); var target = PortfolioTarget.Percent(algorithm, symbol, weight); if (target != null) { targets.Add(target); } sidx++; } } // Get expired insights and create flatten targets for each symbol var expiredInsights = _insightCollection.RemoveExpiredInsights(algorithm.UtcTime); var expiredTargets = from insight in expiredInsights group insight.Symbol by insight.Symbol into g where !_insightCollection.HasActiveInsights(g.Key, algorithm.UtcTime) select new PortfolioTarget(g.Key, 0); targets.AddRange(expiredTargets); _nextExpiryTime = _insightCollection.GetNextExpiryTime(); _rebalancingTime = algorithm.UtcTime.Add(_rebalancingPeriod); return(targets); }