/*
* 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 QuantConnect.Interfaces;
using QuantConnect.Securities;
using QuantConnect.Util;
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
///
public DateTime GetStartTimeAlgoTz(
Symbol symbol,
int periods,
Resolution resolution,
SecurityExchangeHours exchange)
{
var isExtendedMarketHours = _algorithm.SubscriptionManager
.SubscriptionDataConfigService
.GetSubscriptionDataConfigs(symbol)
.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);
return localStartTime.ConvertTo(exchange.TimeZone, _algorithm.TimeZone);
}
}
}