20 using System.Collections.Generic;
31 public partial class QCAlgorithm
34 private readonly HashSet<Symbol> _universeSelectionUniverses =
new ();
35 private bool _isEmitWarmupInsightWarningSent;
36 private bool _isEmitDelistedInsightWarningSent;
42 [DocumentationAttribute(Logging)]
91 _universeSelectionUniverses.Add(universe.Configuration.Symbol);
96 InsightsGenerated += (algorithm, data) =>
Log($
"{Time}: {string.Join(" |
", data.Insights.OrderBy(i => i.Symbol.ToString()))}");
111 foreach (var ukvp
in UniverseManager.Where(kvp => kvp.Value.DisposeRequested))
113 var universeSymbol = ukvp.Key;
122 _universeSelectionUniverses.Remove(universeSymbol);
125 var toRemove =
new HashSet<Symbol>(_universeSelectionUniverses);
129 _universeSelectionUniverses.Add(universe.Configuration.Symbol);
132 toRemove.Remove(universe.Configuration.Symbol);
136 foreach (var universeSymbol
in toRemove)
153 var insightsEnumerable =
Alpha.
Update(
this, slice);
155 var insights = insightsEnumerable == Enumerable.Empty<
Insight>()
156 ?
new Insight[] { } : insightsEnumerable.ToArray();
159 if (insights.Length != 0)
161 insights = InitializeInsights(insights);
162 OnInsightsGenerated(insights);
165 ProcessInsights(insights);
176 private void ProcessInsights(
Insight[] insights)
185 foreach (var target
in targets)
188 security.Holdings.Target = target;
194 if (targets.Length > 0)
196 Log($
"{Time}: PORTFOLIO: {string.Join(" |
", targets.Select(t => t.ToString()).OrderBy(t => t))}");
202 var riskTargetOverrides = riskTargetOverridesEnumerable == Enumerable.Empty<
IPortfolioTarget>()
206 foreach (var target
in riskTargetOverrides)
209 security.Holdings.Target = target;
215 if (riskTargetOverrides.Length > 0)
217 Log($
"{Time}: RISK: {string.Join(" |
", riskTargetOverrides.Select(t => t.ToString()).OrderBy(t => t))}");
223 if (riskTargetOverrides.Length != 0
224 || targets.Length != 0)
227 riskAdjustedTargets = riskTargetOverrides.Concat(targets).DistinctBy(pt => pt.Symbol).ToArray();
237 if (riskTargetOverrides.Length > 0)
239 Log($
"{Time}: RISK ADJUSTED TARGETS: {string.Join(" |
", riskAdjustedTargets.Select(t => t.ToString()).OrderBy(t => t))}");
250 [DocumentationAttribute(AlgorithmFramework)]
251 [DocumentationAttribute(Universes)]
256 Debug($
"{Time}: {changes}");
287 if (compositeUniverseSelection !=
null)
322 if (compositeAlphaModel !=
null)
324 compositeAlphaModel.
AddAlpha(alpha);
381 if (compositeRiskModel !=
null)
407 if (!_isEmitWarmupInsightWarningSent)
409 Error(
"Warning: insights emitted during algorithm warmup are ignored.");
410 _isEmitWarmupInsightWarningSent =
true;
415 insights = InitializeInsights(insights);
416 OnInsightsGenerated(insights);
417 ProcessInsights(insights);
439 List<Insight> validInsights =
null;
440 for (var i = 0; i < insights.Length; i++)
443 if (security.IsDelisted)
445 if (!_isEmitDelistedInsightWarningSent)
447 Error($
"QCAlgorithm.EmitInsights(): Warning: cannot emit insights for delisted securities, these will be discarded");
448 _isEmitDelistedInsightWarningSent =
true;
452 if (validInsights ==
null)
454 validInsights =
new List<Insight>() {};
455 for (var j = 0; j < i; j++)
457 validInsights.Add(insights[j]);
464 insights[i] = InitializeInsightFields(insights[i], security);
467 if (validInsights !=
null)
469 validInsights.Add(insights[i]);
474 return validInsights ==
null ? insights : validInsights.ToArray();
487 switch (insight.
Type)