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="T:System.Collections.Generic.IDictionary`2"/>.
166  /// </summary>
167  /// <param name="key">The object to use as the key of the element to add.</param><param name="universe">The object to use as the value of the element to add.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
168  public void Add(Symbol key, Universe universe)
169  {
170  if (_universes.TryAdd(key, universe))
171  {
172  lock(_pendingChanges)
173  {
174  _pendingChanges.Enqueue(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, universe));
175  }
176  }
177  }
178 
179  /// <summary>
180  /// Will trigger collection changed event if required
181  /// </summary>
182  public void ProcessChanges()
183  {
184  NotifyCollectionChangedEventArgs universeChange;
185  do
186  {
187  lock (_pendingChanges)
188  {
189  _pendingChanges.TryDequeue(out universeChange);
190  }
191 
192  if (universeChange != null)
193  {
194  OnCollectionChanged(universeChange);
195  }
196  }
197  while (universeChange != null);
198  }
199 
200  /// <summary>
201  /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
202  /// </summary>
203  /// <returns>
204  /// 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"/>.
205  /// </returns>
206  /// <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>
207  public bool Remove(Symbol key)
208  {
209  Universe universe;
210  if (_universes.TryRemove(key, out universe))
211  {
212  universe.Dispose();
213  OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, universe));
214  return true;
215  }
216  return false;
217  }
218 
219  /// <summary>
220  /// Gets the value associated with the specified key.
221  /// </summary>
222  /// <returns>
223  /// true if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false.
224  /// </returns>
225  /// <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>
226  public bool TryGetValue(Symbol key, out Universe value)
227  {
228  return _universes.TryGetValue(key, out value);
229  }
230 
231  /// <summary>
232  /// Gets or sets the element with the specified key.
233  /// </summary>
234  /// <returns>
235  /// The element with the specified key.
236  /// </returns>
237  /// <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>
238  public Universe this[Symbol symbol]
239  {
240  get
241  {
242  if (!_universes.ContainsKey(symbol))
243  {
244  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)}\")'");
245  }
246  return _universes[symbol];
247  }
248  set
249  {
250  Universe existing;
251  if (_universes.TryGetValue(symbol, out existing) && existing != value)
252  {
253  throw new ArgumentException($"Unable to over write existing Universe: {symbol.Value}");
254  }
255 
256  // no security exists for the specified symbol key, add it now
257  if (existing == null)
258  {
259  Add(symbol, value);
260  }
261  }
262  }
263 
264  /// <summary>
265  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
266  /// </summary>
267  /// <returns>
268  /// 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"/>.
269  /// </returns>
270  public ICollection<Symbol> Keys => _universes.Select(x => x.Key).ToList();
271 
272  /// <summary>
273  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
274  /// </summary>
275  /// <returns>
276  /// 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"/>.
277  /// </returns>
278  public ICollection<Universe> Values => _universes.Select(x => x.Value).ToList();
279 
280  #endregion
281 
282  /// <summary>
283  /// Event invocator for the <see cref="CollectionChanged"/> event
284  /// </summary>
285  /// <param name="e"></param>
286  protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
287  {
288  CollectionChanged?.Invoke(this, e);
289  }
290  }
291 }