{"record":{"id":"5afbed44dbaf9a00","repo":"QuantConnect/Lean","slug":"long-position-must-be-allowed-in-meanreversionport","errorCode":null,"errorMessage":"Long position must be allowed in MeanReversionPortfolioConstructionModel.","messagePattern":"Long position must be allowed in MeanReversionPortfolioConstructionModel\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithm.Framework/Portfolio/MeanReversionPortfolioConstructionModel.py","lineNumber":44,"sourceCode":"                 portfolioBias = PortfolioBias.LongShort,\n                 reversion_threshold = 1,\n                 window_size = 20,\n                 resolution = Resolution.Daily):\n        \"\"\"Initialize the model\n        Args:\n            rebalance: Rebalancing parameter. If it is a timedelta, date rules or Resolution, it will be converted into a function.\n                              If None will be ignored.\n                              The function returns the next expected rebalance time for a given algorithm UTC DateTime.\n                              The function returns null if unknown, in which case the function will be called again in the\n                              next loop. Returning current time will trigger rebalance.\n            portfolioBias: Specifies the bias of the portfolio (Short, Long/Short, Long)\n            reversion_threshold: Reversion threshold\n            window_size: Window size of mean price calculation\n            resolution: The resolution of the history price and rebalancing\n        \"\"\"\n        super().__init__()\n        if portfolioBias == PortfolioBias.Short:\n            raise ArgumentException(\"Long position must be allowed in MeanReversionPortfolioConstructionModel.\")\n            \n        self.reversion_threshold = reversion_threshold\n        self.window_size = window_size\n        self.resolution = resolution\n\n        self.num_of_assets = 0\n        # Initialize a dictionary to store stock data\n        self.symbol_data = {}\n\n        # If the argument is an instance of Resolution or Timedelta\n        # Redefine rebalancingFunc\n        rebalancingFunc = rebalance\n        if isinstance(rebalance, int):\n            rebalance = Extensions.ToTimeSpan(rebalance)\n        if isinstance(rebalance, timedelta):\n            rebalancingFunc = lambda dt: dt + rebalance\n        if rebalancingFunc:\n            self.SetRebalancingFunc(rebalancingFunc)","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Framework/Portfolio/MeanReversionPortfolioConstructionModel.py#L26-L62","documentation":"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.","triggerScenarios":"Instantiating MeanReversionPortfolioConstructionModel(..., portfolioBias=PortfolioBias.Short). The constructor checks portfolioBias == PortfolioBias.Short and raises ArgumentException immediately at setup.","commonSituations":"Copy-pasting a model instantiation from a short-biased strategy, or assuming PortfolioBias.Short means 'allow shorts' (it actually means long-only-is-forbidden).","solutions":["Pass PortfolioBias.Long (long-only) or PortfolioBias.LongShort (both sides) — the only valid values for this model.","If you genuinely need a short-only mean-reversion variant, subclass and invert the signal logic yourself instead of using the built-in model."],"exampleFix":"# before\nself.set_portfolio_construction(MeanReversionPortfolioConstructionModel(\n    portfolio_bias=PortfolioBias.Short))  # raises\n\n# after\nself.set_portfolio_construction(MeanReversionPortfolioConstructionModel(\n    portfolio_bias=PortfolioBias.LongShort))","handlingStrategy":"validation","validationCode":"from AlgorithmImports import *\n\nvalid = {PortfolioBias.Long, PortfolioBias.LongShort}\nif portfolio_bias not in valid:\n    raise ValueError('MeanReversionPortfolioConstructionModel requires Long or LongShort bias')\nmodel = MeanReversionPortfolioConstructionModel(portfolio_bias=portfolio_bias)","typeGuard":"def supports_mean_reversion(bias: PortfolioBias) -> bool:\n    return bias in (PortfolioBias.Long, PortfolioBias.LongShort)","tryCatchPattern":null,"preventionTips":["Remember PortfolioBias.Short forbids longs — most reversion models need longs.","Default to LongShort unless you specifically need long-only."],"tags":["portfolio-construction","configuration","portfolio-bias"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}