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