QuantConnect/Lean · error · RegressionTestException

There should not be more than 5 data points, but there were

Error message

There should not be more than 5 data points, but there were {_dataPoints}

What it means

Asserts the custom ISecurityDataFilter actually reduced the feed: with SPY minute data on 2013-10-07, the filter drops everything at/after 09:35, leaving 5 points (09:30-09:34). If OnData fires more than 5 times, the filter was not applied and unfiltered bars reached the algorithm.

Source

Thrown at Algorithm.CSharp/CustomSecurityDataFilterRegressionAlgorithm.cs:50

        public override void Initialize()
        {
            SetCash(2500000);
            SetStartDate(2013, 10, 7);
            SetEndDate(2013, 10, 7);

            var security = AddSecurity(SecurityType.Equity, "SPY");
            security.SetDataFilter(new CustomDataFilter());
            _dataPoints = 0;
        }

        public override void OnData(Slice slice)
        {
            _dataPoints++;
            SetHoldings("SPY", 0.2);
            if (_dataPoints > 5)
            {
                throw new RegressionTestException($"There should not be more than 5 data points, but there were {_dataPoints}");
            }
        }

        private class CustomDataFilter : ISecurityDataFilter
        {
            public bool Filter(Security vehicle, BaseData data)
            {
                // Skip data after 9:35am
                if (data.Time >= new DateTime(2013, 10, 7, 9, 35, 0, 0))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Call SetDataFilter on the exact Security returned by AddSecurity/AddEquity and store that reference.
  2. Make the Filter comparison use exchange-local time (data.Time is already in the exchange zone for most feeds) consistent with the 09:35 threshold.
  3. Verify the CustomDataFilter implements ISecurityDataFilter.Filter(Security, BaseData) and returns false for data.Time >= 2013-10-07 09:35.
  4. Confirm data resolution is Minute (so the 09:30-09:34 five-bar expectation holds); a coarser resolution changes the count.

Example fix

// before: filter set on a throwaway security reference
AddEquity("SPY").SetDataFilter(new CustomDataFilter());

// after: keep and configure the same instance
var security = AddSecurity(SecurityType.Equity, "SPY");
security.SetDataFilter(new CustomDataFilter());
Defensive patterns

Strategy: validation

Validate before calling

// Validate the filter is installed and reduces the feed
var security = AddEquity("SPY", Resolution.Minute);
security.SetDataFilter(new CustomDataFilter());
Debug.Assert(security.Subscriptions.First().Filter is CustomDataFilter);

Type guard

bool FilterInstalled(Security s) => s.Subscriptions.Any(x => x.Filter is CustomDataFilter);

Prevention

When it happens

Trigger: Calling security.SetDataFilter(new CustomDataFilter()) in Initialize but still receiving >5 OnData calls, indicating SetDataFilter did not register, or the filter's Filter returned true for post-09:35 bars.

Common situations: SetDataFilter called on a different Security instance than the one feeding OnData; filter time comparison uses the wrong time zone (UTC vs exchange) so data.Time never reaches the threshold; or a Lean version change altered when filters run in the pipeline.

Related errors


AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13). Data as JSON: /api/errors/c639456a4d96fbb9. Report an issue: GitHub.