/* * 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.Interfaces; using QuantConnect.Securities; using QuantConnect.Util; using System; namespace QuantConnect.Data { /// /// Helper class used to create new /// public class HistoryRequestFactory { private readonly IAlgorithm _algorithm; /// /// Creates a new instance /// /// The algorithm instance to use public HistoryRequestFactory(IAlgorithm algorithm) { _algorithm = algorithm; } /// /// Creates a new history request /// /// The config /// History request start time in algorithm time zone /// History request end time in algorithm time zone /// Security exchange hours /// The resolution to use. If null will use /// The new public HistoryRequest CreateHistoryRequest(SubscriptionDataConfig subscription, DateTime startAlgoTz, DateTime endAlgoTz, SecurityExchangeHours exchangeHours, Resolution? resolution) { resolution = resolution ?? subscription.Resolution; // find the correct data type for the history request var dataType = subscription.IsCustomData ? subscription.Type : LeanData.GetDataType(resolution.Value, subscription.TickType); var request = new HistoryRequest(subscription, exchangeHours, startAlgoTz.ConvertToUtc(_algorithm.TimeZone), endAlgoTz.ConvertToUtc(_algorithm.TimeZone)) { DataType = dataType, Resolution = resolution.Value, FillForwardResolution = subscription.FillDataForward ? resolution : null, TickType = subscription.TickType }; return request; } /// /// Gets the start time required for the specified bar count in terms of the algorithm's time zone /// /// The symbol to select proper config /// The number of bars requested /// The length of each bar /// The exchange hours used for market open hours /// The time zone in which data are stored /// The start time that would provide the specified number of bars ending at the algorithm's current time public DateTime GetStartTimeAlgoTz( Symbol symbol, int periods, Resolution resolution, SecurityExchangeHours exchange, DateTimeZone dataTimeZone) { var configs = _algorithm.SubscriptionManager .SubscriptionDataConfigService .GetSubscriptionDataConfigs(symbol); // hour resolution does no have extended market hours data var isExtendedMarketHours = resolution != Resolution.Hour && configs.IsExtendedMarketHours(); var timeSpan = resolution.ToTimeSpan(); // make this a minimum of one second timeSpan = timeSpan < Time.OneSecond ? Time.OneSecond : timeSpan; var localStartTime = Time.GetStartTimeForTradeBars( exchange, _algorithm.UtcTime.ConvertFromUtc(exchange.TimeZone), timeSpan, periods, isExtendedMarketHours, dataTimeZone); return localStartTime.ConvertTo(exchange.TimeZone, _algorithm.TimeZone); } } }