|
| 1 | +/* |
| 2 | + * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 3 | + * Lean Algorithmic Trading Engine v2.0. Copyright 2026 QuantConnect Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using NodaTime; |
| 18 | +using System.Linq; |
| 19 | +using QuantConnect.Data; |
| 20 | +using QuantConnect.Util; |
| 21 | +using QuantConnect.Interfaces; |
| 22 | +using System.Collections.Generic; |
| 23 | +using QuantConnect.Lean.Engine.DataFeeds; |
| 24 | + |
| 25 | +namespace QuantConnect.Lean.Engine.HistoricalData |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Base class for history providers that resolve symbol mappings |
| 29 | + /// and synchronize multiple data streams into time-aligned slices. |
| 30 | + /// </summary> |
| 31 | + public abstract class MappedSynchronizingHistoryProvider : SynchronizingHistoryProvider |
| 32 | + { |
| 33 | + /// <summary> |
| 34 | + /// Resolves map files to correctly handle current and historical ticker symbols. |
| 35 | + /// </summary> |
| 36 | + private static readonly Lazy<IMapFileProvider> _mapFileProvider = new(Composer.Instance.GetPart<IMapFileProvider>); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets historical data for a single resolved history request. |
| 40 | + /// Implementations should assume the symbol is already correctly mapped. |
| 41 | + /// </summary> |
| 42 | + /// <param name="request">The resolved history request.</param> |
| 43 | + /// <returns>The historical data.</returns> |
| 44 | + public abstract IEnumerable<BaseData>? GetHistory(HistoryRequest request); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets the history for the requested securities |
| 48 | + /// </summary> |
| 49 | + /// <param name="requests">The historical data requests</param> |
| 50 | + /// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param> |
| 51 | + /// <returns>An enumerable of the slices of data covering the span specified in each request</returns> |
| 52 | + public override IEnumerable<Slice>? GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone) |
| 53 | + { |
| 54 | + var subscriptions = new List<Subscription>(); |
| 55 | + foreach (var request in requests) |
| 56 | + { |
| 57 | + var history = request |
| 58 | + .SplitHistoryRequestWithUpdatedMappedSymbol(_mapFileProvider.Value) |
| 59 | + .SelectMany(x => GetHistory(x) ?? []); |
| 60 | + var subscription = CreateSubscription(request, history); |
| 61 | + if (!subscription.MoveNext()) |
| 62 | + { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + subscriptions.Add(subscription); |
| 67 | + } |
| 68 | + |
| 69 | + if (subscriptions.Count == 0) |
| 70 | + { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + // Ownership of subscription is transferred to CreateSliceEnumerableFromSubscriptions |
| 75 | + return CreateSliceEnumerableFromSubscriptions(subscriptions, sliceTimeZone); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments