Skip to content

Commit

Permalink
Improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Dec 5, 2024
1 parent 381c90c commit d0c52ae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm : QCAlgorithm,
private RelativeStrengthIndex _rsiTimeDelta;
private Dictionary<DateTime, decimal> _values = new();
private int _count;
private bool _indicatorsCompared;

public override void Initialize()
{
Expand Down Expand Up @@ -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
Expand All @@ -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");
}
}

/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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')
Expand All @@ -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")
23 changes: 22 additions & 1 deletion Tests/Common/Data/MarketHourAwareConsolidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d0c52ae

Please sign in to comment.