Lean  $LEAN_TAG$
AuxiliaryDataEnumerator.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 QuantConnect.Util;
19 using QuantConnect.Data;
20 using System.Collections;
22 using System.Collections.Generic;
24 
26 {
27  /// <summary>
28  /// Auxiliary data enumerator that will, initialize and call the <see cref="ITradableDateEventProvider.GetEvents"/>
29  /// implementation each time there is a new tradable day for every <see cref="ITradableDateEventProvider"/>
30  /// provided.
31  /// </summary>
32  public class AuxiliaryDataEnumerator : IEnumerator<BaseData>
33  {
34  private readonly Queue<BaseData> _auxiliaryData;
35  private bool _initialized;
36  private DateTime _startTime;
37  private IMapFileProvider _mapFileProvider;
38  private IFactorFileProvider _factorFileProvider;
39  private ITradableDateEventProvider[] _tradableDateEventProviders;
40 
41  /// <summary>
42  /// The associated data configuration
43  /// </summary>
44  protected SubscriptionDataConfig Config { get; }
45 
46  /// <summary>
47  /// Creates a new instance
48  /// </summary>
49  /// <param name="config">The <see cref="SubscriptionDataConfig"/></param>
50  /// <param name="factorFileProvider">The factor file provider to use</param>
51  /// <param name="mapFileProvider">The <see cref="MapFile"/> provider to use</param>
52  /// <param name="tradableDateEventProviders">The tradable dates event providers</param>
53  /// <param name="tradableDayNotifier">Tradable dates provider</param>
54  /// <param name="startTime">Start date for the data request</param>
57  IFactorFileProvider factorFileProvider,
58  IMapFileProvider mapFileProvider,
59  ITradableDateEventProvider []tradableDateEventProviders,
60  ITradableDatesNotifier tradableDayNotifier,
61  DateTime startTime)
62  {
63  Config = config;
64  _startTime = startTime;
65  _mapFileProvider = mapFileProvider;
66  _auxiliaryData = new Queue<BaseData>();
67  _factorFileProvider = factorFileProvider;
68  _tradableDateEventProviders = tradableDateEventProviders;
69 
70  if (tradableDayNotifier != null)
71  {
72  tradableDayNotifier.NewTradableDate += NewTradableDate;
73  }
74  }
75 
76  /// <summary>
77  /// Advances the enumerator to the next element.
78  /// </summary>
79  /// <returns>Always true</returns>
80  public virtual bool MoveNext()
81  {
82  Current = _auxiliaryData.Count != 0 ? _auxiliaryData.Dequeue() : null;
83  return true;
84  }
85 
86  /// <summary>
87  /// Handle a new tradable date, drives the <see cref="ITradableDateEventProvider"/> instances
88  /// </summary>
89  protected void NewTradableDate(object sender, NewTradableDateEventArgs eventArgs)
90  {
91  Initialize();
92  for (var i = 0; i < _tradableDateEventProviders.Length; i++)
93  {
94  foreach (var newEvent in _tradableDateEventProviders[i].GetEvents(eventArgs))
95  {
96  _auxiliaryData.Enqueue(newEvent);
97  }
98  }
99  }
100 
101  /// <summary>
102  /// Initializes the underlying tradable data event providers
103  /// </summary>
104  protected void Initialize()
105  {
106  if (!_initialized)
107  {
108  _initialized = true;
109  // Late initialization so it is performed in the data feed stack
110  for (var i = 0; i < _tradableDateEventProviders.Length; i++)
111  {
112  _tradableDateEventProviders[i].Initialize(Config, _factorFileProvider, _mapFileProvider, _startTime);
113  }
114  }
115  }
116 
117  /// <summary>
118  /// Dispose of the Stream Reader and close out the source stream and file connections.
119  /// </summary>
120  public void Dispose()
121  {
122  for (var i = 0; i < _tradableDateEventProviders.Length; i++)
123  {
124  var disposable =_tradableDateEventProviders[i] as IDisposable;
125  disposable?.DisposeSafely();
126  }
127  }
128 
129  /// <summary>
130  /// Reset the IEnumeration
131  /// </summary>
132  /// <remarks>Not used</remarks>
133  public void Reset()
134  {
135  throw new NotImplementedException("Reset method not implemented. Assumes loop will only be used once.");
136  }
137 
138  object IEnumerator.Current => Current;
139 
140  /// <summary>
141  /// Last read BaseData object from this type and source
142  /// </summary>
143  public BaseData Current
144  {
145  get;
146  private set;
147  }
148  }
149 }