QuantConnect / QuantConnect/Lean

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

Open
#8,649 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug depth
Dominant language
C#
Stars
21.7k
Forks
5.3k
Avg merge
2d 22h
Merged PRs (30d)
34

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduced C# algorithm's Initialize method and trace the ordering between the RSI consolidator callback, OnData, and Schedule.On/TestSchedule. Run the provided reproduction to confirm the 1 AM callback occurs before the midnight indicator update. Done means the schedule observes the prior RSI update and the reproduction no longer raises its regression exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.