Lean  $LEAN_TAG$
BaseDataCollectionSubscriptionEnumeratorFactory.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.Data;
20 using System.Collections.Generic;
22 
24 {
25  /// <summary>
26  /// Provides an implementation of <see cref="ISubscriptionEnumeratorFactory"/> that reads
27  /// an entire <see cref="SubscriptionDataSource"/> into a single <see cref="BaseDataCollection"/>
28  /// to be emitted on the tradable date at midnight
29  /// </summary>
30  /// <remarks>This enumerator factory is currently only used in backtesting with coarse data</remarks>
32  {
33  private IObjectStore _objectStore;
34 
35  /// <summary>
36  /// Instanciates a new <see cref="BaseDataCollectionSubscriptionEnumeratorFactory"/>
37  /// </summary>
38  /// <param name="objectStore">The object store to use</param>
40  {
41  _objectStore = objectStore;
42  }
43 
44  /// <summary>
45  /// Creates an enumerator to read the specified request
46  /// </summary>
47  /// <param name="request">The subscription request to be read</param>
48  /// <param name="dataProvider">Provider used to get data when it is not present on disk</param>
49  /// <returns>An enumerator reading the subscription request</returns>
50  public IEnumerator<BaseData> CreateEnumerator(SubscriptionRequest request, IDataProvider dataProvider)
51  {
52  using (var dataCacheProvider = new SingleEntryDataCacheProvider(dataProvider))
53  {
54  var configuration = request.Configuration;
55  var sourceFactory = (BaseData)Activator.CreateInstance(request.Configuration.Type);
56 
57  // Behaves in the same way as in live trading
58  // (i.e. only emit coarse data on dates following a trading day)
59  // The shifting of dates is needed to ensure we never emit coarse data on the same date,
60  // because it would enable look-ahead bias.
61 
62  foreach (var date in request.TradableDaysInDataTimeZone)
63  {
64  var source = sourceFactory.GetSource(configuration, date, false);
65  var factory = SubscriptionDataSourceReader.ForSource(source, dataCacheProvider, configuration, date, false, sourceFactory,
66  dataProvider, _objectStore);
67  var coarseFundamentalForDate = factory.Read(source);
68  // shift all date of emitting the file forward one day to model emitting coarse midnight the next day.
69  yield return new BaseDataCollection(date.AddDays(1), configuration.Symbol, coarseFundamentalForDate);
70  }
71  }
72  }
73  }
74 }