Lean  $LEAN_TAG$
DataUniverseDownloaderGetParameters.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 System.Linq;
18 using QuantConnect.Util;
20 using System.Collections.Generic;
22 
23 namespace QuantConnect;
24 
25 /// <summary>
26 /// Represents the parameters required for downloading universe data.
27 /// </summary>
28 public sealed class DataUniverseDownloaderGetParameters : DataDownloaderGetParameters
29 {
30  /// <summary>
31  /// The initialized instance of the security exchange hours.
32  /// </summary>
33  private readonly SecurityExchangeHours _securityExchangeHours;
34 
35  /// <summary>
36  /// The tick types supported for universe data.
37  /// </summary>
38  private readonly TickType[] UniverseTickTypes = { TickType.Quote, TickType.Trade, TickType.OpenInterest };
39 
40  /// <summary>
41  /// Gets the underlying symbol associated with the universe.
42  /// </summary>
43  public Symbol UnderlyingSymbol { get => Symbol.HasUnderlying ? Symbol.Underlying : Symbol.Empty; }
44 
45  /// <summary>
46  /// Initializes a new instance of the <see cref="DataUniverseDownloaderGetParameters"/> class.
47  /// </summary>
48  /// <param name="canonicalSymbol">The canonical symbol for the data request.</param>
49  /// <param name="startDate">The start date for the data request.</param>
50  /// <param name="endDate">The end date for the data request.</param>
51  /// <param name="securityExchangeHours">The security exchange hours for this symbol</param>
52  /// <exception cref="ArgumentException">Thrown when the provided symbol is not canonical.</exception>
53  public DataUniverseDownloaderGetParameters(Symbol canonicalSymbol, DateTime startDate, DateTime endDate, SecurityExchangeHours securityExchangeHours = default)
54  : base(
55  canonicalSymbol.IsCanonical() ? canonicalSymbol : throw new ArgumentException("DataUniverseDownloaderGetParameters: Symbol must be canonical.", nameof(canonicalSymbol)),
56  Resolution.Daily,
57  startDate,
58  endDate)
59  {
60  _securityExchangeHours = securityExchangeHours ?? MarketHoursDatabase.FromDataFolder().GetExchangeHours(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType);
61 
62  EndUtc = EndUtc.ConvertToUtc(_securityExchangeHours.TimeZone);
63  StartUtc = StartUtc.ConvertToUtc(_securityExchangeHours.TimeZone);
64  }
65 
66  /// <summary>
67  /// Gets the file name where the universe data will be saved.
68  /// </summary>
69  /// <param name="processingDate">The date for which the file name is generated.</param>
70  /// <returns>The universe file name.</returns>
71  public string GetUniverseFileName(DateTime processingDate)
72  {
73  return BaseChainUniverseData.GetUniverseFullFilePath(Symbol, processingDate);
74  }
75 
76  /// <summary>
77  /// Creates data download parameters for each day in the range.
78  /// </summary>
79  public IEnumerable<(DateTime, IEnumerable<DataDownloaderGetParameters>)> CreateDataDownloaderGetParameters()
80  {
81  foreach (var processingDate in Time.EachTradeableDay(_securityExchangeHours, StartUtc, EndUtc))
82  {
83  var processingDateUtc = processingDate.ConvertToUtc(_securityExchangeHours.TimeZone);
84 
85  var requests = new List<DataDownloaderGetParameters>(3);
86 
87  if (UnderlyingSymbol != Symbol.Empty)
88  {
89  requests.Add(new(UnderlyingSymbol, Resolution, processingDateUtc, processingDateUtc.AddDays(1), TickType.Trade));
90  }
91 
92  requests.AddRange(UniverseTickTypes.Select(tickType => new DataDownloaderGetParameters(Symbol, Resolution, processingDateUtc, processingDateUtc.AddDays(1), tickType)));
93 
94  yield return (processingDate, requests);
95  }
96  }
97 }