|
17 | 17 | using NUnit.Framework; |
18 | 18 | using QuantConnect.Securities; |
19 | 19 | using QuantConnect.Data.Market; |
| 20 | +using System.Collections.Generic; |
20 | 21 |
|
21 | 22 | namespace QuantConnect.Tests.Indicators |
22 | 23 | { |
@@ -90,12 +91,103 @@ public void EndTimeDoesNotOverflowWhenAccessedBeforeFirstUpdate() |
90 | 91 | Assert.AreEqual(1000, session.Volume); |
91 | 92 | } |
92 | 93 |
|
93 | | - private Session GetSession(TickType tickType, int initialSize) |
| 94 | + private static IEnumerable<TestCaseData> ConsolidationTestCases() |
| 95 | + { |
| 96 | + // Hour resolution during regular market hours |
| 97 | + yield return new TestCaseData(new DateTime(2025, 8, 25, 10, 0, 0), Resolution.Hour); |
| 98 | + |
| 99 | + // Daily resolution and bar emitted at midnight |
| 100 | + yield return new TestCaseData(new DateTime(2025, 8, 25, 0, 0, 0), Resolution.Daily); |
| 101 | + } |
| 102 | + |
| 103 | + [TestCaseSource(nameof(ConsolidationTestCases))] |
| 104 | + public void ConsolidatesDaily(DateTime baseDate, Resolution resolution) |
| 105 | + { |
| 106 | + var symbol = Symbols.SPY; |
| 107 | + var session = GetSession(TickType.Trade, 4); |
| 108 | + var days = new[] |
| 109 | + { |
| 110 | + new { Expected = new TradeBar(baseDate.Date, symbol, 100, 101, 99, 100, 6000, Time.OneDay) }, |
| 111 | + new { Expected = new TradeBar(baseDate.Date.AddDays(1), symbol, 100, 101, 99, 100, 6000, Time.OneDay) }, |
| 112 | + new { Expected = new TradeBar(baseDate.Date.AddDays(2), symbol, 100, 101, 99, 100, 6000, Time.OneDay) }, |
| 113 | + }; |
| 114 | + |
| 115 | + Assert.AreEqual(1, session.Samples); |
| 116 | + |
| 117 | + for (int i = 0; i < days.Length; i++) |
| 118 | + { |
| 119 | + var startDate = baseDate.AddDays(i); |
| 120 | + var endDate = startDate.Date.AddDays(1); |
| 121 | + |
| 122 | + if (resolution == Resolution.Hour) |
| 123 | + { |
| 124 | + for (int j = 0; j < 6; j++) |
| 125 | + { |
| 126 | + session.Update(new TradeBar(startDate.AddHours(j), symbol, 100, 101, 99, 100, 1000, Time.OneHour)); |
| 127 | + } |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + session.Update(new TradeBar(startDate, symbol, 100, 101, 99, 100, 6000, Time.OneDay)); |
| 132 | + } |
| 133 | + |
| 134 | + session.Scan(endDate); |
| 135 | + Assert.AreEqual(i + 2, session.Samples); |
| 136 | + Assert.IsTrue(BarsAreEqual(days[i].Expected, session[1])); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + [TestCaseSource(nameof(NextSessionTradingDayCases))] |
| 141 | + public void CreatesNewSessionBarWithCorrectNextTradingDay(DateTime startDate, DateTime expectedDate) |
| 142 | + { |
| 143 | + var symbol = Symbols.SPY; |
| 144 | + var session = GetSession(TickType.Trade, 3); |
| 145 | + var endDate = startDate.AddHours(14); |
| 146 | + |
| 147 | + for (int i = 0; i < 6; i++) |
| 148 | + { |
| 149 | + session.Update(new TradeBar(startDate.AddHours(i), symbol, 100, 101, 99, 100, 1000, TimeSpan.FromHours(1))); |
| 150 | + } |
| 151 | + |
| 152 | + session.Scan(endDate); |
| 153 | + |
| 154 | + var sessionBar = session[0]; |
| 155 | + Assert.AreNotEqual(DateTime.MaxValue, sessionBar.Time); |
| 156 | + Assert.AreEqual(expectedDate, sessionBar.Time); |
| 157 | + Assert.AreEqual(expectedDate.AddDays(1), sessionBar.EndTime); |
| 158 | + Assert.AreEqual(0, sessionBar.Open); |
| 159 | + Assert.AreEqual(0, sessionBar.High); |
| 160 | + Assert.AreEqual(0, sessionBar.Low); |
| 161 | + Assert.AreEqual(0, sessionBar.Close); |
| 162 | + Assert.AreEqual(0, sessionBar.Volume); |
| 163 | + } |
| 164 | + |
| 165 | + private static IEnumerable<TestCaseData> NextSessionTradingDayCases() |
| 166 | + { |
| 167 | + // Regular weekday: next trading day is simply the next calendar day |
| 168 | + yield return new TestCaseData(new DateTime(2025, 8, 25, 10, 0, 0), new DateTime(2025, 8, 26)); |
| 169 | + |
| 170 | + // Friday before Labor Day weekend -> next trading day is Tuesday (Sep 2, 2025) |
| 171 | + yield return new TestCaseData(new DateTime(2025, 8, 29, 10, 0, 0), new DateTime(2025, 9, 2)); |
| 172 | + } |
| 173 | + |
| 174 | + private static Session GetSession(TickType tickType, int initialSize) |
94 | 175 | { |
95 | 176 | var symbol = Symbols.SPY; |
96 | 177 | var marketHoursDatabase = MarketHoursDatabase.FromDataFolder(); |
97 | 178 | var exchangeHours = marketHoursDatabase.GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType); |
98 | 179 | return new Session(tickType, exchangeHours, symbol, initialSize); |
99 | 180 | } |
| 181 | + |
| 182 | + private static bool BarsAreEqual(TradeBar bar1, TradeBar bar2) |
| 183 | + { |
| 184 | + return bar1.Time == bar2.Time && |
| 185 | + bar1.EndTime == bar2.EndTime && |
| 186 | + bar1.Open == bar2.Open && |
| 187 | + bar1.High == bar2.High && |
| 188 | + bar1.Low == bar2.Low && |
| 189 | + bar1.Close == bar2.Close && |
| 190 | + bar1.Volume == bar2.Volume; |
| 191 | + } |
100 | 192 | } |
101 | 193 | } |
0 commit comments