Lean  $LEAN_TAG$
OptionUniverseSelectionModel.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;
21 
23 {
24  /// <summary>
25  /// Provides an implementation of <see cref="IUniverseSelectionModel"/> that subscribes to option 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>> _optionChainSymbolSelector;
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="OptionUniverseSelectionModel"/>
42  /// </summary>
43  /// <param name="refreshInterval">Time interval between universe refreshes</param>
44  /// <param name="optionChainSymbolSelector">Selects symbols from the provided option chain</param>
45  public OptionUniverseSelectionModel(TimeSpan refreshInterval, Func<DateTime, IEnumerable<Symbol>> optionChainSymbolSelector)
46  : this(refreshInterval, optionChainSymbolSelector, null)
47  {
48  }
49 
50  /// <summary>
51  /// Creates a new instance of <see cref="OptionUniverseSelectionModel"/>
52  /// </summary>
53  /// <param name="refreshInterval">Time interval between universe refreshes</param>
54  /// <param name="optionChainSymbolSelector">Selects symbols from the provided option 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>> optionChainSymbolSelector,
59  UniverseSettings universeSettings
60  )
61  {
62  _nextRefreshTimeUtc = DateTime.MinValue;
63 
64  _refreshInterval = refreshInterval;
65  _universeSettings = universeSettings;
66  _optionChainSymbolSelector = optionChainSymbolSelector;
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 uniqueUnderlyingSymbols = new HashSet<Symbol>();
79  foreach (var optionSymbol in _optionChainSymbolSelector(algorithm.UtcTime))
80  {
81  if (!optionSymbol.SecurityType.IsOption())
82  {
83  throw new ArgumentException("optionChainSymbolSelector must return option, index options, or futures options symbols.");
84  }
85 
86  // prevent creating duplicate option chains -- one per underlying
87  if (uniqueUnderlyingSymbols.Add(optionSymbol.Underlying))
88  {
89  yield return algorithm.CreateOptionChain(optionSymbol, Filter, _universeSettings);
90  }
91  }
92  }
93 
94  /// <summary>
95  /// Defines the option chain universe filter
96  /// </summary>
98  {
99  // NOP
100  return filter;
101  }
102  }
103 }