Skip to content

Commit 1ef66a1

Browse files
authored
Fix Retrieval of Historical Data for Equities with Different Resolutions (#8538)
* Refactor GetMatchingSubscriptions: - Fixed retrieval of minute-resolution quote data for hour-resolution equity - Ensured correct retrieval of data regardless of equity resolution - Created a unit test to verify functionality * Update unit test and create test cases * Add new unit test * Addressed review comments
1 parent 0fccf60 commit 1ef66a1

File tree

3 files changed

+161
-57
lines changed

3 files changed

+161
-57
lines changed

Algorithm.Python/HistoryAlgorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def initialize(self):
6161
# get the historical data from last current day to this current day in minute resolution
6262
# with Extended Market option
6363
interval_bar_history = self.history(["SPY"], self.time - timedelta(1), self.time, Resolution.MINUTE, False, True)
64-
self.assert_history_count("History([\"SPY\"], self.time - timedelta(1), self.time, Resolution.MINUTE, False, True)", interval_bar_history, 828)
64+
self.assert_history_count("History([\"SPY\"], self.time - timedelta(1), self.time, Resolution.MINUTE, False, True)", interval_bar_history, 919)
6565

6666
# get the historical data from last current day to this current day in minute resolution
6767
# with Fill Forward option

Algorithm/QCAlgorithm.History.cs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,14 +1084,42 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
10841084
// since this might be called when creating a security and warming it up
10851085
if (configs != null && configs.Count != 0)
10861086
{
1087-
if (resolution.HasValue
1088-
&& (resolution == Resolution.Daily || resolution == Resolution.Hour)
1089-
&& symbol.SecurityType == SecurityType.Equity)
1087+
if (resolution.HasValue && symbol.SecurityType == SecurityType.Equity)
10901088
{
1091-
// for Daily and Hour resolution, for equities, we have to
1092-
// filter out any existing subscriptions that could be of Quote type
1093-
// This could happen if they were Resolution.Minute/Second/Tick
1094-
return configs.Where(s => s.TickType != TickType.Quote);
1089+
// Check if resolution is set and not Daily or Hourly for an Equity symbol
1090+
if (resolution == Resolution.Daily || resolution == Resolution.Hour)
1091+
{
1092+
// for Daily and Hour resolution, for equities, we have to
1093+
// filter out any existing subscriptions that could be of Quote type
1094+
// This could happen if they were Resolution.Minute/Second/Tick
1095+
return configs.Where(s => s.TickType != TickType.Quote);
1096+
}
1097+
1098+
1099+
// If no existing configuration for the Quote tick type, add the new config
1100+
if (type == null && !configs.Any(config => config.TickType == TickType.Quote))
1101+
{
1102+
type = LeanData.GetDataType(resolution.Value, TickType.Quote);
1103+
var entry = MarketHoursDatabase.GetEntry(symbol, new[] { type });
1104+
var baseFillForward = configs[0].FillDataForward;
1105+
var baseExtendedMarketHours = configs[0].ExtendedMarketHours;
1106+
1107+
// Create a new SubscriptionDataConfig
1108+
var newConfig = new SubscriptionDataConfig(
1109+
type,
1110+
symbol,
1111+
resolution.Value,
1112+
entry.DataTimeZone,
1113+
entry.ExchangeHours.TimeZone,
1114+
baseFillForward,
1115+
baseExtendedMarketHours,
1116+
false, tickType: TickType.Quote);
1117+
1118+
configs.Add(newConfig);
1119+
1120+
// Sort the configs in descending order based on tick type
1121+
return configs.OrderByDescending(config => GetTickTypeOrder(config.SecurityType, config.TickType));
1122+
}
10951123
}
10961124

10971125
if (symbol.IsCanonical() && configs.Count > 1)

0 commit comments

Comments
 (0)