Skip to content

Commit d40aa3c

Browse files
committed
Adjust times to exchange and QB time zones in Option/Future history calls
1 parent e1cd636 commit d40aa3c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

Research/QuantBook.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,11 @@ private IEnumerable<T> GetChainHistory<T>(Symbol canonicalSymbol, DateTime date,
862862
{
863863
// Use this GetEntry extension method since it's data type dependent, so we get the correct entry for the option universe
864864
var marketHoursEntry = MarketHoursDatabase.GetEntry(canonicalSymbol, new[] { typeof(T) });
865-
var start = QuantConnect.Time.GetStartTimeForTradeBars(marketHoursEntry.ExchangeHours, date, QuantConnect.Time.OneDay, 1,
865+
var startInExchangeTz = QuantConnect.Time.GetStartTimeForTradeBars(marketHoursEntry.ExchangeHours, date, QuantConnect.Time.OneDay, 1,
866866
extendedMarketHours: false, marketHoursEntry.DataTimeZone);
867-
var universeData = History<T>(canonicalSymbol, start, date).SingleOrDefault();
867+
var start = startInExchangeTz.ConvertTo(marketHoursEntry.ExchangeHours.TimeZone, TimeZone);
868+
var end = date.ConvertTo(marketHoursEntry.ExchangeHours.TimeZone, TimeZone);
869+
var universeData = History<T>(canonicalSymbol, start, end).SingleOrDefault();
868870

869871
if (universeData is not null)
870872
{

Tests/Research/QuantBookHistoryTests.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,30 +192,40 @@ private static TestCaseData[] CanonicalOptionIntradayHistoryTestCases
192192
var twx = Symbol.Create("TWX", SecurityType.Equity, Market.USA);
193193
var twxOption = Symbol.CreateCanonicalOption(twx);
194194

195+
var spx = Symbol.Create("SPX", SecurityType.Index, Market.USA);
196+
var spxwOption = Symbol.CreateCanonicalOption(spx, Market.USA, null);
197+
195198
return
196199
[
197-
new TestCaseData(twxOption, new DateTime(2014, 06, 05), (DateTime?)null),
198-
new TestCaseData(twxOption, new DateTime(2014, 06, 05), new DateTime(2014, 06, 05)),
199-
new TestCaseData(twxOption, new DateTime(2014, 06, 05), new DateTime(2014, 06, 06)),
200-
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 0, 0, 0), new DateTime(2014, 06, 05, 15, 0, 0)),
201-
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 05, 15, 0, 0)),
202-
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 06)),
203-
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 06, 10, 0, 0)),
204-
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 06, 15, 0, 0))
200+
new TestCaseData(twxOption, new DateTime(2014, 06, 05), (DateTime?)null, Resolution.Minute),
201+
new TestCaseData(twxOption, new DateTime(2014, 06, 05), new DateTime(2014, 06, 05), Resolution.Minute),
202+
new TestCaseData(twxOption, new DateTime(2014, 06, 05), new DateTime(2014, 06, 06), Resolution.Minute),
203+
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 0, 0, 0), new DateTime(2014, 06, 05, 15, 0, 0), Resolution.Minute),
204+
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 05, 15, 0, 0), Resolution.Minute),
205+
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 06), Resolution.Minute),
206+
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 06, 10, 0, 0), Resolution.Minute),
207+
new TestCaseData(twxOption, new DateTime(2014, 06, 05, 10, 0, 0), new DateTime(2014, 06, 06, 15, 0, 0), Resolution.Minute),
208+
209+
new TestCaseData(spxwOption, new DateTime(2021, 01, 04), (DateTime?)null, Resolution.Hour),
210+
new TestCaseData(spxwOption, new DateTime(2021, 01, 04), new DateTime(2021, 01, 04), Resolution.Hour),
211+
new TestCaseData(spxwOption, new DateTime(2021, 01, 04), new DateTime(2021, 01, 05), Resolution.Hour),
212+
new TestCaseData(spxwOption, new DateTime(2021, 01, 04, 10, 0, 0), new DateTime(2021, 01, 04, 15, 0, 0), Resolution.Hour),
213+
new TestCaseData(spxwOption, new DateTime(2021, 01, 04, 10, 0, 0), new DateTime(2021, 01, 05, 15, 0, 0), Resolution.Hour),
214+
new TestCaseData(spxwOption, new DateTime(2021, 01, 14, 10, 0, 0), new DateTime(2021, 01, 14, 15, 0, 0), Resolution.Hour),
205215
];
206216
}
207217
}
208218

209219
[TestCaseSource(nameof(CanonicalOptionIntradayHistoryTestCases))]
210-
public void CanonicalOptionIntradayQuantBookHistoryWithIntradayRange(Symbol canonicalOption, DateTime start, DateTime? end)
220+
public void CanonicalOptionIntradayQuantBookHistoryWithIntradayRange(Symbol canonicalOption, DateTime start, DateTime? end, Resolution resolution)
211221
{
212222
var quantBook = new QuantBook();
213223
var historyProvider = new TestHistoryProvider(quantBook.HistoryProvider);
214224
quantBook.SetHistoryProvider(historyProvider);
215225
quantBook.SetStartDate((end ?? start).Date.AddDays(1));
216226

217227
var option = quantBook.AddSecurity(canonicalOption);
218-
var history = quantBook.OptionHistory(canonicalOption, start, end, Resolution.Minute);
228+
var history = quantBook.OptionHistory(canonicalOption, start, end, resolution);
219229

220230
Assert.Greater(history.Count, 0);
221231

0 commit comments

Comments
 (0)