Lean  $LEAN_TAG$
CustomUniverseSelectionModel.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 Python.Runtime;
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 Symbol _symbol;
35  private readonly Func<DateTime, IEnumerable<string>> _selector;
36  private readonly UniverseSettings _universeSettings;
37  private readonly TimeSpan _interval;
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="CustomUniverseSelectionModel"/> class
41  /// for <see cref="Market.USA"/> and <see cref="SecurityType.Equity"/>
42  /// using the algorithm's universe settings
43  /// </summary>
44  /// <param name="name">A unique name for this universe</param>
45  /// <param name="selector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
46  public CustomUniverseSelectionModel(string name, Func<DateTime, IEnumerable<string>> selector)
47  : this(SecurityType.Equity, name, Market.USA, selector, null, Time.OneDay)
48  {
49  }
50 
51  /// <summary>
52  /// Initializes a new instance of the <see cref="CustomUniverseSelectionModel"/> class
53  /// for <see cref="Market.USA"/> and <see cref="SecurityType.Equity"/>
54  /// using the algorithm's universe settings
55  /// </summary>
56  /// <param name="name">A unique name for this universe</param>
57  /// <param name="selector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
58  public CustomUniverseSelectionModel(string name, PyObject selector)
59  : this(SecurityType.Equity, name, Market.USA, selector, null, Time.OneDay)
60  {
61  }
62 
63  /// <summary>
64  /// Initializes a new instance of the <see cref="CustomUniverseSelectionModel"/> class
65  /// </summary>
66  /// <param name="securityType">The security type of the universe</param>
67  /// <param name="name">A unique name for this universe</param>
68  /// <param name="market">The market of the universe</param>
69  /// <param name="selector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
70  /// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
71  /// <param name="interval">The interval at which selection should be performed</param>
72  public CustomUniverseSelectionModel(SecurityType securityType, string name, string market, Func<DateTime, IEnumerable<string>> selector, UniverseSettings universeSettings, TimeSpan interval)
73  {
74  _interval = interval;
75  _selector = selector;
76  _universeSettings = universeSettings;
77  _symbol = Symbol.Create($"{name}-{securityType}-{market}", securityType, market);
78  }
79 
80  /// <summary>
81  /// Initializes a new instance of the <see cref="CustomUniverseSelectionModel"/> class
82  /// </summary>
83  /// <param name="securityType">The security type of the universe</param>
84  /// <param name="name">A unique name for this universe</param>
85  /// <param name="market">The market of the universe</param>
86  /// <param name="selector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
87  /// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
88  /// <param name="interval">The interval at which selection should be performed</param>
89  public CustomUniverseSelectionModel(SecurityType securityType, string name, string market, PyObject selector, UniverseSettings universeSettings, TimeSpan interval)
90  : this(
91  securityType,
92  name,
93  market,
94  selector.ConvertToDelegate<Func<DateTime, object>>().ConvertToUniverseSelectionStringDelegate(),
95  universeSettings,
96  interval
97  )
98  {
99  }
100 
101  /// <summary>
102  /// Creates the universes for this algorithm. Called at algorithm start.
103  /// </summary>
104  /// <returns>The universes defined by this model</returns>
105  public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
106  {
107  var universeSettings = _universeSettings ?? algorithm.UniverseSettings;
108  var entry = MarketHours.GetEntry(_symbol.ID.Market, (string) null, _symbol.SecurityType);
109 
110  var config = new SubscriptionDataConfig(
111  universeSettings.Resolution == Resolution.Tick ? typeof(Tick) : typeof(TradeBar),
112  _symbol,
113  universeSettings.Resolution,
114  entry.DataTimeZone,
115  entry.ExchangeHours.TimeZone,
116  universeSettings.FillForward,
117  universeSettings.ExtendedMarketHours,
118  true
119  );
120 
121  yield return new CustomUniverse(config, universeSettings, _interval, dt => Select(algorithm, dt));
122  }
123 
124  /// <summary>
125  ///
126  /// </summary>
127  /// <param name="algorithm"></param>
128  /// <param name="date"></param>
129  /// <returns></returns>
130  public virtual IEnumerable<string> Select(QCAlgorithm algorithm, DateTime date)
131  {
132  if (_selector == null)
133  {
134  throw new ArgumentNullException(nameof(_selector));
135  }
136 
137  return _selector(date);
138  }
139 
140  /// <summary>
141  /// Returns a string that represents the current object
142  /// </summary>
143  public override string ToString() => _symbol.Value;
144  }
145 }