Lean  $LEAN_TAG$
DataDownloadConfig.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.Globalization;
18 using QuantConnect.Logging;
22 
24 {
25  /// <summary>
26  /// Represents the configuration for data download.
27  /// </summary>
28  public struct DataDownloadConfig
29  {
30  /// <summary>
31  /// Type of tick data to download.
32  /// </summary>
33  public TickType TickType { get; }
34 
35  /// <summary>
36  /// Type of security for which data is to be downloaded.
37  /// </summary>
38  public SecurityType SecurityType { get; }
39 
40  /// <summary>
41  /// Resolution of the downloaded data.
42  /// </summary>
43  public Resolution Resolution { get; }
44 
45  /// <summary>
46  /// Start date for the data download.
47  /// </summary>
48  public DateTime StartDate { get; }
49 
50  /// <summary>
51  /// End date for the data download.
52  /// </summary>
53  public DateTime EndDate { get; }
54 
55  /// <summary>
56  /// Market name for which the data is to be downloaded.
57  /// </summary>
58  public string MarketName { get; }
59 
60  /// <summary>
61  /// List of symbols for which data is to be downloaded.
62  /// </summary>
63  public List<Symbol> Symbols { get; } = new();
64 
65  /// <summary>
66  /// Initializes a new instance of the <see cref="DataDownloadConfig"/> struct.
67  /// </summary>
68  /// <param name="parameters">Dictionary containing the parameters for data download.</param>
70  {
71  TickType = ParseEnum<TickType>(Config.Get(DownloaderCommandArguments.CommandDataType).ToString());
72  SecurityType = ParseEnum<SecurityType>(Config.Get(DownloaderCommandArguments.CommandSecurityType).ToString());
73  Resolution = ParseEnum<Resolution>(Config.Get(DownloaderCommandArguments.CommandResolution).ToString());
74 
75  StartDate = DateTime.ParseExact(Config.Get(DownloaderCommandArguments.CommandStartDate).ToString(), DateFormat.EightCharacter, CultureInfo.InvariantCulture);
76  EndDate = DateTime.ParseExact(Config.Get(DownloaderCommandArguments.CommandEndDate).ToString(), DateFormat.EightCharacter, CultureInfo.InvariantCulture);
77 
78 #pragma warning disable CA1308 // class Market keeps all name in lowercase
79  MarketName = Config.Get(DownloaderCommandArguments.CommandMarketName).ToString().ToLower(CultureInfo.InvariantCulture);
80 #pragma warning restore CA1308
81 
82  if (string.IsNullOrEmpty(MarketName))
83  {
85  Log.Trace($"{nameof(DataDownloadConfig)}: Default market '{MarketName}' applied for SecurityType '{SecurityType}'");
86  }
87 
88  if (!Market.SupportedMarkets().Contains(MarketName))
89  {
90  throw new ArgumentException($"The specified market '{MarketName}' is not supported. Supported markets are: {string.Join(", ", Market.SupportedMarkets())}.");
91  }
92 
93  foreach (var ticker in (Config.GetValue<Dictionary<string, string>>(DownloaderCommandArguments.CommandTickers))!.Keys)
94  {
96  }
97  }
98 
99  /// <summary>
100  /// Initializes a new instance of the <see cref="DataDownloadConfig"/> class with the specified parameters.
101  /// </summary>
102  /// <param name="tickType">The type of tick data to be downloaded.</param>
103  /// <param name="securityType">The type of security for which data is being downloaded.</param>
104  /// <param name="resolution">The resolution of the data being downloaded.</param>
105  /// <param name="startDate">The start date for the data download range.</param>
106  /// <param name="endDate">The end date for the data download range.</param>
107  /// <param name="market">The name of the market from which the data is being downloaded.</param>
108  /// <param name="symbols">A list of symbols for which data is being downloaded.</param>
109  public DataDownloadConfig(TickType tickType, SecurityType securityType, Resolution resolution, DateTime startDate, DateTime endDate, string market, List<Symbol> symbols)
110  {
111  TickType = tickType;
112  SecurityType = securityType;
113  Resolution = resolution;
114  StartDate = startDate;
115  EndDate = endDate;
116  MarketName = market;
117  Symbols = symbols;
118  }
119 
120  /// <summary>
121  /// Returns a string representation of the <see cref="DataDownloadConfig"/> struct.
122  /// </summary>
123  /// <returns>A string representation of the <see cref="DataDownloadConfig"/> struct.</returns>
124  public override string ToString()
125  {
126  return $"TickType: {TickType}, " +
127  $"SecurityType: {SecurityType}, " +
128  $"Resolution: {Resolution}, " +
129  $"StartDate: {StartDate:yyyyMMdd}, " +
130  $"EndDate: {EndDate:yyyyMMdd}, " +
131  $"MarketName: {MarketName}, " +
132  $"Symbols: {string.Join(", ", Symbols.Select(s => s.ToString()))}";
133  }
134 
135  /// <summary>
136  /// Parses a string value to an enum of type <typeparamref name="TEnum"/>.
137  /// </summary>
138  /// <typeparam name="TEnum">The enum type to parse to.</typeparam>
139  /// <param name="value">The string value to parse.</param>
140  /// <returns>The parsed enum value.</returns>
141  private static TEnum ParseEnum<TEnum>(string value) where TEnum : struct, Enum
142  {
143  if (!Enum.TryParse(value, true, out TEnum result) || !Enum.IsDefined(typeof(TEnum), result))
144  {
145  throw new ArgumentException($"Invalid {typeof(TEnum).Name} specified. Please provide a valid {typeof(TEnum).Name}.");
146  }
147 
148  return result;
149  }
150  }
151 }