Skip to content

Commit d1337ea

Browse files
authored
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 44386c4. * 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 909c129. refactor: Inject IMapFileProvider into downloader selectors Revert "refactor: Inject IMapFileProvider into downloader selectors" This reverts commit 0add952. Reapply "refactor: remove Lazy from CanonicalDataDownloaderDecorator" This reverts commit 9e3c4be. * 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
1 parent 7333dd3 commit d1337ea

7 files changed

Lines changed: 532 additions & 32 deletions

File tree

DownloaderDataProvider/Program.cs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
using QuantConnect.Interfaces;
2222
using QuantConnect.Securities;
2323
using QuantConnect.Configuration;
24-
using QuantConnect.Lean.Engine.DataFeeds;
2524
using QuantConnect.Data.UniverseSelection;
2625
using DataFeeds = QuantConnect.Lean.Engine.DataFeeds;
26+
using QuantConnect.Lean.Engine.DataFeeds.DataDownloader;
2727
using QuantConnect.DownloaderDataProvider.Launcher.Models;
2828
using QuantConnect.DownloaderDataProvider.Launcher.Models.Constants;
29-
using QuantConnect.Lean.Engine.HistoricalData;
3029

3130
namespace QuantConnect.DownloaderDataProvider.Launcher;
31+
3232
public static class Program
3333
{
3434
/// <summary>
@@ -63,30 +63,29 @@ public static void Main(string[] args)
6363
Config.MergeCommandLineArgumentsWithConfiguration(DownloaderDataProviderArgumentParser.ParseArguments(args));
6464
}
6565

66-
InitializeConfigurations();
66+
var dataDownloaderSelector = InitializeConfigurations();
6767

68-
var dataDownloader = Composer.Instance.GetExportedValueByTypeName<IDataDownloader>(Config.Get(DownloaderCommandArguments.CommandDownloaderDataDownloader));
6968
var commandDataType = Config.Get(DownloaderCommandArguments.CommandDataType).ToUpperInvariant();
7069

70+
var dataDownloadConfig = default(BaseDataDownloadConfig);
7171
switch (commandDataType)
7272
{
7373
case "UNIVERSE":
74-
RunUniverseDownloader(dataDownloader, new DataUniverseDownloadConfig());
74+
dataDownloadConfig = new DataUniverseDownloadConfig();
75+
RunUniverseDownloader(dataDownloaderSelector.GetDataDownloader(dataDownloadConfig.DataType), dataDownloadConfig);
7576
break;
7677
case "TRADE":
7778
case "QUOTE":
7879
case "OPENINTEREST":
79-
RunDownload(dataDownloader, new DataDownloadConfig(), Globals.DataFolder, _dataCacheProvider);
80+
dataDownloadConfig = new DataDownloadConfig();
81+
RunDownload(dataDownloaderSelector.GetDataDownloader(dataDownloadConfig.DataType), dataDownloadConfig, Globals.DataFolder, _dataCacheProvider);
8082
break;
8183
default:
8284
Log.Error($"QuantConnect.DownloaderDataProvider.Launcher: Unsupported command data type '{commandDataType}'. Valid options: UNIVERSE, TRADE, QUOTE, OPENINTEREST.");
8385
break;
8486
}
8587

86-
if (dataDownloader is BrokerageDataDownloader brokerageDataDownloader)
87-
{
88-
brokerageDataDownloader.DisposeSafely();
89-
}
88+
dataDownloaderSelector.Dispose();
9089
}
9190

