/*
* 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 System;
using System.Linq;
using QuantConnect.Util;
using QuantConnect.Securities;
using System.Collections.Generic;
using QuantConnect.Data.UniverseSelection;
namespace QuantConnect;
///
/// Represents the parameters required for downloading universe data.
///
public sealed class DataUniverseDownloaderGetParameters : DataDownloaderGetParameters
{
///
/// The initialized instance of the security exchange hours.
///
private readonly SecurityExchangeHours _securityExchangeHours;
///
/// The tick types supported for universe data.
///
private readonly TickType[] UniverseTickTypes = { TickType.Quote, TickType.Trade, TickType.OpenInterest };
///
/// Gets the underlying symbol associated with the universe.
///
public Symbol UnderlyingSymbol { get => Symbol.HasUnderlying ? Symbol.Underlying : Symbol.Empty; }
///
/// Initializes a new instance of the class.
///
/// The canonical symbol for the data request.
/// The start date for the data request.
/// The end date for the data request.
/// The security exchange hours for this symbol
/// Thrown when the provided symbol is not canonical.
public DataUniverseDownloaderGetParameters(Symbol canonicalSymbol, DateTime startDate, DateTime endDate, SecurityExchangeHours securityExchangeHours = default)
: base(
canonicalSymbol.IsCanonical() ? canonicalSymbol : throw new ArgumentException("DataUniverseDownloaderGetParameters: Symbol must be canonical.", nameof(canonicalSymbol)),
Resolution.Daily,
startDate,
endDate)
{
_securityExchangeHours = securityExchangeHours ?? MarketHoursDatabase.FromDataFolder().GetExchangeHours(canonicalSymbol.ID.Market, canonicalSymbol, canonicalSymbol.SecurityType);
EndUtc = EndUtc.ConvertToUtc(_securityExchangeHours.TimeZone);
StartUtc = StartUtc.ConvertToUtc(_securityExchangeHours.TimeZone);
}
///
/// Gets the file name where the universe data will be saved.
///
/// The date for which the file name is generated.
/// The universe file name.
public string GetUniverseFileName(DateTime processingDate)
{
return BaseChainUniverseData.GetUniverseFullFilePath(Symbol, processingDate);
}
///
/// Creates data download parameters for each day in the range.
///
public IEnumerable<(DateTime, IEnumerable)> CreateDataDownloaderGetParameters()
{
foreach (var processingDate in Time.EachTradeableDay(_securityExchangeHours, StartUtc, EndUtc))
{
var processingDateUtc = processingDate.ConvertToUtc(_securityExchangeHours.TimeZone);
var requests = new List(3);
if (UnderlyingSymbol != Symbol.Empty)
{
requests.Add(new(UnderlyingSymbol, Resolution, processingDateUtc, processingDateUtc.AddDays(1), TickType.Trade));
}
requests.AddRange(UniverseTickTypes.Select(tickType => new DataDownloaderGetParameters(Symbol, Resolution, processingDateUtc, processingDateUtc.AddDays(1), tickType)));
yield return (processingDate, requests);
}
}
}