Lean icon indicating copy to clipboard operation
Lean copied to clipboard

The Schedule runs before the Consolidator's Callback regardless of the time

Open JosueNina opened this issue 9 months ago • 1 comments

Expected Behavior

The scheduled event at 1 AM should execute after the RSI indicator update, which occurs at midnight (12 AM)

Actual Behavior

The scheduled event at 1 AM executes before the RSI indicator update

Potential Solution

Reproducing the Problem

Executes this code:

namespace QuantConnect.Algorithm.CSharp
{
    public class CrawlingTanFrog : QCAlgorithm
    {
        private RelativeStrengthIndex _relativeStrengthIndex;
        private Symbol _spy;
        private bool _itWasUpdated;
        private int _dataPointCount;
        public override void Initialize()
        {
            SetStartDate(2013, 01, 01);
            SetEndDate(2013, 01, 05);
            _spy = AddEquity("SPY", Resolution.Hour).Symbol;
            _relativeStrengthIndex = new RelativeStrengthIndex(14, MovingAverageType.Wilders);
            RegisterIndicator(_spy, _relativeStrengthIndex, TimeSpan.FromDays(1));


            var history = History<TradeBar>(_spy, 20, Resolution.Daily).ToList();
            foreach (var bar in history)
            {
                _relativeStrengthIndex.Update(bar.EndTime, bar.Close);
            }
            if (!_relativeStrengthIndex.IsReady)
            {
                throw new RegressionTestException($"{_relativeStrengthIndex.Name} is not ready.");
            }
            _relativeStrengthIndex.Updated += (sender, data) =>
            {
                var updatedTime = Time;

                // RSI1 should update at midnight when precise end time is disabled
                if (updatedTime.TimeOfDay != new TimeSpan(0, 0, 0))
                {
                    throw new RegressionTestException($"{_relativeStrengthIndex.Name} must have updated at midnight, but it was updated at {updatedTime}");
                }

                _itWasUpdated = true;
            };

            _dataPointCount = 0;

            // 1 AM
            Schedule.On(DateRules.EveryDay(), TimeRules.At(1, 0, 0), TestSchedule);
        }

        private void TestSchedule()
        {
            // Expect that RSI was updated at midnight
            // Because the scheduler is set to run at 1 AM and the RSI was updated at midnight
            if (!_itWasUpdated && _dataPointCount > 0)
            {
                throw new RegressionTestException("The indicator was not updated.");
            }
        }

        public override void OnData(Slice slice)
        {
            _dataPointCount++;
            var time = Time;
        }
    }
}

System Information

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues
  • [x] I have provided detailed steps to reproduce the issue

JosueNina avatar Mar 25 '25 18:03 JosueNina

Related to https://github.com/QuantConnect/Lean/issues/5595

Martin-Molinero avatar Aug 15 '25 17:08 Martin-Molinero