/* * 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.Data; using QuantConnect.Data.Market; using QuantConnect.Data.UniverseSelection; using QuantConnect.Lean.Engine.DataFeeds; using QuantConnect.Securities; using System; using System.Collections.Generic; using System.Linq; using HistoryRequest = QuantConnect.Data.HistoryRequest; namespace QuantConnect.Lean.Engine.HistoricalData { /// /// Implements a History provider that always return a IEnumerable of Slice with prices following a sine function /// public class SineHistoryProvider : HistoryProviderBase { private readonly SecurityChanges _securityChanges = SecurityChanges.None; private readonly SecurityManager _securities; /// /// Gets the total number of data points emitted by this history provider /// public override int DataPointCount => 0; /// /// Initializes a new instance of the class /// /// Collection of securities that a history request can return public SineHistoryProvider(SecurityManager securities) { _securities = securities; } /// /// Initializes this history provider to work for the specified job /// /// The initialization parameters public override void Initialize(HistoryProviderInitializeParameters parameters) { } /// /// Gets the history for the requested securities /// /// The historical data requests /// The time zone used when time stamping the slice instances /// An enumerable of the slices of data covering the span specified in each request public override IEnumerable GetHistory(IEnumerable requests, DateTimeZone sliceTimeZone) { var configsByDateTime = GetSubscriptionDataConfigByDateTime(requests); var count = configsByDateTime.Count; var i = 0; var timeSliceFactory = new TimeSliceFactory(sliceTimeZone); foreach (var kvp in configsByDateTime) { var utcDateTime = kvp.Key; var configs = kvp.Value; var last = Convert.ToDecimal(100 + 10 * Math.Sin(Math.PI * (360 - count + i) / 180.0)); var high = last * 1.005m; var low = last / 1.005m; var packets = new List(); foreach (var config in configs) { Security security; if (!_securities.TryGetValue(config.Symbol, out security)) { continue; } var period = config.Resolution.ToTimeSpan(); var time = (utcDateTime - period).ConvertFromUtc(config.DataTimeZone); var data = new TradeBar(time, config.Symbol, last, high, last, last, 1000, period); security.SetMarketPrice(data); packets.Add(new DataFeedPacket(security, config, new List { data })); } i++; yield return timeSliceFactory.Create(utcDateTime, packets, _securityChanges, new Dictionary()).Slice; } } private Dictionary> GetSubscriptionDataConfigByDateTime( IEnumerable requests) { var dictionary = new Dictionary>(); var barSize = requests.Select(x => x.Resolution.ToTimeSpan()).Min(); var startUtc = requests.Min(x => x.StartTimeUtc); var endUtc = requests.Max(x => x.EndTimeUtc); for (var utcDateTime = startUtc; utcDateTime < endUtc; utcDateTime += barSize) { var subscriptionDataConfig = new List(); foreach (var request in requests) { var exchange = request.ExchangeHours; var extendedMarket = request.IncludeExtendedMarketHours; var localDateTime = utcDateTime.ConvertFromUtc(exchange.TimeZone); if (!exchange.IsOpen(localDateTime, extendedMarket)) { continue; } var config = request.ToSubscriptionDataConfig(); subscriptionDataConfig.Add(config); } if (subscriptionDataConfig.Count > 0) { dictionary.Add(utcDateTime.Add(barSize), subscriptionDataConfig); } } return dictionary; } } }