{"record":{"id":"dfcc28f603e71d68","repo":"QuantConnect/Lean","slug":"long-position-must-be-allowed-in-riskparityportfol","errorCode":null,"errorMessage":"Long position must be allowed in RiskParityPortfolioConstructionModel.","messagePattern":"Long position must be allowed in RiskParityPortfolioConstructionModel\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithm.Framework/Portfolio/RiskParityPortfolioConstructionModel.py","lineNumber":44,"sourceCode":"                 lookback = 1,\n                 period = 252,\n                 resolution = Resolution.DAILY,\n                 optimizer = None):\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            portfolio_bias: Specifies the bias of the portfolio (Short, Long/Short, Long)\n            lookback(int): Historical return lookback period\n            period(int): The time interval of history price to calculate the weight\n            resolution: The resolution of the history price\n            optimizer(class): Method used to compute the portfolio weights\"\"\"\n        super().__init__()\n        if portfolio_bias == PortfolioBias.SHORT:\n            raise ArgumentException(\"Long position must be allowed in RiskParityPortfolioConstructionModel.\")\n\n        self.lookback = lookback\n        self.period = period\n        self.resolution = resolution\n        self.sign = lambda x: -1 if x < 0 else (1 if x > 0 else 0)\n\n        self.optimizer = RiskParityPortfolioOptimizer() if optimizer is None else optimizer\n\n        self._symbol_data_by_symbol = {}\n\n        # If the argument is an instance of Resolution or Timedelta\n        # Redefine rebalancing_func\n        rebalancing_func = rebalance\n        if isinstance(rebalance, int):\n            rebalance = Extensions.to_time_span(rebalance)\n        if isinstance(rebalance, timedelta):\n            rebalancing_func = lambda dt: dt + rebalance\n        if rebalancing_func:","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Framework/Portfolio/RiskParityPortfolioConstructionModel.py#L26-L62","documentation":"RiskParityPortfolioConstructionModel (Python) rejects PortfolioBias.SHORT. Risk parity balances risk contributions, which requires long exposure; a short-only configuration is incompatible with the model's risk-budgeting math, so the constructor raises ArgumentException.","triggerScenarios":"Constructing RiskParityPortfolioConstructionModel(..., portfolio_bias=PortfolioBias.SHORT). Note this model checks the SHORT (uppercase) attribute name, unlike the camelCase variant in some sibling models.","commonSituations":"Assuming PortfolioBias.SHORT permits shorts (it forbids longs), or porting a short-biased config from another model.","solutions":["Use PortfolioBias.LONG or PortfolioBias.LONGSHORT with this model.","For a short-tilted risk strategy, build a custom model rather than forcing the built-in risk-parity model into short-only."],"exampleFix":"# before\nself.set_portfolio_construction(RiskParityPortfolioConstructionModel(\n    portfolio_bias=PortfolioBias.SHORT))  # raises\n\n# after\nself.set_portfolio_construction(RiskParityPortfolioConstructionModel(\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('RiskParityPortfolioConstructionModel requires Long or LongShort bias')\nmodel = RiskParityPortfolioConstructionModel(portfolio_bias=portfolio_bias)","typeGuard":"def supports_risk_parity(bias: PortfolioBias) -> bool:\n    return bias in (PortfolioBias.LONG, PortfolioBias.LONGSHORT)","tryCatchPattern":null,"preventionTips":["PortfolioBias.SHORT forbids longs — risk parity needs long exposure.","Mind the attribute casing: this model checks portfolio_bias, not portfolioBias."],"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"}