Lean  $LEAN_TAG$
QuoteBarFillForwardEnumerator.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.Collections;
18 using System.Collections.Generic;
19 using QuantConnect.Data;
21 
23 {
24  /// <summary>
25  /// The QuoteBarFillForwardEnumerator wraps an existing base data enumerator
26  /// If the current QuoteBar has null Bid and/or Ask bars, it copies them from the previous QuoteBar
27  /// </summary>
28  public class QuoteBarFillForwardEnumerator : IEnumerator<BaseData>
29  {
30  private QuoteBar _previous;
31  private readonly IEnumerator<BaseData> _enumerator;
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="FillForwardEnumerator"/> class
35  /// </summary>
36  public QuoteBarFillForwardEnumerator(IEnumerator<BaseData> enumerator)
37  {
38  _enumerator = enumerator;
39  }
40 
41  /// <summary>
42  /// Gets the element in the collection at the current position of the enumerator.
43  /// </summary>
44  /// <returns>
45  /// The element in the collection at the current position of the enumerator.
46  /// </returns>
47  public BaseData Current
48  {
49  get;
50  private set;
51  }
52 
53  /// <summary>
54  /// Gets the current element in the collection.
55  /// </summary>
56  /// <returns>
57  /// The current element in the collection.
58  /// </returns>
59  object IEnumerator.Current => Current;
60 
61  /// <summary>
62  /// Advances the enumerator to the next element of the collection.
63  /// </summary>
64  /// <returns>
65  /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
66  /// </returns>
67  /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
68  public bool MoveNext()
69  {
70  if (!_enumerator.MoveNext()) return false;
71 
72  var bar = _enumerator.Current as QuoteBar;
73  if (bar != null)
74  {
75  if (_previous != null)
76  {
77  if (bar.Bid == null)
78  {
79  bar.Bid = _previous.Bid;
80  }
81 
82  if (bar.Ask == null)
83  {
84  bar.Ask = _previous.Ask;
85  }
86  }
87 
88  _previous = bar;
89  }
90 
91  Current = _enumerator.Current;
92 
93  return true;
94  }
95 
96  /// <summary>
97  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
98  /// </summary>
99  public void Dispose()
100  {
101  _enumerator.Dispose();
102  }
103 
104  /// <summary>
105  /// Sets the enumerator to its initial position, which is before the first element in the collection.
106  /// </summary>
107  /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
108  public void Reset()
109  {
110  _enumerator.Reset();
111  }
112  }
113 }