Files
Roman Yavnikov d1337eae2c Feat: CanonicalDataDownloaderDecorator for chain support (#9299)
* feat: add CanonicalDataDownloaderDecorator for chain support

Introduced CanonicalDataDownloaderDecorator to enable automatic resolution and parallel downloading of all contracts for canonical symbols (options and futures chains). Updated Program.cs and DownloaderDataProvider to use this decorator, ensuring seamless data retrieval for both canonical and non-canonical symbols. Refactored initialization logic to handle chain providers within the decorator and removed redundant code from Program.cs.

* feat: improve canonical symbol error handling in data downloader

* refactor: data downloader selection in DownloaderDataProvider

* refactor: CanonicalDataDownloaderDecorator construction

* feat: limit parallelism and improve contract data error handling

* refactor: canonical data downloader and provider usage

* remove: CanonicalNotSupportedException and refactor handling

* refactor: data downloader selection with selector class

Introduce DataDownloaderSelector to choose the correct IDataDownloader implementation based on data type, using CanonicalDataDownloaderDecorator only when needed. Update Program.cs and DownloaderDataProvider to use the selector, remove redundant initialization logic, and ensure proper resource disposal. This improves flexibility, correctness, and resource management for data downloads, especially for custom and canonical data types.

* fix: wrong selector condition

* refactor: change default log handler; add parallelism config to downloader

- Set ConsoleLogHandler as the default log handler in code and config
- Make contract download parallelism configurable via downloader-thread-count (default 4)
- Track and log number of processed contracts in CanonicalDataDownloaderDecorator
- Add error logging for missing universe data
- Improve logging clarity and code readability

* feat: prevent duplicate contract downloads in canonical chains

Introduce ContractDownloadParameters to uniquely identify contract/tick type/resolution combinations and cache them in CanonicalDataDownloaderDecorator, avoiding redundant downloads when contracts are shared across canonical symbol chains. Add date range optimization for contract downloads and unit tests for the new class.

* Revert "feat: prevent duplicate contract downloads in canonical chains"

This reverts commit 44386c4c788904531c473c60c2a173e2354561ec.

* feat: clamp contract date ranges, deduplicate downloads

Added AdjustDateRangeForContract to CanonicalDataDownloaderDecorator to clamp start/end dates based on contract expiry and security type. Introduced _contractsCache to prevent duplicate downloads across canonical chains and _processedContracts for logging. Updated contract retrieval logic for deduplication. Added NUnit tests to verify date range adjustments for futures and options.

* refactor: canonical contract date range adjustment logic

* test:feat: add test for non-option/future contract date adjustment

* feat: make look-back periods for canonical symbols configurable

* refactor: remove Lazy from CanonicalDataDownloaderDecorator

Revert "refactor: remove Lazy from CanonicalDataDownloaderDecorator"

This reverts commit 909c1299406688a19352273f8d0b5b3ea99c0fb9.

refactor: Inject IMapFileProvider into downloader selectors

Revert "refactor: Inject IMapFileProvider into downloader selectors"

This reverts commit 0add95269809680e425bfa6080e465bf16377e56.

Reapply "refactor: remove Lazy from CanonicalDataDownloaderDecorator"

This reverts commit 9e3c4bed078868a3c8dfedcab0e52d6dced1daad.

* refactor: data downloader dependency injection

* refactor: provider initialization in data downloader classes

* refactor: DataDownloaderSelector dependencies, add tests

- Require explicit IDataProvider in DataDownloaderSelector constructor, removing default and fallback logic
- Change default log handler to ConsoleLogHandler in Program.cs
- Pass DownloaderDataProvider as IDataProvider to DataDownloaderSelector
- Add DataDownloaderSelector unit tests to verify correct downloader selection and decorator usage

* feat: ensure factorFileProvider is initialized when defaulted

* fix: missed dataProvider in DataDownloaderSelector ctor

* refactor: improve IFactorFileProvider resolution in DataDownloaderSelector
2026-02-23 15:58:15 +02:00

224 lines
11 KiB
C#

/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using NodaTime;
using QuantConnect.Util;
using QuantConnect.Data;
using QuantConnect.Logging;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using QuantConnect.Configuration;
using QuantConnect.Data.UniverseSelection;
using DataFeeds = QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Lean.Engine.DataFeeds.DataDownloader;
using QuantConnect.DownloaderDataProvider.Launcher.Models;
using QuantConnect.DownloaderDataProvider.Launcher.Models.Constants;
namespace QuantConnect.DownloaderDataProvider.Launcher;
public static class Program
{
/// <summary>
/// Synchronizer in charge of guaranteeing a single operation per file path
/// </summary>
private readonly static KeyStringSynchronizer DiskSynchronizer = new();
/// <summary>
/// The provider used to cache history data files
/// </summary>
private static readonly IDataCacheProvider _dataCacheProvider = new DiskDataCacheProvider(DiskSynchronizer);
/// <summary>
/// Represents the time interval of 5 seconds.
/// </summary>
private static TimeSpan _logDisplayInterval = TimeSpan.FromSeconds(5);
/// <summary>
/// Provides access to exchange hours and raw data times zones in various markets
/// </summary>
private static readonly MarketHoursDatabase _marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
/// <summary>
/// The main entry point for the application.
/// </summary>
/// <param name="args">Command-line arguments passed to the application.</param>
public static void Main(string[] args)
{
// Parse report arguments and merge with config to use in the optimizer
if (args.Length > 0)
{
Config.MergeCommandLineArgumentsWithConfiguration(DownloaderDataProviderArgumentParser.ParseArguments(args));
}
var dataDownloaderSelector = InitializeConfigurations();
var commandDataType = Config.Get(DownloaderCommandArguments.CommandDataType).ToUpperInvariant();
var dataDownloadConfig = default(BaseDataDownloadConfig);
switch (commandDataType)
{
case "UNIVERSE":
dataDownloadConfig = new DataUniverseDownloadConfig();
RunUniverseDownloader(dataDownloaderSelector.GetDataDownloader(dataDownloadConfig.DataType), dataDownloadConfig);
break;
case "TRADE":
case "QUOTE":
case "OPENINTEREST":
dataDownloadConfig = new DataDownloadConfig();
RunDownload(dataDownloaderSelector.GetDataDownloader(dataDownloadConfig.DataType), dataDownloadConfig, Globals.DataFolder, _dataCacheProvider);
break;
default:
Log.Error($"QuantConnect.DownloaderDataProvider.Launcher: Unsupported command data type '{commandDataType}'. Valid options: UNIVERSE, TRADE, QUOTE, OPENINTEREST.");
break;
}
dataDownloaderSelector.Dispose();
}
/// <summary>
/// Executes a data download operation using the specified data downloader.
/// </summary>
/// <param name="dataDownloader">An instance of an object implementing the <see cref="IDataDownloader"/> interface, responsible for downloading data.</param>
/// <param name="dataDownloadConfig">Configuration settings for the data download operation.</param>
/// <param name="dataDirectory">The directory where the downloaded data will be stored.</param>
/// <param name="dataCacheProvider">The provider used to cache history data files</param>
/// <param name="mapSymbol">True if the symbol should be mapped while writing the data</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="dataDownloader"/> is null.</exception>
public static void RunDownload(IDataDownloader dataDownloader, BaseDataDownloadConfig dataDownloadConfig, string dataDirectory, IDataCacheProvider dataCacheProvider, bool mapSymbol = true)
{
if (dataDownloader == null)
{
throw new ArgumentNullException(nameof(dataDownloader), "The data downloader instance cannot be null. Please ensure that a valid instance of data downloader is provided.");
}
var totalDownloadSymbols = dataDownloadConfig.Symbols.Count;
var completeSymbolCount = 0;
var startDownloadUtcTime = DateTime.UtcNow;
foreach (var symbol in dataDownloadConfig.Symbols)
{
var downloadParameters = new DataDownloaderGetParameters(symbol, dataDownloadConfig.Resolution, dataDownloadConfig.StartDate, dataDownloadConfig.EndDate, dataDownloadConfig.TickType);
Log.Trace($"DownloaderDataProvider.Main(): Starting download {downloadParameters}");
var downloadedData = dataDownloader.Get(downloadParameters);
if (downloadedData == null)
{
completeSymbolCount++;
Log.Trace($"DownloaderDataProvider.Main(): No data available for the following parameters: {downloadParameters}");
continue;
}
var (dataTimeZone, exchangeTimeZone) = GetDataAndExchangeTimeZoneBySymbol(symbol);
var writer = new LeanDataWriter(dataDownloadConfig.Resolution, symbol, dataDirectory, dataDownloadConfig.TickType, dataCacheProvider, mapSymbol: mapSymbol);
var groupedData = DataFeeds.DownloaderDataProvider.FilterAndGroupDownloadDataBySymbol(
downloadedData,
symbol,
dataDownloadConfig.DataType,
exchangeTimeZone,
dataTimeZone,
downloadParameters.StartUtc,
downloadParameters.EndUtc);
var lastLogStatusTime = DateTime.UtcNow;
foreach (var data in groupedData)
{
writer.Write(data.Select(data =>
{
var utcNow = DateTime.UtcNow;
if (utcNow - lastLogStatusTime >= _logDisplayInterval)
{
lastLogStatusTime = utcNow;
Log.Trace($"Downloading data for {downloadParameters.Symbol}. Please hold on...");
}
return data;
}));
}
completeSymbolCount++;
var symbolPercentComplete = (double)completeSymbolCount / totalDownloadSymbols * 100;
Log.Trace($"DownloaderDataProvider.RunDownload(): {symbolPercentComplete:F2}% complete ({completeSymbolCount} out of {totalDownloadSymbols} symbols)");
Log.Trace($"DownloaderDataProvider.RunDownload(): Download completed for {downloadParameters.Symbol} at {downloadParameters.Resolution} resolution, " +
$"covering the period from {dataDownloadConfig.StartDate} to {dataDownloadConfig.EndDate}.");
}
Log.Trace($"All downloads completed in {(DateTime.UtcNow - startDownloadUtcTime).TotalSeconds:F2} seconds.");
}
/// <summary>
/// Initiates the universe downloader using the provided configuration.
/// </summary>
/// <param name="dataDownloader">The data downloader instance.</param>
/// <param name="dataUniverseDownloadConfig">The universe download configuration.</param>
private static void RunUniverseDownloader(IDataDownloader dataDownloader, BaseDataDownloadConfig dataUniverseDownloadConfig)
{
foreach (var symbol in dataUniverseDownloadConfig.Symbols)
{
var universeDownloadParameters = new DataUniverseDownloaderGetParameters(symbol, dataUniverseDownloadConfig.StartDate, dataUniverseDownloadConfig.EndDate);
UniverseExtensions.RunUniverseDownloader(dataDownloader, universeDownloadParameters);
}
}
/// <summary>
/// Retrieves the data time zone and exchange time zone associated with the specified symbol.
/// </summary>
/// <param name="symbol">The symbol for which to retrieve time zones.</param>
/// <returns>
/// A tuple containing the data time zone and exchange time zone.
/// The data time zone represents the time zone for data related to the symbol.
/// The exchange time zone represents the time zone for trading activities related to the symbol.
/// </returns>
private static (DateTimeZone dataTimeZone, DateTimeZone exchangeTimeZone) GetDataAndExchangeTimeZoneBySymbol(Symbol symbol)
{
var entry = _marketHoursDatabase.GetEntry(symbol.ID.Market, symbol, symbol.SecurityType);
return (entry.DataTimeZone, entry.ExchangeHours.TimeZone);
}
/// <summary>
/// Initializes various configurations for the application.
/// This method sets up logging, data providers, map file providers, and factor file providers.
/// </summary>
/// <remarks>
/// The method reads configuration values to determine whether debugging is enabled,
/// which log handler to use, and which data, map file, and factor file providers to initialize.
/// </remarks>
/// <seealso cref="Log"/>
/// <seealso cref="Config"/>
/// <seealso cref="Composer"/>
/// <seealso cref="ILogHandler"/>
/// <seealso cref="IDataProvider"/>
/// <seealso cref="IMapFileProvider"/>
/// <seealso cref="IFactorFileProvider"/>
public static DataDownloaderSelector InitializeConfigurations()
{
Log.DebuggingEnabled = Config.GetBool("debug-mode", false);
Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "ConsoleLogHandler"));
var dataProvider = Composer.Instance.GetExportedValueByTypeName<IDataProvider>("DefaultDataProvider");
var mapFileProvider = Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(Config.Get("map-file-provider", "LocalDiskMapFileProvider"));
var factorFileProvider = Composer.Instance.GetExportedValueByTypeName<IFactorFileProvider>(Config.Get("factor-file-provider", "LocalDiskFactorFileProvider"));
mapFileProvider.Initialize(dataProvider);
factorFileProvider.Initialize(mapFileProvider, dataProvider);
var dataDownloader = Composer.Instance.GetExportedValueByTypeName<IDataDownloader>(Config.Get(DownloaderCommandArguments.CommandDownloaderDataDownloader));
return new DataDownloaderSelector(dataDownloader, mapFileProvider, dataProvider, factorFileProvider);
}
}