Skip to content

Commit

Permalink
Address peer review and add some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Nov 26, 2024
1 parent 10b8723 commit 99f4920
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
10 changes: 9 additions & 1 deletion Common/Util/LeanData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,15 @@ public static bool UseDailyStrictEndTimes(IAlgorithmSettings settings, BaseDataR
/// </summary>
public static bool UseDailyStrictEndTimes(IAlgorithmSettings settings, Type dataType, Symbol symbol, TimeSpan increment, SecurityExchangeHours exchangeHours)
{
return UseDailyStrictEndTimes(dataType) && UseStrictEndTime(settings.DailyPreciseEndTime, symbol, increment, exchangeHours);
return UseDailyStrictEndTimes(settings.DailyPreciseEndTime, dataType, symbol, increment, exchangeHours);
}

/// <summary>
/// Helper method to determine if we should use strict end time
/// </summary>
public static bool UseDailyStrictEndTimes(bool dailyStrictEndTimeEnabled, Type dataType, Symbol symbol, TimeSpan increment, SecurityExchangeHours exchangeHours)
{
return UseDailyStrictEndTimes(dataType) && UseStrictEndTime(dailyStrictEndTimeEnabled, symbol, increment, exchangeHours);
}

/// <summary>
Expand Down
18 changes: 12 additions & 6 deletions Engine/DataFeeds/Enumerators/FillForwardEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ protected virtual bool RequiresFillForwardData(TimeSpan fillForwardResolution, B
var startTime = (_useStrictEndTime && item.Period > Time.OneHour) ? item.Start : RoundDown(item.Start, item.Period);
var potentialBarEndTime = startTime.ConvertToUtc(Exchange.TimeZone) + item.Period;


// to avoid duality it's necessary to compare potentialBarEndTime with
// next.EndTime calculated as Time + resolution,
// and both should be based on the same TZ (for example UTC)
Expand All @@ -381,13 +380,20 @@ protected virtual bool RequiresFillForwardData(TimeSpan fillForwardResolution, B
if (_useStrictEndTime)
{
// TODO: what about extended market hours
// Not using Exchange.Hours.RegularMarketDuration so we can handle things like early closes.
expectedPeriod = Exchange.Hours.GetMarketHours(potentialBarEndTimeInExchangeTZ).MarketDuration;
// Market could be closed at end time, so let's try with the start time to get the actual market duration.
if (expectedPeriod == TimeSpan.Zero)
// NOTE: Not using Exchange.Hours.RegularMarketDuration so we can handle things like early closes.
var marketHours = Exchange.Hours.GetMarketHours(nextFillForwardBarStartTime);
if (marketHours.MarketDuration == TimeSpan.Zero)
{
expectedPeriod = Exchange.Hours.GetMarketHours(nextFillForwardBarStartTime).MarketDuration;
// Start time belongs to an extended market hours only day (like a Sunday with a single post-market segment),
// so the potential end time must be the next date, let's try with that:
marketHours = Exchange.Hours.GetMarketHours(nextFillForwardBarStartTime.Date.AddDays(1));
}

// If market duration is still zero here, we are on an extended market hours only day followed by a closed day,
// this bar should be skipped, so a fill-forwarded bar will be emitted with period zero,
// which will be likely filtered out later on, but we still advance time here
// to ensure we keep fill-forwarding until the next data is reached
expectedPeriod = marketHours.MarketDuration;
}
fillForward.Time = (potentialBarEndTime - expectedPeriod).ConvertFromUtc(Exchange.TimeZone);
fillForward.EndTime = potentialBarEndTimeInExchangeTZ;
Expand Down
10 changes: 6 additions & 4 deletions Engine/DataFeeds/SubscriptionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public static SubscriptionData Create(bool dailyStrictEndTimeEnabled, Subscripti
// during warmup, data might be emitted with a different span based on the warmup resolution, so let's get the actual bar span here
var barSpan = data.EndTime - data.Time;
// rounding down does not make sense for daily increments using strict end times
if (!LeanData.UseDailyStrictEndTimes(configuration.Type) ||
!LeanData.UseStrictEndTime(dailyStrictEndTimeEnabled, configuration.Symbol, barSpan, exchangeHours))
if (!LeanData.UseDailyStrictEndTimes(dailyStrictEndTimeEnabled, configuration.Type, configuration.Symbol, barSpan, exchangeHours))
{
// Let's round down for any data source that implements a time delta between
// the start of the data and end of the data (usually used with Bars).
Expand Down Expand Up @@ -105,8 +104,11 @@ public static SubscriptionData Create(bool dailyStrictEndTimeEnabled, Subscripti
// the daily calendar will have the same time/end time so the bar times will not be adjusted.
// TODO: What about extended market hours? How to handle non-adjacent market hour segments in a day? Same in FillForwardEnumerator
var calendar = LeanData.GetDailyCalendar(data.Time, exchangeHours, false);
data.Time = calendar.Start;
data.EndTime = calendar.End;
if (calendar.Start.Date == data.Time.Date)
{
data.Time = calendar.Start;
data.EndTime = calendar.End;
}
}

if (factor.HasValue && (configuration.SecurityType != SecurityType.Equity || (factor.Value != 1 || configuration.SumOfDividends != 0)))
Expand Down

0 comments on commit 99f4920

Please sign in to comment.