Lean  $LEAN_TAG$
CorporateEventEnumeratorFactory.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 System.Collections.Generic;
19 
20 using QuantConnect.Data;
23 
25 {
26  /// <summary>
27  /// Helper class used to create the corporate event providers
28  /// <see cref="MappingEventProvider"/>, <see cref="SplitEventProvider"/>,
29  /// <see cref="DividendEventProvider"/>, <see cref="DelistingEventProvider"/>
30  /// </summary>
31  public static class CorporateEventEnumeratorFactory
32  {
33  /// <summary>
34  /// Creates a new <see cref="AuxiliaryDataEnumerator"/> that will hold the
35  /// corporate event providers
36  /// </summary>
37  /// <param name="rawDataEnumerator">The underlying raw data enumerator</param>
38  /// <param name="config">The <see cref="SubscriptionDataConfig"/></param>
39  /// <param name="factorFileProvider">Used for getting factor files</param>
40  /// <param name="tradableDayNotifier">Tradable dates provider</param>
41  /// <param name="mapFileProvider">The <see cref="MapFile"/> provider to use</param>
42  /// <param name="startTime">Start date for the data request</param>
43  /// <param name="endTime">
44  /// End date for the data request.
45  /// This will be used for <see cref="DataNormalizationMode.ScaledRaw"/> data normalization mode to adjust prices to the given end date
46  /// </param>
47  /// <param name="enablePriceScaling">Applies price factor</param>
48  /// <returns>The new auxiliary data enumerator</returns>
49  public static IEnumerator<BaseData> CreateEnumerators(
50  IEnumerator<BaseData> rawDataEnumerator,
52  IFactorFileProvider factorFileProvider,
53  ITradableDatesNotifier tradableDayNotifier,
54  IMapFileProvider mapFileProvider,
55  DateTime startTime,
56  DateTime endTime,
57  bool enablePriceScaling = true)
58  {
59 
60  var tradableEventProviders = new List<ITradableDateEventProvider>();
61 
62  if (config.EmitSplitsAndDividends())
63  {
64  tradableEventProviders.Add(new SplitEventProvider());
65  tradableEventProviders.Add(new DividendEventProvider());
66  }
67 
68  if (config.TickerShouldBeMapped())
69  {
70  tradableEventProviders.Add(new MappingEventProvider());
71  }
72 
73  tradableEventProviders.Add(new DelistingEventProvider());
74 
75  var enumerator = new AuxiliaryDataEnumerator(
76  config,
77  factorFileProvider,
78  mapFileProvider,
79  tradableEventProviders.ToArray(),
80  tradableDayNotifier,
81  startTime);
82 
83  // avoid price scaling for backtesting; calculate it directly in worker
84  // and allow subscription to extract the the data depending on config data mode
85  var dataEnumerator = rawDataEnumerator;
86  if (enablePriceScaling && config.PricesShouldBeScaled())
87  {
88  dataEnumerator = new PriceScaleFactorEnumerator(
89  rawDataEnumerator,
90  config,
91  factorFileProvider,
92  endDate: endTime);
93  }
94 
95  return new SynchronizingBaseDataEnumerator(dataEnumerator, enumerator);
96  }
97  }
98 }