Lean  $LEAN_TAG$
InceptionDateUniverseSelectionModel.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 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using Python.Runtime;
21 
23 {
24  /// <summary>
25  /// Inception Date Universe that accepts a Dictionary of DateTime keyed by String that represent
26  /// the Inception date for each ticker
27  /// </summary>
29  {
30  private readonly Queue<KeyValuePair<string, DateTime>> _queue;
31  private readonly List<string> _symbols;
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="InceptionDateUniverseSelectionModel"/> class
35  /// </summary>
36  /// <param name="name">A unique name for this universe</param>
37  /// <param name="tickersByDate">Dictionary of DateTime keyed by String that represent the Inception date for each ticker</param>
38  public InceptionDateUniverseSelectionModel(string name, Dictionary<string, DateTime> tickersByDate) :
39  base(name, (Func<DateTime, IEnumerable<string>>) null)
40  {
41  _queue = new Queue<KeyValuePair<string, DateTime>>(tickersByDate);
42  _symbols = new List<string>();
43  }
44 
45  /// <summary>
46  /// Initializes a new instance of the <see cref="InceptionDateUniverseSelectionModel"/> class
47  /// </summary>
48  /// <param name="name">A unique name for this universe</param>
49  /// <param name="tickersByDate">Dictionary of DateTime keyed by String that represent the Inception date for each ticker</param>
50  public InceptionDateUniverseSelectionModel(string name, PyObject tickersByDate) :
51  this(name, tickersByDate.ConvertToDictionary<string, DateTime>())
52  {
53  }
54 
55  /// <summary>
56  /// Returns all tickers that are trading at current algorithm Time
57  /// </summary>
58  public override IEnumerable<string> Select(QCAlgorithm algorithm, DateTime date)
59  {
60  // Move Symbols that are trading from the queue to a list
61  var added = new List<string>();
62  while (_queue.TryPeek(out var keyValuePair) && keyValuePair.Value <= date)
63  {
64  added.Add(_queue.Dequeue().Key);
65  }
66 
67  // If no pending for addition found, return Universe Unchanged
68  // Otherwise adds to list of current tickers and return it
69  if (added.Count == 0)
70  {
71  return Universe.Unchanged;
72  }
73 
74  _symbols.AddRange(added);
75  return _symbols;
76  }
77  }
78 }