diff --git a/Algorithm.CSharp/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.cs b/Algorithm.CSharp/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.cs index 4a424dccfba8..4294ae52697a 100644 --- a/Algorithm.CSharp/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.cs +++ b/Algorithm.CSharp/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.cs @@ -32,6 +32,7 @@ public class ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm : QCAlgorithm, private RelativeStrengthIndex _rsiTimeDelta; private Dictionary _values = new(); private int _count; + private bool _indicatorsCompared; public override void Initialize() { @@ -68,9 +69,10 @@ public override void OnData(Slice slice) var time = bar.EndTime.Date; if (_rsiTimeDelta.Current.Value != _values[time]) { - throw new Exception($"Both {_rsi.Name} and {_rsiTimeDelta.Name} should have the same values, but they differ. {_rsi.Name}: {_values[time]} | {_rsiTimeDelta.Name}: {_rsiTimeDelta.Current.Value}"); + throw new RegressionTestException($"Both {_rsi.Name} and {_rsiTimeDelta.Name} should have the same values, but they differ. {_rsi.Name}: {_values[time]} | {_rsiTimeDelta.Name}: {_rsiTimeDelta.Current.Value}"); } } + _indicatorsCompared = true; Quit(); } else @@ -88,6 +90,14 @@ public override void OnData(Slice slice) } } + public override void OnEndOfAlgorithm() + { + if (!_indicatorsCompared) + { + throw new RegressionTestException($"Indicators {_rsi.Name} and {_rsiTimeDelta.Name} should have been compared, but they were not. Please make sure the indicators are getting SPY data"); + } + } + /// /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. /// diff --git a/Algorithm.Python/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.py b/Algorithm.Python/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.py index 45d3c5f3c831..2f0ee19bba29 100644 --- a/Algorithm.Python/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.py +++ b/Algorithm.Python/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.py @@ -38,6 +38,7 @@ def initialize(self): self._rsi_timedelta = RelativeStrengthIndex("Second", 15, MovingAverageType.WILDERS) self._values = {} self.count = 0; + self._indicators_compared = False; def on_data(self, data: Slice): if self.is_warming_up: @@ -51,6 +52,7 @@ def on_data(self, data: Slice): self._rsi_timedelta.update(bar.end_time, bar.close) if self._rsi_timedelta.current.value != self._values[time]: raise Exception(f"Both {self._rsi.name} and {self._rsi_timedelta.name} should have the same values, but they differ. {self._rsi.name}: {self._values[time]} | {self._rsi_timedelta.name}: {self._rsi_timedelta.current.value}") + self._indicators_compared = True self.quit() else: time = self.time.strftime('%Y-%m-%d') @@ -61,3 +63,7 @@ def on_data(self, data: Slice): # how many we need to request from the history if self.time.hour == 16: self.count += 1 + + def on_end_of_algorithm(self): + if not self._indicators_compared: + raise Exception(f"Indicators {self._rsi.name} and {self._rsi_timedelta.name} should have been compared, but they were not. Please make sure the indicators are getting SPY data") diff --git a/Tests/Common/Data/MarketHourAwareConsolidatorTests.cs b/Tests/Common/Data/MarketHourAwareConsolidatorTests.cs index d577c73000bd..b639320a5193 100644 --- a/Tests/Common/Data/MarketHourAwareConsolidatorTests.cs +++ b/Tests/Common/Data/MarketHourAwareConsolidatorTests.cs @@ -118,7 +118,28 @@ public void Daily(bool strictEndTime) } [Test] - public void FirstHourBarIsNotSkippedWhenBarResolutionIsHour() + public void BarIsSkippedWhenDataResolutionIsNotHourAndMarketIsClose() + { + var symbol = Symbols.SPY; + using var consolidator = new MarketHourAwareConsolidator(true, Resolution.Daily, typeof(TradeBar), TickType.Trade, false); + var consolidatedBarsCount = 0; + TradeBar latestBar = null; + + consolidator.DataConsolidated += (sender, bar) => + { + latestBar = (TradeBar)bar; + consolidatedBarsCount++; + }; + + var time = new DateTime(2020, 05, 01, 09, 30, 0); + // this bar will be ignored because it's during market closed hours and the bar resolution is not Hour + consolidator.Update(new TradeBar() { Time = time.Subtract(Time.OneMinute), Period = Time.OneMinute, Symbol = symbol, Open = 1 }); + Assert.IsNull(latestBar); + Assert.AreEqual(0, consolidatedBarsCount); + } + + [Test] + public void DailyBarCanBeConsolidatedFromHourData() { var symbol = Symbols.SPY; using var consolidator = new MarketHourAwareConsolidator(true, Resolution.Daily, typeof(TradeBar), TickType.Trade, false);