Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion Common/AlgorithmImports.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
from QuantConnect.Securities.Equity import *
from QuantConnect.Securities.Future import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Common import *
from QuantConnect.Orders.TimeInForces import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Selection import *
Expand Down
68 changes: 1 addition & 67 deletions Common/Data/Consolidators/MarketHourAwareConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,6 @@ public MarketHourAwareConsolidator(bool dailyStrictEndTimeEnabled, Resolution re
Consolidator.DataConsolidated += ForwardConsolidatedBar;
}

/// <summary>
/// Initializes a new instance of the <see cref="MarketHourAwareConsolidator"/> class for an arbitrary period.
/// Intraday periods are anchored to the market open without extending past the close.
/// </summary>
/// <param name="dailyStrictEndTimeEnabled">True if daily strict end times should be enabled</param>
/// <param name="period">The consolidation period</param>
/// <param name="dataType">The target data type</param>
/// <param name="tickType">The target tick type</param>
/// <param name="extendedMarketHours">True if extended market hours should be consolidated</param>
public MarketHourAwareConsolidator(bool dailyStrictEndTimeEnabled, TimeSpan period, Type dataType, TickType tickType, bool extendedMarketHours)
{
_dailyStrictEndTimeEnabled = dailyStrictEndTimeEnabled;
Period = period;
_extendedMarketHours = extendedMarketHours;

// when the period exactly matches a standard resolution, reuse the resolution based consolidation so its
// well-tested behavior is preserved; only arbitrary periods need the market-open anchored intraday calendar
var resolution = period.ToHigherResolutionEquivalent(false);
if (resolution.ToTimeSpan() == period)
{
Consolidator = CreateConsolidator(resolution, dataType, tickType);
}
else
{
Func<DateTime, CalendarInfo> calendar = period < Time.OneDay ? IntradayCalendar : DailyStrictEndTime;
Consolidator = CreateConsolidator(calendar, dataType, tickType);
}
Consolidator.DataConsolidated += ForwardConsolidatedBar;
}

/// <summary>
/// Creates the inner consolidator that produces the requested <paramref name="dataType"/> output.
/// </summary>
Expand Down Expand Up @@ -151,28 +121,6 @@ protected virtual IDataConsolidator CreateConsolidator(Resolution resolution, Ty
throw new ArgumentNullException(nameof(dataType), $"{dataType.Name} not supported");
}

/// <summary>
/// Creates the underlying calendar based consolidator for the given data type, used for arbitrary periods
/// </summary>
protected virtual IDataConsolidator CreateConsolidator(Func<DateTime, CalendarInfo> calendar, Type dataType, TickType tickType)
{
if (dataType == typeof(Tick))
{
return tickType == TickType.Trade
? new TickConsolidator(calendar)
: new TickQuoteBarConsolidator(calendar);
}
if (dataType == typeof(TradeBar))
{
return new TradeBarConsolidator(calendar);
}
if (dataType == typeof(QuoteBar))
{
return new QuoteBarConsolidator(calendar);
}
throw new ArgumentNullException(nameof(dataType), $"{dataType.Name} not supported");
}

/// <summary>
/// Event handler that fires when a new piece of data is produced
/// </summary>
Expand Down Expand Up @@ -247,27 +195,13 @@ protected void Initialize(IBaseData data)
/// </summary>
protected virtual CalendarInfo DailyStrictEndTime(DateTime dateTime)
{
// strict end times describe a single daily bar, so periods larger than a day fall back to standard period consolidation
if (!_useStrictEndTime || Period > Time.OneDay)
if (!_useStrictEndTime)
{
return new(Period > Time.OneDay ? dateTime : dateTime.RoundDown(Period), Period);
}
return LeanData.GetDailyCalendar(dateTime, ExchangeHours, _extendedMarketHours);
}

/// <summary>
/// Determines a bar start time and period for intraday consolidation, anchored to the market open
/// without extending past the market close so a bar never spans across closed market hours
/// </summary>
protected virtual CalendarInfo IntradayCalendar(DateTime dateTime)
{
if (ExchangeHours == null || ExchangeHours.IsMarketAlwaysOpen)
{
return new(dateTime.RoundDown(Period), Period);
}
return LeanData.GetIntradayCalendar(dateTime, Period, ExchangeHours, _extendedMarketHours);
}

/// <summary>
/// Useful for testing
/// </summary>
Expand Down
27 changes: 0 additions & 27 deletions Common/Util/LeanData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1476,33 +1476,6 @@ public static CalendarInfo GetDailyCalendar(DateTime exchangeTimeZoneDate, Secur
return new CalendarInfo(startTime, period);
}

/// <summary>
/// Helper method to return the intraday bar start time and period, anchored to the market open, without extending past the close
/// </summary>
/// <param name="exchangeTimeZoneDate">The point in time we want to get the bar information about</param>
/// <param name="period">The intraday consolidation period</param>
/// <param name="exchangeHours">The associated exchange hours</param>
/// <param name="extendedMarketHours">True if extended market hours should be taken into consideration</param>
/// <returns>The calendar information that holds a start time and a period</returns>
public static CalendarInfo GetIntradayCalendar(DateTime exchangeTimeZoneDate, TimeSpan period, SecurityExchangeHours exchangeHours, bool extendedMarketHours)
{
var marketOpen = exchangeHours.IsOpen(exchangeTimeZoneDate, extendedMarketHours)
? exchangeHours.GetPreviousMarketOpen(exchangeTimeZoneDate.AddTicks(1), extendedMarketHours)
: exchangeHours.GetPreviousMarketOpen(exchangeTimeZoneDate, extendedMarketHours);

var intervalsPassed = (long)Math.Floor((exchangeTimeZoneDate - marketOpen).Ticks / (double)period.Ticks);
var startTime = marketOpen.AddTicks(intervalsPassed * period.Ticks);

// keep the last bar from extending past the market close
var endTime = startTime + period;
var marketClose = exchangeHours.GetNextMarketClose(marketOpen, extendedMarketHours);
if (endTime > marketClose)
{
endTime = marketClose;
}
return new CalendarInfo(startTime, endTime - startTime);
}

/// <summary>
/// Helper method to get the next daily end time, taking into account strict end times if appropriate
/// </summary>
Expand Down
Loading
Loading