Lean  $LEAN_TAG$
CompositeUniverseSelectionModel.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 System.Collections.Generic;
18 using System.Linq;
19 using Python.Runtime;
21 using QuantConnect.Util;
22 
24 {
25  /// <summary>
26  /// Provides an implementation of <see cref="IUniverseSelectionModel"/> that combines multiple universe
27  /// selection models into a single model.
28  /// </summary>
30  {
31  private readonly List<IUniverseSelectionModel> _universeSelectionModels = new List<IUniverseSelectionModel>();
32  private bool _alreadyCalledCreateUniverses;
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="CompositeUniverseSelectionModel"/> class
36  /// </summary>
37  /// <param name="universeSelectionModels">The individual universe selection models defining this composite model</param>
38  public CompositeUniverseSelectionModel(params IUniverseSelectionModel[] universeSelectionModels)
39  {
40  if (universeSelectionModels.IsNullOrEmpty())
41  {
42  throw new ArgumentException("Must specify at least 1 universe selection model for the CompositeUniverseSelectionModel");
43  }
44 
45  _universeSelectionModels.AddRange(universeSelectionModels);
46  }
47 
48  /// <summary>
49  /// Initializes a new instance of the <see cref="CompositeUniverseSelectionModel"/> class
50  /// </summary>
51  /// <param name="universeSelectionModels">The individual universe selection models defining this composite model</param>
52  public CompositeUniverseSelectionModel(params PyObject[] universeSelectionModels)
53  {
54  if (universeSelectionModels.IsNullOrEmpty())
55  {
56  throw new ArgumentException("Must specify at least 1 universe selection model for the CompositeUniverseSelectionModel");
57  }
58 
59  foreach (var pyUniverseSelectionModel in universeSelectionModels)
60  {
61  AddUniverseSelection(pyUniverseSelectionModel);
62  }
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the <see cref="CompositeUniverseSelectionModel"/> class
67  /// </summary>
68  /// <param name="universeSelectionModel">The individual universe selection model defining this composite model</param>
69  public CompositeUniverseSelectionModel(PyObject universeSelectionModel)
70  : this(new[] { universeSelectionModel })
71  {
72 
73  }
74 
75  /// <summary>
76  /// Adds a new <see cref="IUniverseSelectionModel"/>
77  /// </summary>
78  /// <param name="universeSelectionModel">The universe selection model to add</param>
79  public void AddUniverseSelection(IUniverseSelectionModel universeSelectionModel)
80  {
81  _universeSelectionModels.Add(universeSelectionModel);
82  }
83 
84  /// <summary>
85  /// Adds a new <see cref="IUniverseSelectionModel"/>
86  /// </summary>
87  /// <param name="pyUniverseSelectionModel">The universe selection model to add</param>
88  public void AddUniverseSelection(PyObject pyUniverseSelectionModel)
89  {
90  IUniverseSelectionModel selectionModel;
91  if (!pyUniverseSelectionModel.TryConvert(out selectionModel))
92  {
93  selectionModel = new UniverseSelectionModelPythonWrapper(pyUniverseSelectionModel);
94  }
95  _universeSelectionModels.Add(selectionModel);
96  }
97 
98  /// <summary>
99  /// Gets the next time the framework should invoke the `CreateUniverses` method to refresh the set of universes.
100  /// </summary>
101  public override DateTime GetNextRefreshTimeUtc()
102  {
103  return _universeSelectionModels.Min(model => model.GetNextRefreshTimeUtc());
104  }
105 
106  /// <summary>
107  /// Creates the universes for this algorithm.
108  /// </summary>
109  /// <param name="algorithm">The algorithm instance to create universes for</param>
110  /// <returns>The universes to be used by the algorithm</returns>
111  public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
112  {
113  foreach (var universeSelectionModel in _universeSelectionModels)
114  {
115  var selectionRefreshTime = universeSelectionModel.GetNextRefreshTimeUtc();
116  var refreshTime = algorithm.UtcTime >= selectionRefreshTime;
117  if (!_alreadyCalledCreateUniverses // first initial call
118  || refreshTime
119  || selectionRefreshTime == DateTime.MaxValue)
120  {
121  foreach (var universe in universeSelectionModel.CreateUniverses(algorithm))
122  {
123  yield return universe;
124  }
125  }
126  }
127  _alreadyCalledCreateUniverses = true;
128  }
129  }
130 }