{"record":{"id":"febdfcbd9eb11a93","repo":"QuantConnect/Lean","slug":"expected-greeks-to-have-valid-values-greeks-were","errorCode":null,"errorMessage":"Expected greeks to have valid values. Greeks were: Delta: {greeks.delta}, Rho: {greeks.rho}, Theta: {greeks.theta}, Vega: {greeks.vega}, Gamma: {greeks.gamma}","messagePattern":"Expected greeks to have valid values\\. Greeks were: Delta: (.+?), Rho: (.+?), Theta: (.+?), Vega: (.+?), Gamma: (.+?)","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/OptionPriceModelForOptionStylesBaseRegressionAlgorithm.py","lineNumber":80,"sourceCode":"\n                # Greeks should have not been successfully accessed if the option style is not supported\n                option_style_str = 'American' if self._option.style == OptionStyle.AMERICAN else 'European'\n                if not self._option_style_is_supported:\n                    raise AssertionError(f'Expected greeks not to be calculated for {contract.symbol.value}, an {option_style_str} style option, using {type(self._option.price_model).__name__}, which does not support them, but they were')\n            except ArgumentException:\n                # ArgumentException is only expected if the option style is not supported\n                if self._option_style_is_supported:\n                    raise AssertionError(f'Expected greeks to be calculated for {contract.symbol.value}, an {option_style_str} style option, using {type(self._option.price_model).__name__}, which supports them, but they were not')\n\n            # Greeks should be valid if they were successfuly accessed for supported option style\n            # Delta can be {-1, 0, 1} if the price is too wild, rho can be 0 if risk free rate is 0\n            # Vega can be 0 if the price is very off from theoretical price, Gamma = 0 if Delta belongs to {-1, 1}\n            if (self._option_style_is_supported\n                and (not greeks\n                    or ((contract.right == OptionRight.CALL and (greeks.delta < 0.0 or greeks.delta > 1.0 or greeks.rho < 0.0))\n                        or (contract.right == OptionRight.PUT and (greeks.delta < -1.0 or greeks.delta > 0.0 or greeks.rho > 0.0))\n                        or greeks.theta == 0.0 or greeks.vega < 0.0 or greeks.gamma < 0.0))):\n                raise AssertionError(f'Expected greeks to have valid values. Greeks were: Delta: {greeks.delta}, Rho: {greeks.rho}, Theta: {greeks.theta}, Vega: {greeks.vega}, Gamma: {greeks.gamma}')\n\n\n\n","sourceCodeStart":62,"sourceCodeEnd":84,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/OptionPriceModelForOptionStylesBaseRegressionAlgorithm.py#L62-L84","documentation":"This assertion fires inside a QuantConnect Lean regression test that validates an option price model (e.g. BlackScholes, BjerksundStensland) actually produced mathematically sane Greeks for a contract whose option style the model claims to support. Lean exposes Greeks via contract.greeks; for supported styles the engine must return a populated Greek object whose values fall within theoretically valid ranges. The check enforces CALL delta in [0,1] with rho>=0, PUT delta in [-1,0] with rho<=0, theta!=0, vega>=0, gamma>=0, because a supported model returning out-of-range Greeks signals a pricing or data-feeding bug rather than a legitimate edge case.","triggerScenarios":"Accessing contract.greeks for a contract whose option style is marked supported (self._option_style_is_supported == True) when at least one Greek violates the bounds: e.g. a CALL whose delta < 0 or > 1, a CALL with rho < 0, a PUT with delta < -1 or > 0, a PUT with rho > 0, theta exactly 0.0, vega < 0, or gamma < 0. The value `greeks` being None/falsy also triggers it.","commonSituations":"A Lean engine change to the price model math that miscalculates a Greek; switching the risk-free rate feed so rho crosses zero unexpectedly; feeding stale or missing implied-volatility data so the model extrapolates; or running the regression against a data package whose underlying prices are wildly off from theoretical, causing delta to clip to +/-1 plus sign errors.","solutions":["Inspect the printed Greek values to identify which bound was violated (delta sign/range, rho sign, theta==0, vega<0, gamma<0) and correlate it with the contract.right.","Verify the option price model set via option.set_price_model(...) actually supports the contract's OptionStyle (American vs European); an unsupported style should raise ArgumentException earlier, not yield bad Greeks.","Check the underlying price and implied volatility feeds for the regression date; stale/NaN IV or a bad spot causes the model to return degenerate Greeks.","If you are editing the pricing model in Lean C#, diff the Greek calculation against the known-good prior output and re-run the regression after the fix."],"exampleFix":"// before: model returns CALL delta = -0.15\n// after (correct pricing-model fix in C#): ensure delta sign convention matches contract.right\n// In the test, narrow only what you intentionally assert:\nif self._option_style_is_supported and (not greeks or greeks.delta is None):\n    raise AssertionError('Greeks not populated for supported style')","handlingStrategy":"validation","validationCode":"# Before trusting greeks, validate the price model supports the style and bounds-check each Greek\ndef safe_greeks(contract, option):\n    if not option.price_model.supports_style(option.style):\n        return None\n    g = contract.greeks\n    if g is None or any(v is None for v in (g.delta, g.rho, g.theta, g.vega, g.gamma)):\n        return None\n    lo, hi = (0.0, 1.0) if contract.right == OptionRight.CALL else (-1.0, 0.0)\n    if not (lo <= g.delta <= hi):\n        return None\n    return g","typeGuard":"from QuantConnect.Data.Market import Greeks\n\ndef greeks_are_valid(greeks, right) -> bool:\n    if greeks is None:\n        return False\n    if right == OptionRight.CALL:\n        if not (0.0 <= greeks.delta <= 1.0) or greeks.rho < 0.0:\n            return False\n    else:\n        if not (-1.0 <= greeks.delta <= 0.0) or greeks.rho > 0.0:\n            return False\n    return greeks.theta != 0.0 and greeks.vega >= 0.0 and greeks.gamma >= 0.0","tryCatchPattern":null,"preventionTips":["Only access contract.greeks for option styles the configured price model supports.","Sanity-check Greek ranges before using them in sizing/risk logic, not just in tests.","Feed clean underlying price and implied-volatility data; degenerate inputs produce degenerate Greeks.","When changing a Lean pricing model, re-run the style/greeks regression before merging."],"tags":["options","greeks","regression-test","quantconnect","pricing-model"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}