Skip to content

Commit 0ea3ff9

Browse files
committed
Align algorithm time to StartDate before OnWarmupFinished fires
1 parent 2996695 commit 0ea3ff9

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

Engine/AlgorithmManager.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -727,18 +727,6 @@ private IEnumerable<TimeSlice> Stream(IAlgorithm algorithm, ISynchronizer synchr
727727
{
728728
// warmup finished, send an update
729729
warmingUp = false;
730-
731-
// Align time to StartDate so OnWarmupFinished always fires at midnight,
732-
// even when the first post warmup slice arrives later (e.g. a ScheduledUniverse at 8 AM).
733-
if (!algorithm.LiveMode)
734-
{
735-
var warmupEndUtc = algorithm.StartDate.ConvertToUtc(algorithm.TimeZone);
736-
if (algorithm.UtcTime > warmupEndUtc)
737-
{
738-
algorithm.SetDateTime(warmupEndUtc);
739-
}
740-
}
741-
742730
// we trigger this callback here and not internally in the algorithm so that we can go through python if required
743731
algorithm.OnWarmupFinished();
744732
algorithm.Debug("Algorithm finished warming up.");

Engine/DataFeeds/Synchronizer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace QuantConnect.Lean.Engine.DataFeeds
3030
public class Synchronizer : ISynchronizer, IDataFeedTimeProvider, IDisposable
3131
{
3232
private DateTimeZone _dateTimeZone;
33+
private DateTime _warmupEndUtc;
3334

3435
/// <summary>
3536
/// The algorithm instance
@@ -116,6 +117,13 @@ public virtual IEnumerable<TimeSlice> StreamData(CancellationToken cancellationT
116117
// check for cancellation
117118
if (timeSlice == null || cancellationToken.IsCancellationRequested) break;
118119

120+
// If the first post warmup slice skips past StartDate, emit a time pulse at StartDate
121+
// so the algorithm time is aligned before OnWarmupFinished fires
122+
if (!Algorithm.LiveMode && Algorithm.IsWarmingUp && timeSlice.Time > _warmupEndUtc)
123+
{
124+
yield return TimeSliceFactory.CreateTimePulse(_warmupEndUtc);
125+
}
126+
119127
if (timeSlice.IsTimePulse && Algorithm.UtcTime == timeSlice.Time)
120128
{
121129
previousWasTimePulse = timeSlice.IsTimePulse;
@@ -167,6 +175,7 @@ protected virtual void PostInitialize()
167175

168176
// this is set after the algorithm initializes
169177
_dateTimeZone = Algorithm.TimeZone;
178+
_warmupEndUtc = Algorithm.StartDate.ConvertToUtc(_dateTimeZone);
170179
TimeSliceFactory = new TimeSliceFactory(_dateTimeZone);
171180
SubscriptionSynchronizer.SetTimeSliceFactory(TimeSliceFactory);
172181
}

0 commit comments

Comments
 (0)