{"record":{"id":"043ce6b2205f775a","repo":"QuantConnect/Lean","slug":"expected-greeks-to-be-calculated-for-contract-sym","errorCode":null,"errorMessage":"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","messagePattern":"Expected greeks to be calculated for (.+?), an (.+?) style option, using (.+?), which supports them, but they were not","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/OptionPriceModelForOptionStylesBaseRegressionAlgorithm.py","lineNumber":70,"sourceCode":"        if not self._check_greeks or len(contracts) == 0 or not self._option:\n            return\n\n        self._check_greeks = False\n        self._tried_greeks_calculation = True\n\n        for contract in contracts:\n            greeks = None\n            try:\n                greeks = contract.greeks\n\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":52,"sourceCodeEnd":84,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/OptionPriceModelForOptionStylesBaseRegressionAlgorithm.py#L52-L84","documentation":"The complement of 178: raised in the except ArgumentException handler when the style IS supported but accessing contract.greeks threw ArgumentException anyway. It proves a supported style/model combination must succeed. CAVEAT: option_style_str is assigned inside the try (line 64) AFTER the throwing statement (line 61); if contract.greeks throws, option_style_str is undefined and this raise line itself raises NameError before the intended AssertionError message. The root cause it targets: a price model incorrectly rejecting a style it should support.","triggerScenarios":"self._option_style_is_supported is True, but contract.greeks threw ArgumentException. Execution enters the except block and the supported-style branch raises (or, due to the scoping bug, raises NameError on option_style_str).","commonSituations":"The price model's style-support check became too strict (rejecting a supported style); the option was created with the wrong style for the model; a refactor inverted the Supports() result.","solutions":["Inspect the price model's SupportFor/style guard; ensure it does not throw for styles it supports.","Confirm the regression subclass declared option_style_is_supported consistently with the model's actual support matrix.","Fix the latent bug: move option_style_str assignment before the try (or outside it) so the error message is constructible when contract.greeks throws early.","Verify the option's OptionStyle matches what the model is expected to price."],"exampleFix":"# before: option_style_str only defined inside try, undefined if greeks throws\ntry:\n    greeks = contract.greeks\n    option_style_str = 'American' if ... else 'European'\n    ...\nexcept ArgumentException:\n    raise AssertionError(f'... {option_style_str} ...')  # NameError risk\n# after: compute the label before the try so the message is always valid\noption_style_str = 'American' if self._option.style == OptionStyle.AMERICAN else 'European'\ntry:\n    greeks = contract.greeks\nexcept ArgumentException:\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')","handlingStrategy":"try-catch","validationCode":"# preflight: a supported style must compute greeks without throwing\nif self._option_style_is_supported:\n    try:\n        contract.greeks\n    except ArgumentException as e:\n        self.debug(f\"supported style unexpectedly rejected: {e}\")","typeGuard":"def style_is_supported(price_model, style) -> bool:\n    return price_model.supported_styles(style)","tryCatchPattern":"# compute the label OUTSIDE the try so the error message is always buildable\noption_style_str = 'American' if self._option.style == OptionStyle.AMERICAN else 'European'\ntry:\n    greeks = contract.greeks\nexcept ArgumentException:\n    if self._option_style_is_supported:\n        raise AssertionError(f'Expected greeks for {contract.symbol.value} ({option_style_str}, {type(self._option.price_model).__name__}), but they were not')","preventionTips":["Move any variable referenced in an except handler out of the try so it is defined even when the throwing statement fails early.","Keep the model's Supports() result aligned with the regression's declared flag.","Test both supported and unsupported style paths after any pricing-model change."],"tags":["quantconnect","lean","regression","options","greeks","option-pricing","option-style","exception-handling"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}