Lean  $LEAN_TAG$
DownloaderExtensions.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;
18 using System.Collections.Generic;
20 using NodaTime;
21 
22 namespace QuantConnect.Data
23 {
24  /// <summary>
25  /// Contains extension methods for the Downloader functionality.
26  /// </summary>
27  public static class DownloaderExtensions
28  {
29  /// <summary>
30  /// Get <see cref="DataDownloaderGetParameters"/> for all mapped <seealso cref="Symbol"/> with appropriate ticker name in specific date time range.
31  /// </summary>
32  /// <param name="dataDownloaderParameter">Generated class in "Lean.Engine.DataFeeds.DownloaderDataProvider"</param>
33  /// <param name="mapFileProvider">Provides instances of <see cref="MapFileResolver"/> at run time</param>
34  /// <param name="exchangeTimeZone">Provides the time zone this exchange</param>
35  /// <returns>
36  /// Return DataDownloaderGetParameters with different
37  /// <see cref="DataDownloaderGetParameters.StartUtc"/> - <seealso cref="DataDownloaderGetParameters.EndUtc"/> range
38  /// and <seealso cref="Symbol"/>
39  /// </returns>
40  /// <exception cref="ArgumentNullException">Thrown when <paramref name="dataDownloaderParameter"/> is null.</exception>
41  public static IEnumerable<DataDownloaderGetParameters> GetDataDownloaderParameterForAllMappedSymbols(
42  this DataDownloaderGetParameters dataDownloaderParameter,
43  IMapFileProvider mapFileProvider,
44  DateTimeZone exchangeTimeZone)
45  {
46  if (dataDownloaderParameter == null)
47  {
48  throw new ArgumentNullException(nameof(dataDownloaderParameter));
49  }
50 
51  if (dataDownloaderParameter.Symbol.SecurityType != SecurityType.Future
52  && dataDownloaderParameter.Symbol.RequiresMapping()
53  && dataDownloaderParameter.Resolution >= Resolution.Hour)
54  {
55  var yieldMappedSymbol = default(bool);
56  foreach (var symbolDateRange in mapFileProvider.RetrieveAllMappedSymbolInDateRange(dataDownloaderParameter.Symbol))
57  {
58  var startDateTimeUtc = symbolDateRange.StartDateTimeLocal.ConvertToUtc(exchangeTimeZone);
59  var endDateTimeUtc = symbolDateRange.EndDateTimeLocal.ConvertToUtc(exchangeTimeZone);
60 
61  // The first start date returns from mapFile like IPO (DateTime) and can not be greater then request StartTime
62  // The Downloader doesn't know start DateTime exactly, it always download all data, except for options and index options
63  if (dataDownloaderParameter.Symbol.SecurityType == SecurityType.Option ||
64  dataDownloaderParameter.Symbol.SecurityType == SecurityType.IndexOption)
65  {
66  // The symbol was delisted before the request start time
67  if (endDateTimeUtc < dataDownloaderParameter.StartUtc)
68  {
69  continue;
70  }
71 
72  if (startDateTimeUtc < dataDownloaderParameter.StartUtc)
73  {
74  startDateTimeUtc = dataDownloaderParameter.StartUtc;
75  }
76  }
77 
78  if (endDateTimeUtc > dataDownloaderParameter.EndUtc)
79  {
80  endDateTimeUtc = dataDownloaderParameter.EndUtc;
81  }
82 
83  yield return new DataDownloaderGetParameters(
84  symbolDateRange.Symbol, dataDownloaderParameter.Resolution, startDateTimeUtc, endDateTimeUtc, dataDownloaderParameter.TickType);
85  yieldMappedSymbol = true;
86  }
87 
88  if (!yieldMappedSymbol)
89  {
90  yield return dataDownloaderParameter;
91  }
92  }
93  else
94  {
95  yield return dataDownloaderParameter;
96  }
97  }
98  }
99 }