Lean  $LEAN_TAG$
FutureUniverseSelectionModel.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;
19 using System.Collections.Generic;
21 
23 {
24  /// <summary>
25  /// Provides an implementation of <see cref="IUniverseSelectionModel"/> that subscribes to future chains
26  /// </summary>
28  {
29  private DateTime _nextRefreshTimeUtc;
30 
31  private readonly TimeSpan _refreshInterval;
32  private readonly UniverseSettings _universeSettings;
33  private readonly Func<DateTime, IEnumerable<Symbol>> _futureChainSymbolSelector;
34 
35  /// <summary>
36  /// Gets the next time the framework should invoke the `CreateUniverses` method to refresh the set of universes.
37  /// </summary>
38  public override DateTime GetNextRefreshTimeUtc() => _nextRefreshTimeUtc;
39 
40  /// <summary>
41  /// Creates a new instance of <see cref="FutureUniverseSelectionModel"/>
42  /// </summary>
43  /// <param name="refreshInterval">Time interval between universe refreshes</param>
44  /// <param name="futureChainSymbolSelector">Selects symbols from the provided future chain</param>
45  public FutureUniverseSelectionModel(TimeSpan refreshInterval, Func<DateTime, IEnumerable<Symbol>> futureChainSymbolSelector)
46  : this(refreshInterval, futureChainSymbolSelector, null)
47  {
48  }
49 
50  /// <summary>
51  /// Creates a new instance of <see cref="FutureUniverseSelectionModel"/>
52  /// </summary>
53  /// <param name="refreshInterval">Time interval between universe refreshes</param>
54  /// <param name="futureChainSymbolSelector">Selects symbols from the provided future chain</param>
55  /// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
57  TimeSpan refreshInterval,
58  Func<DateTime, IEnumerable<Symbol>> futureChainSymbolSelector,
59  UniverseSettings universeSettings
60  )
61  {
62  _nextRefreshTimeUtc = DateTime.MinValue;
63 
64  _refreshInterval = refreshInterval;
65  _universeSettings = universeSettings;
66  _futureChainSymbolSelector = futureChainSymbolSelector;
67  }
68 
69  /// <summary>
70  /// Creates the universes for this algorithm. Called once after <see cref="IAlgorithm.Initialize"/>
71  /// </summary>
72  /// <param name="algorithm">The algorithm instance to create universes for</param>
73  /// <returns>The universes to be used by the algorithm</returns>
74  public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
75  {
76  _nextRefreshTimeUtc = algorithm.UtcTime + _refreshInterval;
77 
78  var uniqueSymbols = new HashSet<Symbol>();
79  foreach (var futureSymbol in _futureChainSymbolSelector(algorithm.UtcTime))
80  {
81  if (futureSymbol.SecurityType != SecurityType.Future)
82  {
83  throw new ArgumentException("FutureChainSymbolSelector must return future symbols.");
84  }
85 
86  // prevent creating duplicate future chains -- one per symbol
87  if (uniqueSymbols.Add(futureSymbol))
88  {
89  foreach (var universe in algorithm.CreateFutureChain(futureSymbol, Filter, _universeSettings))
90  {
91  yield return universe;
92  }
93  }
94  }
95  }
96 
97  /// <summary>
98  /// Defines the future chain universe filter
99  /// </summary>
101  {
102  // NOP
103  return filter;
104  }
105  }
106 }