Lean  $LEAN_TAG$
UniverseManager.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;
18 using System.Collections.Concurrent;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
21 using System.Linq;
23 using QuantConnect.Util;
24 
26 {
27  /// <summary>
28  /// Manages the algorithm's collection of universes
29  /// </summary>
30  public class UniverseManager : IDictionary<Symbol, Universe>, INotifyCollectionChanged
31  {
32  private readonly Queue<NotifyCollectionChangedEventArgs> _pendingChanges = new();
33  private readonly ConcurrentDictionary<Symbol, Universe> _universes;
34 
35  /// <summary>
36  /// Event fired when a universe is added or removed
37  /// </summary>
38  public event NotifyCollectionChangedEventHandler CollectionChanged;
39 
40  /// <summary>
41  /// Read-only dictionary containing all active securities. An active security is
42  /// a security that is currently selected by the universe or has holdings or open orders.
43  /// </summary>
44  public IReadOnlyDictionary<Symbol, Security> ActiveSecurities => this
45  .SelectMany(ukvp => ukvp.Value.Members.Select(mkvp => mkvp.Value))
46  .DistinctBy(s => s.Symbol).ToDictionary(s => s.Symbol);
47 
48  /// <summary>
49  /// Initializes a new instance of the <see cref="UniverseManager"/> class
50  /// </summary>
51  public UniverseManager()
52  {
53  _universes = new ConcurrentDictionary<Symbol, Universe>();
54  }
55 
56  #region IDictionary implementation
57 
58  /// <summary>
59  /// Returns an enumerator that iterates through the collection.
60  /// </summary>
61  /// <returns>
62  /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
63  /// </returns>
64  /// <filterpriority>1</filterpriority>
65  public IEnumerator<KeyValuePair<Symbol, Universe>> GetEnumerator()
66  {
67  return _universes.GetEnumerator();
68  }
69 
70  /// <summary>
71  /// Returns an enumerator that iterates through a collection.
72  /// </summary>
73  /// <returns>
74  /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
75  /// </returns>
76  /// <filterpriority>2</filterpriority>
77  IEnumerator IEnumerable.GetEnumerator()
78  {
79  return ((IEnumerable)_universes).GetEnumerator();
80  }
81 
82  /// <summary>
83  /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
84  /// </summary>
85  /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
86  public void Add(KeyValuePair<Symbol, Universe> item)
87  {
88  Add(item.Key, item.Value);
89  }
90 
91  /// <summary>
92  /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
93  /// </summary>
94  /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
95  public void Clear()
96  {
97  _universes.Clear();
98  }
99 
100  /// <summary>
101  /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
102  /// </summary>
103  /// <returns>
104  /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
105  /// </returns>
106  /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
107  public bool Contains(KeyValuePair<Symbol, Universe> item)
108  {
109  return ContainsKey(item.Key);
110  }
111 
112  /// <summary>
113  /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
114  /// </summary>
115  /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception><exception cref="T:System.ArgumentException">The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</exception>
116  public void CopyTo(KeyValuePair<Symbol, Universe>[] array, int arrayIndex)
117  {
118  ((IDictionary<Symbol, Universe>)_universes).CopyTo(array, arrayIndex);
119  }
120 
121  /// <summary>
122  /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
123  /// </summary>
124  /// <returns>
125  /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
126  /// </returns>
127  /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
128  public bool Remove(KeyValuePair<Symbol, Universe> item)
129  {
130  return Remove(item.Key);
131  }
132 
133  /// <summary>
134  /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
135  /// </summary>
136  /// <returns>
137  /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
138  /// </returns>
139  public int Count => _universes.Skip(0).Count();
140 
141  /// <summary>
142  /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
143  /// </summary>
144  /// <returns>
145  /// true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
146  /// </returns>
147  public bool IsReadOnly
148  {
149  get { return false; }
150  }
151 
152  /// <summary>
153  /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key.
154  /// </summary>
155  /// <returns>
156  /// true if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false.
157  /// </returns>
158  /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception>
159  public bool ContainsKey(Symbol key)
160  {
161  return _universes.ContainsKey(key);
162  }
163 
164  /// <summary>
165  /// Adds an element with the provided key and value to the <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"/>.
166  /// </summary>
167  /// <param name="key">The object to use as the key of the element to add.
168  /// </param><param name="value">The object to use as the value of the element to add.</param>
169  /// <exception cref="System.ArgumentNullException"><paramref name="key"/> is null.</exception>
170  /// <exception cref="System.ArgumentException">An element with the same key already exists in the <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"/>.</exception>
171  /// <exception cref="System.NotSupportedException">The <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"/> is read-only.</exception>
172  public void Add(Symbol key, Universe value)
173  {
174  if (_universes.TryAdd(key, value))
175  {
176  lock(_pendingChanges)
177  {
178  _pendingChanges.Enqueue(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value));
179  }
180  }
181  }
182 
183  /// <summary>
184  /// Will trigger collection changed event if required
185  /// </summary>
186  public void ProcessChanges()
187  {
188  NotifyCollectionChangedEventArgs universeChange;
189  do
190  {
191  lock (_pendingChanges)
192  {
193  _pendingChanges.TryDequeue(out universeChange);
194  }
195 
196  if (universeChange != null)
197  {
198  OnCollectionChanged(universeChange);
199  }
200  }
201  while (universeChange != null);
202  }
203 
204  /// <summary>
205  /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
206  /// </summary>
207  /// <returns>
208  /// true if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>.
209  /// </returns>
210  /// <param name="key">The key of the element to remove.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
211  public bool Remove(Symbol key)
212  {
213  Universe universe;
214  if (_universes.TryRemove(key, out universe))
215  {
216  universe.Dispose();
217  OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, universe));
218  return true;
219  }
220  return false;
221  }
222 
223  /// <summary>
224  /// Gets the value associated with the specified key.
225  /// </summary>
226  /// <returns>
227  /// true if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false.
228  /// </returns>
229  /// <param name="key">The key whose value to get.</param><param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception>
230  public bool TryGetValue(Symbol key, out Universe value)
231  {
232  return _universes.TryGetValue(key, out value);
233  }
234 
235  /// <summary>
236  /// Gets or sets the element with the specified key.
237  /// </summary>
238  /// <returns>
239  /// The element with the specified key.
240  /// </returns>
241  /// <param name="symbol">The key of the element to get or set.</param><exception cref="T:System.ArgumentNullException"><paramref name="symbol"/> is null.</exception><exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name="symbol"/> is not found.</exception><exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
242  public Universe this[Symbol symbol]
243  {
244  get
245  {
246  if (!_universes.ContainsKey(symbol))
247  {
248  throw new KeyNotFoundException($"This universe symbol ({symbol}) was not found in your universe list. Please add this security or check it exists before using it with 'Universes.ContainsKey(\"{SymbolCache.GetTicker(symbol)}\")'");
249  }
250  return _universes[symbol];
251  }
252  set
253  {
254  Universe existing;
255  if (_universes.TryGetValue(symbol, out existing) && existing != value)
256  {
257  throw new ArgumentException($"Unable to over write existing Universe: {symbol.Value}");
258  }
259 
260  // no security exists for the specified symbol key, add it now
261  if (existing == null)
262  {
263  Add(symbol, value);
264  }
265  }
266  }
267 
268  /// <summary>
269  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
270  /// </summary>
271  /// <returns>
272  /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
273  /// </returns>
274  public ICollection<Symbol> Keys => _universes.Select(x => x.Key).ToList();
275 
276  /// <summary>
277  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
278  /// </summary>
279  /// <returns>
280  /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
281  /// </returns>
282  public ICollection<Universe> Values => _universes.Select(x => x.Value).ToList();
283 
284  #endregion
285 
286  /// <summary>
287  /// Event invocator for the <see cref="CollectionChanged"/> event
288  /// </summary>
289  /// <param name="e"></param>
290  protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
291  {
292  CollectionChanged?.Invoke(this, e);
293  }
294  }
295 }