Lean  $LEAN_TAG$
SubscriptionDataEnumerator.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.Collections;
17 using System.Collections.Generic;
18 using QuantConnect.Data;
20 
22 {
23  /// <summary>
24  /// An <see cref="IEnumerator{SubscriptionData}"/> which wraps an existing <see cref="IEnumerator{BaseData}"/>.
25  /// </summary>
26  /// <remarks>Using this class is important, versus directly yielding, because we setup the <see cref="Dispose"/> chain</remarks>
27  public class SubscriptionDataEnumerator : IEnumerator<SubscriptionData>
28  {
29  private readonly IEnumerator<BaseData> _enumerator;
30  private readonly SubscriptionDataConfig _configuration;
31  private readonly SecurityExchangeHours _exchangeHours;
32  private readonly TimeZoneOffsetProvider _offsetProvider;
33  private readonly bool _isUniverse;
34  private readonly bool _dailyStrictEndTimeEnabled;
35 
36  object IEnumerator.Current => Current;
37 
38  /// <summary>
39  /// Gets the element in the collection at the current position of the enumerator.
40  /// </summary>
41  public SubscriptionData Current { get; private set; }
42 
43  /// <summary>
44  /// Creates a new instance
45  /// </summary>
46  /// <param name="configuration">The subscription's configuration</param>
47  /// <param name="exchangeHours">The security's exchange hours</param>
48  /// <param name="offsetProvider">The subscription's time zone offset provider</param>
49  /// <param name="enumerator">The underlying data enumerator</param>
50  /// <param name="isUniverse">The subscription is a universe subscription</param>
51  /// <returns>A subscription data enumerator</returns>
53  SecurityExchangeHours exchangeHours,
54  TimeZoneOffsetProvider offsetProvider,
55  IEnumerator<BaseData> enumerator,
56  bool isUniverse,
57  bool dailyStrictEndTimeEnabled)
58  {
59  _enumerator = enumerator;
60  _offsetProvider = offsetProvider;
61  _exchangeHours = exchangeHours;
62  _configuration = configuration;
63  _isUniverse = isUniverse;
64  _dailyStrictEndTimeEnabled = dailyStrictEndTimeEnabled;
65  }
66 
67  /// <summary>
68  /// Advances the enumerator to the next element of the collection.
69  /// </summary>
70  /// <returns>True if the enumerator was successfully advanced to the next element;
71  /// False if the enumerator has passed the end of the collection.</returns>
72  public bool MoveNext()
73  {
74  var result = _enumerator.MoveNext();
75  if (result)
76  {
77  // Use our config filter to see if we should emit this
78  // This currently catches Auxiliary data that we don't want to emit
79  if (_enumerator.Current != null && !_configuration.ShouldEmitData(_enumerator.Current, _isUniverse))
80  {
81  // We shouldn't emit this data, so we will MoveNext() again.
82  return MoveNext();
83  }
84 
85  Current = SubscriptionData.Create(_dailyStrictEndTimeEnabled, _configuration, _exchangeHours, _offsetProvider, _enumerator.Current, _configuration.DataNormalizationMode);
86  }
87  return result;
88  }
89 
90  /// <summary>
91  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
92  /// </summary>
93  public void Dispose()
94  {
95  _enumerator.Dispose();
96  }
97 
98  /// <summary>
99  /// Sets the enumerator to its initial position, which is before the first element in the collection.
100  /// </summary>
101  public void Reset()
102  {
103  _enumerator.Reset();
104  }
105  }
106 }