44 private static TimeSpan _logDisplayInterval = TimeSpan.FromSeconds(5);
55 public static void Main(
string[] args)
67 var dataDownloadConfig =
new DataDownloadConfig();
69 RunDownload(dataDownloader, dataDownloadConfig, Globals.DataFolder, _dataCacheProvider);
81 public static void RunDownload(IDataDownloader dataDownloader, DataDownloadConfig dataDownloadConfig,
string dataDirectory,
IDataCacheProvider dataCacheProvider,
bool mapSymbol =
true)
83 if (dataDownloader ==
null)
85 throw new ArgumentNullException(nameof(dataDownloader),
"The data downloader instance cannot be null. Please ensure that a valid instance of data downloader is provided.");
88 var totalDownloadSymbols = dataDownloadConfig.Symbols.Count;
89 var completeSymbolCount = 0;
90 var startDownloadUtcTime = DateTime.UtcNow;
92 foreach (var symbol
in dataDownloadConfig.Symbols)
94 var downloadParameters =
new DataDownloaderGetParameters(symbol, dataDownloadConfig.Resolution, dataDownloadConfig.StartDate, dataDownloadConfig.EndDate, dataDownloadConfig.TickType);
96 Log.
Trace($
"DownloaderDataProvider.Main(): Starting download {downloadParameters}");
97 var downloadedData = dataDownloader.Get(downloadParameters);
99 if (downloadedData ==
null)
101 completeSymbolCount++;
102 Log.
Trace($
"DownloaderDataProvider.Main(): No data available for the following parameters: {downloadParameters}");
106 var (dataTimeZone, exchangeTimeZone) = GetDataAndExchangeTimeZoneBySymbol(symbol);
108 var writer =
new LeanDataWriter(dataDownloadConfig.Resolution, symbol, dataDirectory, dataDownloadConfig.TickType, dataCacheProvider, mapSymbol: mapSymbol);
110 var groupedData = DataFeeds.DownloaderDataProvider.FilterAndGroupDownloadDataBySymbol(
116 downloadParameters.StartUtc,
117 downloadParameters.EndUtc);
119 var lastLogStatusTime = DateTime.UtcNow;
121 foreach (var data
in groupedData)
123 writer.Write(data.Select(data =>
125 var utcNow = DateTime.UtcNow;
126 if (utcNow - lastLogStatusTime >= _logDisplayInterval)
128 lastLogStatusTime = utcNow;
129 Log.Trace($
"Downloading data for {downloadParameters.Symbol}. Please hold on...");
135 completeSymbolCount++;
136 var symbolPercentComplete = (double)completeSymbolCount / totalDownloadSymbols * 100;
137 Log.
Trace($
"DownloaderDataProvider.RunDownload(): {symbolPercentComplete:F2}% complete ({completeSymbolCount} out of {totalDownloadSymbols} symbols)");
139 Log.
Trace($
"DownloaderDataProvider.RunDownload(): Download completed for {downloadParameters.Symbol} at {downloadParameters.Resolution} resolution, " +
140 $
"covering the period from {dataDownloadConfig.StartDate} to {dataDownloadConfig.EndDate}.");
142 Log.
Trace($
"All downloads completed in {(DateTime.UtcNow - startDownloadUtcTime).TotalSeconds:F2} seconds.");
154 private static (DateTimeZone dataTimeZone, DateTimeZone exchangeTimeZone) GetDataAndExchangeTimeZoneBySymbol(Symbol symbol)
156 var entry = _marketHoursDatabase.GetEntry(symbol.ID.Market, symbol, symbol.SecurityType);
157 return (entry.DataTimeZone, entry.ExchangeHours.TimeZone);
185 if (optionChainProvider ==
null)
191 mapFileProvider.Initialize(dataProvider);
192 factorFileProvider.Initialize(mapFileProvider, dataProvider);