Lean  $LEAN_TAG$
LiveAuxiliaryDataEnumerator.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;
20 using System.Collections.Generic;
22 
24 {
25  /// <summary>
26  /// Auxiliary data enumerator that will trigger new tradable dates event accordingly
27  /// </summary>
29  {
30  private DateTime _lastTime;
31  private ITimeProvider _timeProvider;
32  private SecurityCache _securityCache;
33 
34  /// <summary>
35  /// Creates a new instance
36  /// </summary>
37  /// <param name="config">The <see cref="SubscriptionDataConfig"/></param>
38  /// <param name="factorFileProvider">The factor file provider to use</param>
39  /// <param name="mapFileProvider">The <see cref="MapFile"/> provider to use</param>
40  /// <param name="tradableDateEventProviders">The tradable dates event providers</param>
41  /// <param name="startTime">Start date for the data request</param>
42  /// <param name="timeProvider">The time provider to use</param>
43  /// <param name="securityCache">The security cache</param>
45  IMapFileProvider mapFileProvider, ITradableDateEventProvider[] tradableDateEventProviders,
46  DateTime startTime,
47  ITimeProvider timeProvider,
48  SecurityCache securityCache)
49  // tradableDayNotifier: null -> we are going to trigger the new tradables events for the base implementation
50  : base(config, factorFileProvider, mapFileProvider, tradableDateEventProviders, tradableDayNotifier:null, startTime)
51  {
52  _securityCache = securityCache;
53  _timeProvider = timeProvider;
54 
55  // initialize providers right away so mapping happens before we subscribe
56  Initialize();
57  }
58 
59  /// <summary>
60  /// Moves the LiveAuxiliaryDataEnumerator to the next item
61  /// </summary>
62  public override bool MoveNext()
63  {
64  var currentDate = _timeProvider.GetUtcNow().ConvertFromUtc(Config.ExchangeTimeZone).Add(-Time.LiveAuxiliaryDataOffset).Date;
65  if (currentDate != _lastTime)
66  {
67  // when the date changes for the security we trigger a new tradable date event
68  var newDayEvent = new NewTradableDateEventArgs(currentDate, _securityCache.GetData(), Config.Symbol, null);
69 
70  NewTradableDate(this, newDayEvent);
71  // update last time
72  _lastTime = currentDate;
73  }
74 
75  return base.MoveNext();
76  }
77 
78  /// <summary>
79  /// Helper method to create a new instance.
80  /// Knows which security types should create one and determines the appropriate delisting event provider to use
81  /// </summary>
82  public static bool TryCreate(SubscriptionDataConfig dataConfig, ITimeProvider timeProvider,
83  SecurityCache securityCache, IMapFileProvider mapFileProvider, IFactorFileProvider fileProvider, DateTime startTime,
84  out IEnumerator<BaseData> enumerator)
85  {
86  enumerator = null;
87  var securityType = dataConfig.SecurityType;
88  if (securityType.IsOption() || securityType == SecurityType.Future || securityType == SecurityType.Equity)
89  {
90  var providers = new List<ITradableDateEventProvider>
91  {
92  securityType == SecurityType.Equity
95  };
96 
97  if (dataConfig.TickerShouldBeMapped())
98  {
99  providers.Add(new LiveMappingEventProvider());
100  }
101 
102  if (dataConfig.EmitSplitsAndDividends())
103  {
104  providers.Add(new LiveDividendEventProvider());
105  providers.Add(new LiveSplitEventProvider());
106  }
107 
108  enumerator = new LiveAuxiliaryDataEnumerator(dataConfig, fileProvider, mapFileProvider,
109  providers.ToArray(), startTime, timeProvider, securityCache);
110  }
111  return enumerator != null;
112  }
113  }
114 }