QuantConnect/Lean · error · ValueError

Unexpected distribution: {distribution}

Error message

Unexpected distribution: {distribution}

What it means

Per-row sanity check on the AAPL dividend history: every distribution value must be non-zero. A zero distribution indicates malformed dividend data (a dividend row with a 0 amount) or a deserialization bug that zeroed the distribution field.

Source

Thrown at Algorithm.Python/HistoryAuxiliaryDataRegressionAlgorithm.py:50

        if len(multi_symbol_request) != 12:
                raise ValueError(f"Unexpected multi symbol dividend count: {len(multi_symbol_request)}")

        # continuous future mapping requests
        sp500 = Symbol.create(Futures.Indices.SP_500_E_MINI, SecurityType.FUTURE, Market.CME)
        continuous_future_open_interest_mapping = self.history(SymbolChangedEvent, sp500, datetime(2007, 1, 1), datetime(2012, 1, 1), data_mapping_mode = DataMappingMode.OPEN_INTEREST)
        if len(continuous_future_open_interest_mapping) != 9:
                raise ValueError(f"Unexpected continuous future mapping event count: {len(continuous_future_open_interest_mapping)}")
        continuous_future_last_trading_day_mapping = self.history(SymbolChangedEvent, sp500, datetime(2007, 1, 1), datetime(2012, 1, 1), data_mapping_mode = DataMappingMode.LAST_TRADING_DAY)
        if len(continuous_future_last_trading_day_mapping) != 9:
                raise ValueError(f"Unexpected continuous future mapping event count: {len(continuous_future_last_trading_day_mapping)}")

        dividend = self.history(Dividend, aapl, 360)
        self.debug(str(dividend))
        if len(dividend) != 6:
            raise ValueError(f"Unexpected dividend count: {len(dividend)}")
        for distribution in dividend.distribution:
            if distribution == 0:
                raise ValueError(f"Unexpected distribution: {distribution}")

        split = self.history(Split, aapl, 360)
        self.debug(str(split))
        if len(split) != 2:
            raise ValueError(f"Unexpected split count: {len(split)}")
        for splitfactor in split.splitfactor:
            if splitfactor == 0:
                raise ValueError(f"Unexpected splitfactor: {splitfactor}")

        symbol = Symbol.create("BTCUSD", SecurityType.CRYPTO_FUTURE, Market.BINANCE)
        margin_interest = self.history(MarginInterestRate, symbol, 24 * 3, Resolution.HOUR)
        self.debug(str(margin_interest))
        if len(margin_interest) != 8:
            raise ValueError(f"Unexpected margin interest count: {len(margin_interest)}")
        for interestrate in margin_interest.interestrate:
            if interestrate == 0:
                raise ValueError(f"Unexpected interestrate: {interestrate}")

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Inspect AAPL factor file rows in the window for zero or malformed distribution values.
  2. Trace Dividend.Distribution population in the dividend reader/factor provider.
  3. Correct or remove the bad data row and re-run.

Example fix

# before: malformed factor row {time; priceFactor; splitFactor; 0 distribution}
# after: corrected distribution value
{20200713;1.0;1.0;0.205}
Defensive patterns

Strategy: validation

Validate before calling

div = self.history(Dividend, aapl, 360)
bad = [d for d in div.distribution if d == 0]
if bad:
    self.debug(f"zero dividend distributions: {len(bad)}")

Type guard

def has_nonzero_distributions(div_history) -> bool:
    return all(d != 0 for d in div_history.distribution)

Prevention

When it happens

Trigger: Iterating dividend.distribution, a value equals 0.0.

Common situations: A factor file row has a zero/blank distribution that was parsed as 0; a dividend refactor changed how Distribution is populated; data corruption.

Related errors


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