QuantConnect/Lean · error · ArgumentException

Long position must be allowed in MeanReversionPortfolioConst

Error message

Long position must be allowed in MeanReversionPortfolioConstructionModel.

What it means

MeanReversionPortfolioConstructionModel (Python) rejects PortfolioBias.Short in its constructor. Mean-reversion requires the ability to take long positions (it buys oversold assets), so a short-only portfolio is logically incompatible with the model.

Source

Thrown at Algorithm.Framework/Portfolio/MeanReversionPortfolioConstructionModel.py:44

                 portfolioBias = PortfolioBias.LongShort,
                 reversion_threshold = 1,
                 window_size = 20,
                 resolution = Resolution.Daily):
        """Initialize the model
        Args:
            rebalance: Rebalancing parameter. If it is a timedelta, date rules or Resolution, it will be converted into a function.
                              If None will be ignored.
                              The function returns the next expected rebalance time for a given algorithm UTC DateTime.
                              The function returns null if unknown, in which case the function will be called again in the
                              next loop. Returning current time will trigger rebalance.
            portfolioBias: Specifies the bias of the portfolio (Short, Long/Short, Long)
            reversion_threshold: Reversion threshold
            window_size: Window size of mean price calculation
            resolution: The resolution of the history price and rebalancing
        """
        super().__init__()
        if portfolioBias == PortfolioBias.Short:
            raise ArgumentException("Long position must be allowed in MeanReversionPortfolioConstructionModel.")
            
        self.reversion_threshold = reversion_threshold
        self.window_size = window_size
        self.resolution = resolution

        self.num_of_assets = 0
        # Initialize a dictionary to store stock data
        self.symbol_data = {}

        # If the argument is an instance of Resolution or Timedelta
        # Redefine rebalancingFunc
        rebalancingFunc = rebalance
        if isinstance(rebalance, int):
            rebalance = Extensions.ToTimeSpan(rebalance)
        if isinstance(rebalance, timedelta):
            rebalancingFunc = lambda dt: dt + rebalance
        if rebalancingFunc:
            self.SetRebalancingFunc(rebalancingFunc)

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Pass PortfolioBias.Long (long-only) or PortfolioBias.LongShort (both sides) — the only valid values for this model.
  2. If you genuinely need a short-only mean-reversion variant, subclass and invert the signal logic yourself instead of using the built-in model.

Example fix

# before
self.set_portfolio_construction(MeanReversionPortfolioConstructionModel(
    portfolio_bias=PortfolioBias.Short))  # raises

# after
self.set_portfolio_construction(MeanReversionPortfolioConstructionModel(
    portfolio_bias=PortfolioBias.LongShort))
Defensive patterns

Strategy: validation

Validate before calling

from AlgorithmImports import *

valid = {PortfolioBias.Long, PortfolioBias.LongShort}
if portfolio_bias not in valid:
    raise ValueError('MeanReversionPortfolioConstructionModel requires Long or LongShort bias')
model = MeanReversionPortfolioConstructionModel(portfolio_bias=portfolio_bias)

Type guard

def supports_mean_reversion(bias: PortfolioBias) -> bool:
    return bias in (PortfolioBias.Long, PortfolioBias.LongShort)

Prevention

When it happens

Trigger: Instantiating MeanReversionPortfolioConstructionModel(..., portfolioBias=PortfolioBias.Short). The constructor checks portfolioBias == PortfolioBias.Short and raises ArgumentException immediately at setup.

Common situations: Copy-pasting a model instantiation from a short-biased strategy, or assuming PortfolioBias.Short means 'allow shorts' (it actually means long-only-is-forbidden).

Related errors


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