Lean  $LEAN_TAG$
ManualUniverseSelectionModel.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 QuantConnect.Data;
23 
25 {
26  /// <summary>
27  /// Provides an implementation of <see cref="IUniverseSelectionModel"/> that simply
28  /// subscribes to the specified set of symbols
29  /// </summary>
31  {
32  private static readonly MarketHoursDatabase MarketHours = MarketHoursDatabase.FromDataFolder();
33 
34  private readonly IReadOnlyList<Symbol> _symbols;
35  private readonly UniverseSettings _universeSettings;
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="ManualUniverseSelectionModel"/> class using the algorithm's
39  /// security initializer and universe settings
40  /// </summary>
42  : this(Enumerable.Empty<Symbol>())
43  {
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="ManualUniverseSelectionModel"/> class using the algorithm's
48  /// security initializer and universe settings
49  /// </summary>
50  /// <param name="symbols">The symbols to subscribe to.
51  /// Should not send in symbols at <see cref="QCAlgorithm.Securities"/> since those will be managed by the <see cref="UserDefinedUniverse"/></param>
52  public ManualUniverseSelectionModel(IEnumerable<Symbol> symbols)
53  : this(symbols.ToArray())
54  {
55  }
56 
57  /// <summary>
58  /// Initializes a new instance of the <see cref="ManualUniverseSelectionModel"/> class using the algorithm's
59  /// security initializer and universe settings
60  /// </summary>
61  /// <param name="symbols">The symbols to subscribe to
62  /// Should not send in symbols at <see cref="QCAlgorithm.Securities"/> since those will be managed by the <see cref="UserDefinedUniverse"/></param>
63  public ManualUniverseSelectionModel(params Symbol[] symbols)
64  : this (symbols?.AsEnumerable(), null)
65  {
66  }
67 
68  /// <summary>
69  /// Initializes a new instance of the <see cref="ManualUniverseSelectionModel"/> class
70  /// </summary>
71  /// <param name="symbols">The symbols to subscribe to
72  /// Should not send in symbols at <see cref="QCAlgorithm.Securities"/> since those will be managed by the <see cref="UserDefinedUniverse"/></param>
73  /// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
74  public ManualUniverseSelectionModel(IEnumerable<Symbol> symbols, UniverseSettings universeSettings)
75  {
76  if (symbols == null)
77  {
78  throw new ArgumentNullException(nameof(symbols));
79  }
80 
81  _symbols = symbols.Where(s => !s.IsCanonical()).ToList();
82  _universeSettings = universeSettings;
83 
84  foreach (var symbol in _symbols)
85  {
86  SymbolCache.Set(symbol.Value, symbol);
87  }
88  }
89 
90  /// <summary>
91  /// Creates the universes for this algorithm.
92  /// Called at algorithm start.
93  /// </summary>
94  /// <returns>The universes defined by this model</returns>
95  public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
96  {
97  var universeSettings = _universeSettings ?? algorithm.UniverseSettings;
98  var resolution = universeSettings.Resolution;
99  var type = resolution == Resolution.Tick ? typeof(Tick) : typeof(TradeBar);
100 
101  // universe per security type/market
102  foreach (var grp in _symbols.GroupBy(s => new { s.ID.Market, s.SecurityType }))
103  {
105 
106  var market = grp.Key.Market;
107  var securityType = grp.Key.SecurityType;
108  var hashCode = 1;
109  foreach (var symbol in grp)
110  {
111  hashCode = hashCode * 31 + symbol.GetHashCode();
112  }
113  var universeSymbol = Symbol.Create($"manual-universe-selection-model-{securityType}-{market}-{hashCode}", securityType, market);
114  if (securityType == SecurityType.Base)
115  {
116  // add an entry for this custom universe symbol -- we don't really know the time zone for sure,
117  // but we set it to TimeZones.NewYork in AddData, also, since this is a manual universe, the time
118  // zone doesn't actually matter since this universe specifically doesn't do anything with data.
119  var symbolString = MarketHoursDatabase.GetDatabaseSymbolKey(universeSymbol);
121  entry = MarketHours.SetEntry(market, symbolString, securityType, alwaysOpen, TimeZones.NewYork);
122  }
123  else
124  {
125  entry = MarketHours.GetEntry(market, (string) null, securityType);
126  }
127 
128  var config = new SubscriptionDataConfig(type, universeSymbol, resolution, entry.DataTimeZone, entry.ExchangeHours.TimeZone, false, false, true);
129  yield return new ManualUniverse(config, universeSettings, grp);
130  }
131  }
132  }
133 }