9291
/// <summary>
@@ -98,7 +97,7 @@ public static void Main(string[] args)
9897
/// <param name="dataCacheProvider">The provider used to cache history data files</param>
9998
/// <param name="mapSymbol">True if the symbol should be mapped while writing the data</param>
10099
/// <exception cref="ArgumentNullException">Thrown when <paramref name="dataDownloader"/> is null.</exception>
101-
public static void RunDownload(IDataDownloader dataDownloader, DataDownloadConfig dataDownloadConfig, string dataDirectory, IDataCacheProvider dataCacheProvider, bool mapSymbol = true)
100+
public static void RunDownload(IDataDownloader dataDownloader, BaseDataDownloadConfig dataDownloadConfig, string dataDirectory, IDataCacheProvider dataCacheProvider, bool mapSymbol = true)
102101
{
103102
if (dataDownloader == null)
104103
{
@@ -167,7 +166,7 @@ public static void RunDownload(IDataDownloader dataDownloader, DataDownloadConfi
167166
/// </summary>
168167
/// <param name="dataDownloader">The data downloader instance.</param>
169168
/// <param name="dataUniverseDownloadConfig">The universe download configuration.</param>
170-
private static void RunUniverseDownloader(IDataDownloader dataDownloader, DataUniverseDownloadConfig dataUniverseDownloadConfig)
169+
private static void RunUniverseDownloader(IDataDownloader dataDownloader, BaseDataDownloadConfig dataUniverseDownloadConfig)
171170
{
172171
foreach (var symbol in dataUniverseDownloadConfig.Symbols)
173172
{
@@ -206,28 +205,19 @@ private static (DateTimeZone dataTimeZone, DateTimeZone exchangeTimeZone) GetDat
206205
/// <seealso cref="IDataProvider"/>
207206
/// <seealso cref="IMapFileProvider"/>
208207
/// <seealso cref="IFactorFileProvider"/>
209-
public static void InitializeConfigurations()
208+
public static DataDownloaderSelector InitializeConfigurations()
210209
{
211210
Log.DebuggingEnabled = Config.GetBool("debug-mode", false);
212-
Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));
211+
Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "ConsoleLogHandler"));
213212

214213
var dataProvider = Composer.Instance.GetExportedValueByTypeName<IDataProvider>("DefaultDataProvider");
215214
var mapFileProvider = Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(Config.Get("map-file-provider", "LocalDiskMapFileProvider"));
216215
var factorFileProvider = Composer.Instance.GetExportedValueByTypeName<IFactorFileProvider>(Config.Get("factor-file-provider", "LocalDiskFactorFileProvider"));
217216

218-
var optionChainProvider = Composer.Instance.GetPart<IOptionChainProvider>();
219-
if (optionChainProvider == null)
220-
{
221-
var historyManager = Composer.Instance.GetExportedValueByTypeName<HistoryProviderManager>(nameof(HistoryProviderManager));
222-
historyManager.Initialize(new HistoryProviderInitializeParameters(null, null, dataProvider, _dataCacheProvider,
223-
mapFileProvider, factorFileProvider, _ => { }, false, new DataPermissionManager(), null, new AlgorithmSettings()));
224-
var baseOptionChainProvider = new LiveOptionChainProvider();
225-
baseOptionChainProvider.Initialize(new(mapFileProvider, historyManager));
226-
optionChainProvider = new CachingOptionChainProvider(baseOptionChainProvider);
227-
Composer.Instance.AddPart(optionChainProvider);
228-
}
229-
230217
mapFileProvider.Initialize(dataProvider);
231218
factorFileProvider.Initialize(mapFileProvider, dataProvider);
219+
220+
var dataDownloader = Composer.Instance.GetExportedValueByTypeName<IDataDownloader>(Config.Get(DownloaderCommandArguments.CommandDownloaderDataDownloader));
221+
return new DataDownloaderSelector(dataDownloader, mapFileProvider, dataProvider, factorFileProvider);
232222
}
233223
}

DownloaderDataProvider/config.example.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"map-file-provider": "LocalDiskMapFileProvider",
33
"data-provider": "DefaultDataProvider",
4-
"log-handler": "CompositeLogHandler",
4+
"log-handler": "ConsoleLogHandler",
55
"factor-file-provider": "LocalDiskFactorFileProvider",
66

77
// The root directory of the data folder for this application

0 commit comments

Comments
 (0)