Lean  $LEAN_TAG$
StrictDailyEndTimesEnumerator.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 QuantConnect.Data;
18 using QuantConnect.Util;
19 using System.Collections;
21 using System.Collections.Generic;
22 
24 {
25  /// <summary>
26  /// Enumerator that will handle adjusting daily strict end times if appropriate
27  /// </summary>
28  public class StrictDailyEndTimesEnumerator : IEnumerator<BaseData>
29  {
30  private readonly DateTime _localStartTime;
31  private readonly SecurityExchangeHours _securityExchange;
32  private readonly IEnumerator<BaseData> _underlying;
33 
34  /// <summary>
35  /// Current value of the enumerator
36  /// </summary>
37  public BaseData Current { get; private set; }
38 
39  object IEnumerator.Current => Current;
40 
41  /// <summary>
42  /// Creates a new instance
43  /// </summary>
44  public StrictDailyEndTimesEnumerator(IEnumerator<BaseData> underlying, SecurityExchangeHours securityExchangeHours, DateTime localStartTime)
45  {
46  _underlying = underlying;
47  _localStartTime = localStartTime;
48  _securityExchange = securityExchangeHours;
49  }
50 
51  /// <summary>
52  /// Move to the next date
53  /// </summary>
54  public bool MoveNext()
55  {
56  Current = null;
57  bool result;
58  do
59  {
60  result = _underlying.MoveNext();
61  if (!result || !LeanData.UseDailyStrictEndTimes(_underlying.Current?.GetType()))
62  {
63  break;
64  }
65 
66  // before setting the strict daily end times, let's clone it because underlying enumerator (SubscriptionDataReader) might be using it
67  var pontentialNewBar = _underlying.Current.Clone();
68  if (LeanData.SetStrictEndTimes(pontentialNewBar, _securityExchange) && pontentialNewBar.EndTime >= _localStartTime)
69  {
70  Current = pontentialNewBar;
71  break;
72  }
73  }
74  while (true);
75 
76  return result;
77  }
78 
79  /// <summary>
80  /// Reset the enumerator
81  /// </summary>
82  public void Reset()
83  {
84  _underlying.Reset();
85  }
86 
87  /// <summary>
88  /// Dispose the enumerator
89  /// </summary>
90  public void Dispose()
91  {
92  _underlying.Dispose();
93  }
94  }
95 }