{"record":{"id":"cf0541684cc1147b","repo":"QuantConnect/Lean","slug":"regression-test-failed-current-open-interest-isn","errorCode":null,"errorMessage":"Regression test failed: current open interest isn't in the security cache","messagePattern":"Regression test failed: current open interest isn't in the security cache","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/OptionOpenInterestRegressionAlgorithm.py","lineNumber":51,"sourceCode":"        # use the underlying equity as the benchmark\n        self.set_benchmark(\"TWX\")\n\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":33,"sourceCodeEnd":65,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/OptionOpenInterestRegressionAlgorithm.py#L33-L65","documentation":"Option-open-interest regression: after fetching OpenInterest history for a specific ES-style option contract (72.5 call, 2016-01-15 expiry), it reads the security cache and asserts the current OpenInterest is cached. security.cache.get_data(OpenInterest) returning None means the open-interest data point was never registered into the security's cache, indicating the open-interest subscription/cache registration path regressed.","triggerScenarios":"Inside on_data for the matching contract, security.cache.get_data(OpenInterest) == None. This fires before the per-date value checks (50/70), so the cache is empty.","commonSituations":"A refactor of Security.cache or the open-interest data registration in the data feed; open-interest subscription type no longer populating the cache; the option chain filter changed so the contract isn't actually subscribed.","solutions":["Confirm the contract (72.5 call, 2016-01-15) is being subscribed/added (it must be in the chain and pass the filter).","Inspect SecurityCache.GetData / RegisterNewData and the open-interest subscription registration in the data feed; ensure OpenInterest points are stored in the cache.","Verify the open-interest data files for the option exist for 2014-06-05/06.","Trace BaseDataCache for the OpenInterest type registration."],"exampleFix":"// before: open interest not registered into the security cache\nfeed.Subscribe(contract, ...);  // OpenInterest type omitted\n// after: ensure open-interest data is cached\nsecurity.Cache.AddData(openInterestPoint);","handlingStrategy":"validation","validationCode":"# confirm the contract is subscribed and cache populated before asserting\nsecurity = self.securities[contract.symbol]\nif security.cache.get_data(OpenInterest) is None:\n    self.debug(f\"no open-interest in cache for {contract.symbol}; subscribed={contract.symbol in self.portfolio.keys()}\")","typeGuard":"def cache_has_open_interest(algo, symbol) -> bool:\n    sec = algo.securities[symbol]\n    return sec.cache.get_data(OpenInterest) is not None","tryCatchPattern":null,"preventionTips":["Ensure the option contract passes the chain filter so it is subscribed.","Confirm the OpenInterest data type is registered for the option subscription.","Read open interest from the security cache consistently, not from a stale chain object."],"tags":["quantconnect","lean","regression","options","open-interest","security-cache","data-feed"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}