Lean  $LEAN_TAG$
FrontierAwareEnumerator.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.Generic;
19 using QuantConnect.Data;
20 
22 {
23  /// <summary>
24  /// Provides an implementation of <see cref="IEnumerator{BaseData}"/> that will not emit
25  /// data ahead of the frontier as specified by an instance of <see cref="ITimeProvider"/>.
26  /// An instance of <see cref="TimeZoneOffsetProvider"/> is used to convert between UTC
27  /// and the data's native time zone
28  /// </summary>
29  public class FrontierAwareEnumerator : IEnumerator<BaseData>
30  {
31  private BaseData _current;
32  private bool _needsMoveNext = true;
33 
34  private readonly ITimeProvider _timeProvider;
35  private readonly IEnumerator<BaseData> _enumerator;
36  private readonly TimeZoneOffsetProvider _offsetProvider;
37 
38  /// <summary>
39  /// Initializes a new instance of the <see cref="FrontierAwareEnumerator"/> class
40  /// </summary>
41  /// <param name="enumerator">The underlying enumerator to make frontier aware</param>
42  /// <param name="timeProvider">The time provider used for resolving the current frontier time</param>
43  /// <param name="offsetProvider">An offset provider used for converting the frontier UTC time into the data's native time zone</param>
44  public FrontierAwareEnumerator(IEnumerator<BaseData> enumerator, ITimeProvider timeProvider, TimeZoneOffsetProvider offsetProvider)
45  {
46  _enumerator = enumerator;
47  _timeProvider = timeProvider;
48  _offsetProvider = offsetProvider;
49  }
50 
51  /// <summary>
52  /// Advances the enumerator to the next element of the collection.
53  /// </summary>
54  /// <returns>
55  /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
56  /// </returns>
57  /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
58  public bool MoveNext()
59  {
60  var underlyingCurrent = _enumerator.Current;
61  var frontier = _timeProvider.GetUtcNow();
62  var localFrontier = new DateTime(frontier.Ticks + _offsetProvider.GetOffsetTicks(frontier));
63 
64  // if we moved next, but didn't emit, check to see if it's time to emit yet
65  if (!_needsMoveNext && underlyingCurrent != null)
66  {
67  if (underlyingCurrent.EndTime <= localFrontier)
68  {
69  // we can now emit the underlyingCurrent as part of this time slice
70  _current = underlyingCurrent;
71  _needsMoveNext = true;
72  }
73  else
74  {
75  // it's still not time to emit the underlyingCurrent, keep waiting for time to advance
76  _current = null;
77  _needsMoveNext = false;
78  }
79  return true;
80  }
81 
82  // we've exhausted the underlying enumerator, iteration completed
83  if (_needsMoveNext && !_enumerator.MoveNext())
84  {
85  _needsMoveNext = true;
86  _current = null;
87  return false;
88  }
89 
90  underlyingCurrent = _enumerator.Current;
91 
92  if (underlyingCurrent != null && underlyingCurrent.EndTime <= localFrontier)
93  {
94  _needsMoveNext = true;
95  _current = underlyingCurrent;
96  }
97  else
98  {
99  _current = null;
100  _needsMoveNext = underlyingCurrent == null;
101  }
102 
103  // technically we still need to return true since the iteration is not completed,
104  // however, Current may be null follow a true result here
105  return true;
106  }
107 
108  /// <summary>
109  /// Sets the enumerator to its initial position, which is before the first element in the collection.
110  /// </summary>
111  /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
112  public void Reset()
113  {
114  _enumerator.Reset();
115  }
116 
117  /// <summary>
118  /// Gets the element in the collection at the current position of the enumerator.
119  /// </summary>
120  /// <returns>
121  /// The element in the collection at the current position of the enumerator.
122  /// </returns>
123  public BaseData Current
124  {
125  get { return _current; }
126  }
127 
128  /// <summary>
129  /// Gets the current element in the collection.
130  /// </summary>
131  /// <returns>
132  /// The current element in the collection.
133  /// </returns>
134  /// <filterpriority>2</filterpriority>
135  object IEnumerator.Current
136  {
137  get { return Current; }
138  }
139 
140  /// <summary>
141  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
142  /// </summary>
143  /// <filterpriority>2</filterpriority>
144  public void Dispose()
145  {
146  _enumerator.Dispose();
147  }
148  }
149 }