Skip to content

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

Description

@JosueNina

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions