Lean  $LEAN_TAG$
QCAlgorithm.Framework.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.Linq;
17 using QuantConnect.Data;
18 using QuantConnect.Util;
20 using System.Collections.Generic;
28 
29 namespace QuantConnect.Algorithm
30 {
31  public partial class QCAlgorithm
32  {
33  // this is so that later during 'UniverseSelection.CreateUniverses' we wont remove the user universes from the UniverseManager
34  private readonly HashSet<Symbol> _universeSelectionUniverses = new ();
35  private bool _isEmitWarmupInsightWarningSent;
36  private bool _isEmitDelistedInsightWarningSent;
37 
38  /// <summary>
39  /// Enables additional logging of framework models including:
40  /// All insights, portfolio targets, order events, and any risk management altered targets
41  /// </summary>
42  [DocumentationAttribute(Logging)]
43  public bool DebugMode { get; set; }
44 
45  /// <summary>
46  /// Gets or sets the universe selection model.
47  /// </summary>
48  [DocumentationAttribute(AlgorithmFramework)]
50 
51  /// <summary>
52  /// Gets or sets the alpha model
53  /// </summary>
54  [DocumentationAttribute(AlgorithmFramework)]
55  public IAlphaModel Alpha { get; set; }
56 
57  /// <summary>
58  /// Gets the insight manager
59  /// </summary>
60  [DocumentationAttribute(AlgorithmFramework)]
61  public InsightManager Insights { get; private set; }
62 
63  /// <summary>
64  /// Gets or sets the portfolio construction model
65  /// </summary>
66  [DocumentationAttribute(AlgorithmFramework)]
68 
69  /// <summary>
70  /// Gets or sets the execution model
71  /// </summary>
72  [DocumentationAttribute(AlgorithmFramework)]
73  public IExecutionModel Execution { get; set; }
74 
75  /// <summary>
76  /// Gets or sets the risk management model
77  /// </summary>
78  [DocumentationAttribute(AlgorithmFramework)]
79  public IRiskManagementModel RiskManagement { get; set; }
80 
81  /// <summary>
82  /// Called by setup handlers after Initialize and allows the algorithm a chance to organize
83  /// the data gather in the Initialize method
84  /// </summary>
85  [DocumentationAttribute(AlgorithmFramework)]
87  {
88  foreach (var universe in UniverseSelection.CreateUniverses(this))
89  {
90  AddUniverse(universe);
91  _universeSelectionUniverses.Add(universe.Configuration.Symbol);
92  }
93 
94  if (DebugMode)
95  {
96  InsightsGenerated += (algorithm, data) => Log($"{Time}: {string.Join(" | ", data.Insights.OrderBy(i => i.Symbol.ToString()))}");
97  }
98  }
99 
100  /// <summary>
101  /// Used to send data updates to algorithm framework models
102  /// </summary>
103  /// <param name="slice">The current data slice</param>
104  [DocumentationAttribute(AlgorithmFramework)]
105  [DocumentationAttribute(HandlingData)]
106  public void OnFrameworkData(Slice slice)
107  {
109  {
110  // remove deselected universes by symbol before we create new universes
111  foreach (var ukvp in UniverseManager.Where(kvp => kvp.Value.DisposeRequested))
112  {
113  var universeSymbol = ukvp.Key;
114  // have to remove in the next loop after the universe is marked as disposed, when 'Dispose()' is called it will trigger universe selection
115  // and deselect all symbols, sending the removed security changes, which are picked up by the AlgorithmManager and tags securities
116  // as non tradable as long as they are not active in any universe (uses UniverseManager.ActiveSecurities)
117  // but they will remain tradable if a position is still being hold since they won't be remove from the UniverseManager
118  // but this last part will not happen if we remove the universe from the UniverseManager right away, since it won't be part of 'UniverseManager'.
119  // And we have to remove the universe even if it's present at 'universes' because that one is another New universe that should get added!
120  // 'UniverseManager' will skip duplicate entries getting added.
121  UniverseManager.Remove(universeSymbol);
122  _universeSelectionUniverses.Remove(universeSymbol);
123  }
124 
125  var toRemove = new HashSet<Symbol>(_universeSelectionUniverses);
126  foreach (var universe in UniverseSelection.CreateUniverses(this))
127  {
128  // add newly selected universes
129  _universeSelectionUniverses.Add(universe.Configuration.Symbol);
130  AddUniverse(universe);
131 
132  toRemove.Remove(universe.Configuration.Symbol);
133  }
134 
135  // remove deselected universes by symbol but prevent removal of qc algorithm created user defined universes
136  foreach (var universeSymbol in toRemove)
137  {
138  // mark this universe as disposed to remove all child subscriptions
139  UniverseManager[universeSymbol].Dispose();
140  }
141  }
142 
143  // update scores
145 
146  // we only want to run universe selection if there's no data available in the slice
147  if (!slice.HasData)
148  {
149  return;
150  }
151 
152  // insight timestamping handled via InsightsGenerated event handler
153  var insightsEnumerable = Alpha.Update(this, slice);
154  // for performance only call 'ToArray' if not empty enumerable (which is static)
155  var insights = insightsEnumerable == Enumerable.Empty<Insight>()
156  ? new Insight[] { } : insightsEnumerable.ToArray();
157 
158  // only fire insights generated event if we actually have insights
159  if (insights.Length != 0)
160  {
161  insights = InitializeInsights(insights);
162  OnInsightsGenerated(insights);
163  }
164 
165  ProcessInsights(insights);
166  }
167 
168  /// <summary>
169  /// They different framework models will process the new provided insight.
170  /// The <see cref="IPortfolioConstructionModel"/> will create targets,
171  /// the <see cref="IRiskManagementModel"/> will adjust the targets
172  /// and the <see cref="IExecutionModel"/> will execute the <see cref="IPortfolioTarget"/>
173  /// </summary>
174  /// <param name="insights">The insight to process</param>
175  [DocumentationAttribute(AlgorithmFramework)]
176  private void ProcessInsights(Insight[] insights)
177  {
178  // construct portfolio targets from insights
179  var targetsEnumerable = PortfolioConstruction.CreateTargets(this, insights);
180  // for performance only call 'ToArray' if not empty enumerable (which is static)
181  var targets = targetsEnumerable == Enumerable.Empty<IPortfolioTarget>()
182  ? new IPortfolioTarget[] {} : targetsEnumerable.ToArray();
183 
184  // set security targets w/ those generated via portfolio construction module
185  foreach (var target in targets)
186  {
187  var security = Securities[target.Symbol];
188  security.Holdings.Target = target;
189  }
190 
191  if (DebugMode)
192  {
193  // debug printing of generated targets
194  if (targets.Length > 0)
195  {
196  Log($"{Time}: PORTFOLIO: {string.Join(" | ", targets.Select(t => t.ToString()).OrderBy(t => t))}");
197  }
198  }
199 
200  var riskTargetOverridesEnumerable = RiskManagement.ManageRisk(this, targets);
201  // for performance only call 'ToArray' if not empty enumerable (which is static)
202  var riskTargetOverrides = riskTargetOverridesEnumerable == Enumerable.Empty<IPortfolioTarget>()
203  ? new IPortfolioTarget[] { } : riskTargetOverridesEnumerable.ToArray();
204 
205  // override security targets w/ those generated via risk management module
206  foreach (var target in riskTargetOverrides)
207  {
208  var security = Securities[target.Symbol];
209  security.Holdings.Target = target;
210  }
211 
212  if (DebugMode)
213  {
214  // debug printing of generated risk target overrides
215  if (riskTargetOverrides.Length > 0)
216  {
217  Log($"{Time}: RISK: {string.Join(" | ", riskTargetOverrides.Select(t => t.ToString()).OrderBy(t => t))}");
218  }
219  }
220 
221  IPortfolioTarget[] riskAdjustedTargets;
222  // for performance we check the length before
223  if (riskTargetOverrides.Length != 0
224  || targets.Length != 0)
225  {
226  // execute on the targets, overriding targets for symbols w/ risk targets
227  riskAdjustedTargets = riskTargetOverrides.Concat(targets).DistinctBy(pt => pt.Symbol).ToArray();
228  }
229  else
230  {
231  riskAdjustedTargets = new IPortfolioTarget[] { };
232  }
233 
234  if (DebugMode)
235  {
236  // only log adjusted targets if we've performed an adjustment
237  if (riskTargetOverrides.Length > 0)
238  {
239  Log($"{Time}: RISK ADJUSTED TARGETS: {string.Join(" | ", riskAdjustedTargets.Select(t => t.ToString()).OrderBy(t => t))}");
240  }
241  }
242 
243  Execution.Execute(this, riskAdjustedTargets);
244  }
245 
246  /// <summary>
247  /// Used to send security changes to algorithm framework models
248  /// </summary>
249  /// <param name="changes">Security additions/removals for this time step</param>
250  [DocumentationAttribute(AlgorithmFramework)]
251  [DocumentationAttribute(Universes)]
253  {
254  if (DebugMode)
255  {
256  Debug($"{Time}: {changes}");
257  }
258 
259  Alpha.OnSecuritiesChanged(this, changes);
261  Execution.OnSecuritiesChanged(this, changes);
262  RiskManagement.OnSecuritiesChanged(this, changes);
263  }
264 
265  /// <summary>
266  /// Sets the universe selection model
267  /// </summary>
268  /// <param name="universeSelection">Model defining universes for the algorithm</param>
269  [DocumentationAttribute(AlgorithmFramework)]
270  [DocumentationAttribute(Universes)]
271  public void SetUniverseSelection(IUniverseSelectionModel universeSelection)
272  {
273  UniverseSelection = universeSelection;
274  }
275 
276  /// <summary>
277  /// Adds a new universe selection model
278  /// </summary>
279  /// <param name="universeSelection">Model defining universes for the algorithm to add</param>
280  [DocumentationAttribute(AlgorithmFramework)]
281  [DocumentationAttribute(Universes)]
282  public void AddUniverseSelection(IUniverseSelectionModel universeSelection)
283  {
284  if (UniverseSelection.GetType() != typeof(NullUniverseSelectionModel))
285  {
286  var compositeUniverseSelection = UniverseSelection as CompositeUniverseSelectionModel;
287  if (compositeUniverseSelection != null)
288  {
289  compositeUniverseSelection.AddUniverseSelection(universeSelection);
290  }
291  else
292  {
294  }
295  }
296  else
297  {
298  UniverseSelection = universeSelection;
299  }
300  }
301 
302  /// <summary>
303  /// Sets the alpha model
304  /// </summary>
305  /// <param name="alpha">Model that generates alpha</param>
306  [DocumentationAttribute(AlgorithmFramework)]
307  public void SetAlpha(IAlphaModel alpha)
308  {
309  Alpha = alpha;
310  }
311 
312  /// <summary>
313  /// Adds a new alpha model
314  /// </summary>
315  /// <param name="alpha">Model that generates alpha to add</param>
316  [DocumentationAttribute(AlgorithmFramework)]
317  public void AddAlpha(IAlphaModel alpha)
318  {
319  if (Alpha.GetType() != typeof(NullAlphaModel))
320  {
321  var compositeAlphaModel = Alpha as CompositeAlphaModel;
322  if (compositeAlphaModel != null)
323  {
324  compositeAlphaModel.AddAlpha(alpha);
325  }
326  else
327  {
328  Alpha = new CompositeAlphaModel(Alpha, alpha);
329  }
330  }
331  else
332  {
333  Alpha = alpha;
334  }
335  }
336 
337  /// <summary>
338  /// Sets the portfolio construction model
339  /// </summary>
340  /// <param name="portfolioConstruction">Model defining how to build a portfolio from insights</param>
341  [DocumentationAttribute(AlgorithmFramework)]
342  [DocumentationAttribute(TradingAndOrders)]
343  public void SetPortfolioConstruction(IPortfolioConstructionModel portfolioConstruction)
344  {
345  PortfolioConstruction = portfolioConstruction;
346  }
347 
348  /// <summary>
349  /// Sets the execution model
350  /// </summary>
351  /// <param name="execution">Model defining how to execute trades to reach a portfolio target</param>
352  [DocumentationAttribute(AlgorithmFramework)]
353  [DocumentationAttribute(TradingAndOrders)]
354  public void SetExecution(IExecutionModel execution)
355  {
356  Execution = execution;
357  }
358 
359  /// <summary>
360  /// Sets the risk management model
361  /// </summary>
362  /// <param name="riskManagement">Model defining how risk is managed</param>
363  [DocumentationAttribute(AlgorithmFramework)]
364  [DocumentationAttribute(TradingAndOrders)]
365  public void SetRiskManagement(IRiskManagementModel riskManagement)
366  {
367  RiskManagement = riskManagement;
368  }
369 
370  /// <summary>
371  /// Adds a new risk management model
372  /// </summary>
373  /// <param name="riskManagement">Model defining how risk is managed to add</param>
374  [DocumentationAttribute(AlgorithmFramework)]
375  [DocumentationAttribute(TradingAndOrders)]
376  public void AddRiskManagement(IRiskManagementModel riskManagement)
377  {
378  if (RiskManagement.GetType() != typeof(NullRiskManagementModel))
379  {
380  var compositeRiskModel = RiskManagement as CompositeRiskManagementModel;
381  if (compositeRiskModel != null)
382  {
383  compositeRiskModel.AddRiskManagement(riskManagement);
384  }
385  else
386  {
388  }
389  }
390  else
391  {
392  RiskManagement = riskManagement;
393  }
394  }
395 
396  /// <summary>
397  /// Manually emit insights from an algorithm.
398  /// This is typically invoked before calls to submit orders in algorithms written against
399  /// QCAlgorithm that have been ported into the algorithm framework.
400  /// </summary>
401  /// <param name="insights">The array of insights to be emitted</param>
402  [DocumentationAttribute(AlgorithmFramework)]
403  public void EmitInsights(params Insight[] insights)
404  {
405  if (IsWarmingUp)
406  {
407  if (!_isEmitWarmupInsightWarningSent)
408  {
409  Error("Warning: insights emitted during algorithm warmup are ignored.");
410  _isEmitWarmupInsightWarningSent = true;
411  }
412  return;
413  }
414 
415  insights = InitializeInsights(insights);
416  OnInsightsGenerated(insights);
417  ProcessInsights(insights);
418  }
419 
420  /// <summary>
421  /// Manually emit insights from an algorithm.
422  /// This is typically invoked before calls to submit orders in algorithms written against
423  /// QCAlgorithm that have been ported into the algorithm framework.
424  /// </summary>
425  /// <param name="insight">The insight to be emitted</param>
426  [DocumentationAttribute(AlgorithmFramework)]
427  public void EmitInsights(Insight insight)
428  {
429  EmitInsights(new[] { insight });
430  }
431 
432  /// <summary>
433  /// Helper method used to validate insights and prepare them to be emitted
434  /// </summary>
435  /// <param name="insights">insights preparing to be emitted</param>
436  /// <returns>Validated insights</returns>
437  private Insight[] InitializeInsights(Insight[] insights)
438  {
439  List<Insight> validInsights = null;
440  for (var i = 0; i < insights.Length; i++)
441  {
442  var security = Securities[insights[i].Symbol];
443  if (security.IsDelisted)
444  {
445  if (!_isEmitDelistedInsightWarningSent)
446  {
447  Error($"QCAlgorithm.EmitInsights(): Warning: cannot emit insights for delisted securities, these will be discarded");
448  _isEmitDelistedInsightWarningSent = true;
449  }
450 
451  // If this is our first invalid insight, create the list and fill it with previous values
452  if (validInsights == null)
453  {
454  validInsights = new List<Insight>() {};
455  for (var j = 0; j < i; j++)
456  {
457  validInsights.Add(insights[j]);
458  }
459  }
460  }
461  else
462  {
463  // Initialize the insight fields
464  insights[i] = InitializeInsightFields(insights[i], security);
465 
466  // If we already had an invalid insight, this will have been initialized storing the valid ones.
467  if (validInsights != null)
468  {
469  validInsights.Add(insights[i]);
470  }
471  }
472  }
473 
474  return validInsights == null ? insights : validInsights.ToArray();
475 
476  }
477 
478  /// <summary>
479  /// Helper class used to set values not required to be set by alpha models
480  /// </summary>
481  /// <param name="insight">The <see cref="Insight"/> to set the values for</param>
482  /// <param name="security">The <see cref="Security"/> instance associated with this insight</param>
483  /// <returns>The same <see cref="Insight"/> instance with the values set</returns>
484  private Insight InitializeInsightFields(Insight insight, Security security)
485  {
486  insight.GeneratedTimeUtc = UtcTime;
487  switch (insight.Type)
488  {
489  case InsightType.Price:
490  insight.ReferenceValue = security.Price;
491  break;
492  case InsightType.Volatility:
493  insight.ReferenceValue = security.VolatilityModel.Volatility;
494  break;
495  }
496  insight.SourceModel = string.IsNullOrEmpty(insight.SourceModel) ? Alpha.GetModelName() : insight.SourceModel;
497 
498  var exchangeHours = MarketHoursDatabase.GetExchangeHours(insight.Symbol.ID.Market, insight.Symbol, insight.Symbol.SecurityType);
499  insight.SetPeriodAndCloseTime(exchangeHours);
500  return insight;
501  }
502  }
503 }