Lean  $LEAN_TAG$
ConcatEnumerator.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 
17 using System;
18 using System.Linq;
19 using QuantConnect.Data;
20 using QuantConnect.Util;
21 using System.Collections;
22 using QuantConnect.Logging;
23 using System.Collections.Generic;
24 
26 {
27  /// <summary>
28  /// Enumerator that will concatenate enumerators together sequentially enumerating them in the provided order
29  /// </summary>
30  public class ConcatEnumerator : IEnumerator<BaseData>
31  {
32  private readonly List<IEnumerator<BaseData>> _enumerators;
33  private readonly bool _skipDuplicateEndTimes;
34  private DateTime? _lastEnumeratorEndTime;
35  private int _currentIndex;
36 
37  /// <summary>
38  /// The current BaseData object
39  /// </summary>
40  public BaseData Current { get; set; }
41 
42  /// <summary>
43  /// True if emitting a null data point is expected
44  /// </summary>
45  /// <remarks>Warmup enumerators are not allowed to return true and setting current to Null, this is because it's not a valid behavior for backtesting enumerators,
46  /// for example <see cref="FillForwardEnumerator"/></remarks>
47  public bool CanEmitNull { get; set; }
48 
49  object IEnumerator.Current => Current;
50 
51  /// <summary>
52  /// Creates a new instance
53  /// </summary>
54  /// <param name="skipDuplicateEndTimes">True will skip data points from enumerators if before or at the last end time</param>
55  /// <param name="enumerators">The sequence of enumerators to concatenate. Note that the order here matters, it will consume enumerators
56  /// and dispose of them, even if they return true and their current is null, except for the last which will be kept!</param>
57  public ConcatEnumerator(bool skipDuplicateEndTimes,
58  params IEnumerator<BaseData>[] enumerators
59  )
60  {
61  CanEmitNull = true;
62  _skipDuplicateEndTimes = skipDuplicateEndTimes;
63  _enumerators = enumerators.Where(enumerator => enumerator != null).ToList();
64  }
65 
66  /// <summary>
67  /// Advances the enumerator to the next element of the collection.
68  /// </summary>
69  /// <returns>True if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
70  public bool MoveNext()
71  {
72  for (; _currentIndex < _enumerators.Count; _currentIndex++)
73  {
74  var enumerator = _enumerators[_currentIndex];
75  while (enumerator.MoveNext())
76  {
77  if (enumerator.Current == null && (_currentIndex < _enumerators.Count - 1 || !CanEmitNull))
78  {
79  // if there are more enumerators and the current stopped providing data drop it
80  // in live trading, some enumerators will always return true (see TimeTriggeredUniverseSubscriptionEnumeratorFactory & InjectionEnumerator)
81  // but unless it's the last enumerator we drop it, because these first are the warmup enumerators
82  // or we are not allowed to return null
83  break;
84  }
85 
86  if (_skipDuplicateEndTimes
87  && _lastEnumeratorEndTime.HasValue
88  && enumerator.Current != null
89  && enumerator.Current.EndTime <= _lastEnumeratorEndTime)
90  {
91  continue;
92  }
93 
94  Current = enumerator.Current;
95  return true;
96  }
97 
98  _lastEnumeratorEndTime = Current?.EndTime;
99 
100  if (Log.DebuggingEnabled)
101  {
102  Log.Debug($"ConcatEnumerator.MoveNext(): disposing enumerator at position: {_currentIndex} Name: {enumerator.GetType().Name}");
103  }
104 
105  // we wont be using this enumerator again, dispose of it and clear reference
106  enumerator.DisposeSafely();
107  _enumerators[_currentIndex] = null;
108  }
109 
110  Current = null;
111  return false;
112  }
113 
114  /// <summary>
115  /// Sets the enumerator to its initial position, which is before the first element in the collection.
116  /// </summary>
117  public void Reset()
118  {
119  throw new InvalidOperationException($"Can not reset {nameof(ConcatEnumerator)}");
120  }
121 
122  /// <summary>
123  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
124  /// </summary>
125  public void Dispose()
126  {
127  foreach (var enumerator in _enumerators)
128  {
129  enumerator.DisposeSafely();
130  }
131  }
132  }
133 }