{"record":{"id":"8d6a3819e3f9a7d1","repo":"ZhuLinsen/daily_stock_analysis","slug":"longbridge-quotecontext-not-available","errorCode":null,"errorMessage":"Longbridge QuoteContext not available","messagePattern":"Longbridge QuoteContext not available","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"data_provider/longbridge_fetcher.py","lineNumber":881,"sourceCode":"\n    # ------------------------------------------------------------------\n    # BaseFetcher abstract methods (historical daily data)\n    # ------------------------------------------------------------------\n\n    def _fetch_raw_data(\n        self, stock_code: str, start_date: str, end_date: str\n    ) -> pd.DataFrame:\n        \"\"\"Fetch historical candlesticks from Longbridge.\"\"\"\n        if not self.is_available_for_request(\"daily_data\"):\n            raise RuntimeError(\"Longbridge temporarily unavailable for daily_data\")\n\n        symbol = _to_longbridge_symbol(stock_code)\n        if symbol is None:\n            raise ValueError(f\"Cannot convert {stock_code} to Longbridge symbol\")\n\n        ctx = self._get_ctx()\n        if ctx is None:\n            raise RuntimeError(\"Longbridge QuoteContext not available\")\n\n        from longbridge.openapi import Period, AdjustType\n\n        start_dt = datetime.strptime(start_date, \"%Y-%m-%d\").date()\n        end_dt = datetime.strptime(end_date, \"%Y-%m-%d\").date()\n\n        try:\n            candles = ctx.history_candlesticks_by_date(\n                symbol,\n                Period.Day,\n                AdjustType.ForwardAdjust,\n                start_dt,\n                end_dt,\n            )\n        except Exception as e:\n            if self._is_connection_error(e):\n                self._mark_connection_cooldown(e)\n            raise","sourceCodeStart":863,"sourceCodeEnd":899,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/data_provider/longbridge_fetcher.py#L863-L899","documentation":"Raised when LongbridgeFetcher._get_ctx() returns None, i.e. a Longbridge QuoteContext could not be created. This almost always means the Longbridge SDK is not installed or credentials (LONGBRIDGE_APP_KEY / LONGBRIDGE_APP_SECRET / LONGBRIDGE_ACCESS_TOKEN) are missing/invalid, so the quote context was never initialized.","triggerScenarios":"Calling _fetch_raw_data on LongbridgeFetcher after the context initialization failed or was skipped: SDK import failure, missing token env vars, or an earlier failed _get_ctx() that left the context as None.","commonSituations":"Deploying without Longbridge credentials in .env; LongbridgeFetcher left in the data source chain although the account has no API access; SDK version change breaking context creation; token expired/revoked.","solutions":["Set the Longbridge credentials (app key, app secret, access token) in the environment/.env and confirm the longbridge package is installed.","Call the fetcher's availability check (is_available_for_request / credentials probe) before routing requests to LongbridgeFetcher, and drop it from the provider chain when unavailable.","If the context creation previously failed, investigate the underlying error logged during initialization instead of retrying blindly."],"exampleFix":"# before\ndf = longbridge_fetcher.get_stock_data(\"00700.HK\", start, end)  # RuntimeError: QuoteContext not available\n\n# after\nif longbridge_fetcher._get_ctx() is not None:\n    df = longbridge_fetcher.get_stock_data(\"00700.HK\", start, end)\nelse:\n    df = manager.get_stock_data(\"00700.HK\", start, end)  # fallback provider","handlingStrategy":"validation","validationCode":"ctx = longbridge_fetcher._get_ctx()\nif ctx is None:\n    logger.warning(\"Longbridge unavailable; skipping provider\")\n    # route to manager fallback instead of calling the fetcher","typeGuard":null,"tryCatchPattern":"try:\n    df = longbridge_fetcher.get_stock_data(code, start, end)\nexcept RuntimeError as e:\n    if \"QuoteContext not available\" in str(e):\n        df = manager.get_stock_data(code, start, end)\n    else:\n        raise","preventionTips":["Set Longbridge credentials via environment and document them in .env.example.","Probe provider availability at startup and log which providers are active.","Remove unconfigured providers from the priority chain instead of letting them fail per request."],"tags":["longbridge","credentials","configuration","runtime-context"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}