Lean  $LEAN_TAG$
ScheduledUniverseSelectionModel.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 NodaTime;
19 using Python.Runtime;
24 
26 {
27  /// <summary>
28  /// Defines a universe selection model that invokes a selector function on a specific scheduled given by an <see cref="IDateRule"/> and an <see cref="ITimeRule"/>
29  /// </summary>
31  {
32  private readonly IDateRule _dateRule;
33  private readonly ITimeRule _timeRule;
34  private readonly Func<DateTime, IEnumerable<Symbol>> _selector;
35  private readonly DateTimeZone _timeZone;
36  private readonly UniverseSettings _settings;
37 
38  /// <summary>
39  /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class using the algorithm's time zone
40  /// </summary>
41  /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
42  /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
43  /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
44  /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
45  public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, Func<DateTime, IEnumerable<Symbol>> selector, UniverseSettings settings = null)
46  {
47  _dateRule = dateRule;
48  _timeRule = timeRule;
49  _selector = selector;
50  _settings = settings;
51  }
52 
53  /// <summary>
54  /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class
55  /// </summary>
56  /// <param name="timeZone">The time zone the date/time rules are in</param>
57  /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
58  /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
59  /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
60  /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
61  public ScheduledUniverseSelectionModel(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, Func<DateTime, IEnumerable<Symbol>> selector, UniverseSettings settings = null)
62  {
63  _timeZone = timeZone;
64  _dateRule = dateRule;
65  _timeRule = timeRule;
66  _selector = selector;
67  _settings = settings;
68  }
69 
70  /// <summary>
71  /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class using the algorithm's time zone
72  /// </summary>
73  /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
74  /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
75  /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
76  /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
77  public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null)
78  : this(null, dateRule, timeRule, selector, settings)
79  {
80  }
81 
82  /// <summary>
83  /// Initializes a new instance of the <see cref="ScheduledUniverseSelectionModel"/> class
84  /// </summary>
85  /// <param name="timeZone">The time zone the date/time rules are in</param>
86  /// <param name="dateRule">Date rule defines what days the universe selection function will be invoked</param>
87  /// <param name="timeRule">Time rule defines what times on each day selected by date rule the universe selection function will be invoked</param>
88  /// <param name="selector">Selector function accepting the date time firing time and returning the universe selected symbols</param>
89  /// <param name="settings">Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings</param>
90  public ScheduledUniverseSelectionModel(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null)
91  {
92  Func<DateTime, object> func;
93  selector.TryConvertToDelegate(out func);
94  _timeZone = timeZone;
95  _dateRule = dateRule;
96  _timeRule = timeRule;
97  _selector = func.ConvertSelectionSymbolDelegate();
98  _settings = settings;
99  }
100 
101  /// <summary>
102  /// Creates the universes for this algorithm. Called once after <see cref="IAlgorithm.Initialize"/>
103  /// </summary>
104  /// <param name="algorithm">The algorithm instance to create universes for</param>
105  /// <returns>The universes to be used by the algorithm</returns>
106  public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
107  {
108  yield return new ScheduledUniverse(
109  // by default ITimeRule yields in UTC
110  _timeZone ?? TimeZones.Utc,
111  _dateRule,
112  _timeRule,
113  _selector,
114  _settings ?? algorithm.UniverseSettings
115  );
116  }
117  }
118 }