{"record":{"id":"c42d45b52d22b440","repo":"QuantConnect/Lean","slug":"regression-test-failed-current-open-interest-was","errorCode":null,"errorMessage":"Regression test failed: current open interest was not correctly loaded and is not equal to 50","messagePattern":"Regression test failed: current open interest was not correctly loaded and is not equal to 50","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/OptionOpenInterestRegressionAlgorithm.py","lineNumber":53,"sourceCode":"\n    def on_data(self, slice):\n        if not self.portfolio.invested:\n            for chain in slice.option_chains:\n                for contract in chain.value:\n                    if float(contract.symbol.id.strike_price) == 72.5 and \\\n                       contract.symbol.id.option_right == OptionRight.CALL and \\\n                       contract.symbol.id.date == datetime(2016, 1, 15):\n\n                        history = self.history(OpenInterest, contract.symbol, timedelta(1))[\"openinterest\"]\n                        if len(history.index) == 0 or 0 in history.values:\n                            raise ValueError(\"Regression test failed: open interest history request is empty\")\n\n                        security = self.securities[contract.symbol]\n                        open_interest_cache = security.cache.get_data(OpenInterest)\n                        if open_interest_cache == None:\n                            raise ValueError(\"Regression test failed: current open interest isn't in the security cache\")\n                        if slice.time.date() == datetime(2014, 6, 5).date() and (contract.open_interest != 50 or security.open_interest != 50):\n                            raise ValueError(\"Regression test failed: current open interest was not correctly loaded and is not equal to 50\")\n                        if slice.time.date() == datetime(2014, 6, 6).date() and (contract.open_interest != 70 or security.open_interest != 70):\n                            raise ValueError(\"Regression test failed: current open interest was not correctly loaded and is not equal to 70\")\n                        if slice.time.date() == datetime(2014, 6, 6).date():\n                            self.market_order(contract.symbol, 1)\n                            self.market_on_close_order(contract.symbol, -1)\n\n                if all(contract.open_interest == 0 for contract in chain.value):\n                    raise ValueError(\"Regression test failed: open interest is zero for all contracts\")\n\n    def on_order_event(self, order_event):\n        self.log(str(order_event))\n","sourceCodeStart":35,"sourceCodeEnd":65,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/OptionOpenInterestRegressionAlgorithm.py#L35-L65","documentation":"Per-date open-interest value check for 2014-06-05: both contract.open_interest and security.open_interest must equal 50. If either differs, the open-interest value loaded from the data file (or cached) for that date is wrong, indicating a data or cache-value regression for that specific contract.","triggerScenarios":"slice.time.date() == 2014-06-05 and (contract.open_interest != 50 or security.open_interest != 50).","commonSituations":"The option open-interest data file for 2014-06-05 changed; the cache returns a stale/wrong value; contract.open_interest and security.open_interest diverge (cache vs chain contract object out of sync).","solutions":["Inspect the option open-interest data file for the contract on 2014-06-05; it must resolve to 50.","Check that the OptionContract.open_interest reflects the latest cached value and that security.open_interest reads the same cache.","Ensure the open-interest subscription updates the cache before the chain contract is built."],"exampleFix":"// before: chain contract and security cache out of sync\ncontract.open_interest;  // stale\n// after: both read from the same cache point\nsecurity.SetOpenInterest(contract.open_interest);","handlingStrategy":"validation","validationCode":"# check both sources agree and equal the expected value for the date\nexpected = 50 if slice.time.date() == datetime(2014,6,5).date() else None\nif expected is not None and (contract.open_interest != expected or security.open_interest != expected):\n    self.debug(f\"OI mismatch on {slice.time.date()}: contract={contract.open_interest} security={security.open_interest} expected={expected}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep chain contract and security cache open-interest in sync.","Pin expected per-date values and log both sources on mismatch.","Validate the underlying data file values for the regression dates."],"tags":["quantconnect","lean","regression","options","open-interest","security-cache","data-quality"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}