Lean  $LEAN_TAG$
HistoryExtensions.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 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 System.Linq;
19 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
22 
23 namespace QuantConnect.Data
24 {
25  /// <summary>
26  /// Helper extension methods for objects related with Histotical data
27  /// </summary>
28  public static class HistoryExtensions
29  {
30  private static readonly Regex _brokerageHistoryProvider = new("QuantConnect.Lean.Engine.HistoricalData.([a-zA-z]+)HistoryProvider", RegexOptions.Compiled);
31 
32  /// <summary>
33  /// Helper method to get the brokerage name
34  /// </summary>
35  public static bool TryGetBrokerageName(string historyProviderName, out string brokerageName)
36  {
37  brokerageName = null;
38  if (historyProviderName != "QuantConnect.Lean.Engine.HistoricalData.BrokerageHistoryProvider"
39  && historyProviderName != "QuantConnect.Lean.Engine.HistoricalData.SubscriptionDataReaderHistoryProvider")
40  {
41  var matches = _brokerageHistoryProvider.Match(historyProviderName);
42  if (matches.Success)
43  {
44  brokerageName = matches.Groups[1].Value;
45  return true;
46  }
47  }
48 
49  return false;
50  }
51 
52  /// <summary>
53  /// Split <see cref="HistoryRequest"/> on several request with update mapped symbol.
54  /// </summary>
55  /// <param name="request">Represents historical data requests</param>
56  /// <param name="mapFileProvider">Provides instances of <see cref="MapFileResolver"/> at run time</param>
57  /// <returns>
58  /// Return HistoryRequests with different <see cref="BaseDataRequest.StartTimeUtc"/> - <seealso cref="BaseDataRequest.EndTimeUtc"/> range
59  /// and <seealso cref="Symbol.Value"/>
60  /// </returns>
61  /// <exception cref="ArgumentNullException">Thrown when <paramref name="mapFileProvider"/> is null.</exception>
62  /// <example>
63  /// For instances:
64  /// request = { StartTimeUtc = 2013/01/01, EndTimeUtc = 2017/02/02, Symbol = "GOOGL" } split request on:
65  /// 1: request = { StartTimeUtc = 2013/01/01, EndTimeUtc = 2014/04/02, Symbol.Value = "GOOG" }
66  /// 2: request = { StartTimeUtc = 2014/04/**03**, EndTimeUtc = 2017/02/02, Symbol.Value = "GOOGL" }
67  /// > GOOGLE: IPO: August 19, 2004 Name = GOOG then it was restructured: from "GOOG" to "GOOGL" on April 2, 2014
68  /// </example>
69  public static IEnumerable<HistoryRequest> SplitHistoryRequestWithUpdatedMappedSymbol(this HistoryRequest request, IMapFileProvider mapFileProvider)
70  {
71  if (request == null)
72  {
73  throw new ArgumentNullException(nameof(request));
74  }
75 
76  if (request.Symbol.SecurityType != SecurityType.Future && request.Symbol.RequiresMapping())
77  {
78  var isReturnHistoryRequest = default(bool);
79  foreach (var tickerDateRange in mapFileProvider.RetrieveSymbolHistoricalDefinitionsInDateRange(request.Symbol, request.StartTimeLocal, request.EndTimeLocal))
80  {
81  isReturnHistoryRequest = true;
82  var symbol = request.Symbol.UpdateMappedSymbol(tickerDateRange.Ticker);
83  yield return new HistoryRequest(
84  request,
85  symbol,
86  tickerDateRange.StartDateTimeLocal.ConvertToUtc(request.ExchangeHours.TimeZone),
87  tickerDateRange.EndDateTimeLocal.ConvertToUtc(request.ExchangeHours.TimeZone)
88  );
89  }
90 
91  if (!isReturnHistoryRequest)
92  {
93  yield return request;
94  }
95  }
96  else
97  {
98  yield return request;
99  }
100  }
101  }
102 }