{"record":{"id":"5f1dca3aca2251ab","repo":"ZhuLinsen/daily_stock_analysis","slug":"pytdx-stock-code","errorCode":null,"errorMessage":"Pytdx 未查询到 {stock_code} 的数据","messagePattern":"Pytdx 未查询到 (.+?) 的数据","errorType":"exception","errorClass":"DataFetchError","httpStatus":null,"severity":"error","filePath":"data_provider/pytdx_fetcher.py","lineNumber":360,"sourceCode":"        days = (end_dt - start_dt).days\n        count = min(max(days * 5 // 7 + 10, 30), 800)  # 估算交易日，最大 800 条\n        \n        logger.debug(f\"调用 Pytdx get_security_bars(market={market}, code={code}, count={count})\")\n        \n        with self._pytdx_session() as api:\n            try:\n                # 获取日 K 线数据\n                # category: 9-日线, 0-5分钟, 1-15分钟, 2-30分钟, 3-1小时\n                data = api.get_security_bars(\n                    category=9,  # 日线\n                    market=market,\n                    code=code,\n                    start=0,  # 从最新开始\n                    count=count\n                )\n                \n                if data is None or len(data) == 0:\n                    raise DataFetchError(f\"Pytdx 未查询到 {stock_code} 的数据\")\n                \n                # 转换为 DataFrame\n                df = api.to_df(data)\n                \n                # 过滤日期范围\n                df['datetime'] = pd.to_datetime(df['datetime'])\n                df = df[(df['datetime'] >= start_date) & (df['datetime'] <= end_date)]\n                \n                return df\n                \n            except Exception as e:\n                if isinstance(e, DataFetchError):\n                    raise\n                raise DataFetchError(f\"Pytdx 获取数据失败: {e}\") from e\n    \n    def _normalize_data(self, df: pd.DataFrame, stock_code: str) -> pd.DataFrame:\n        \"\"\"\n        标准化 Pytdx 数据","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/data_provider/pytdx_fetcher.py#L342-L378","documentation":"DataFetchError raised when api.get_security_bars(...) returns None or an empty list for the requested market/code — the TDX server accepted the connection but has no bars for that symbol (invalid code for the market, suspended/delisted stock, or a symbol served only on a different TDX market).","triggerScenarios":"Calling _fetch_raw_data with a code whose market prefix (SH/SZ from _get_market_code) does not match the listing venue; newly listed stocks with no history in the requested window; delisted or suspended tickers; typos in the 6-digit code.","commonSituations":"Wrong market inference for codes like 3xxxxx/6xxxxx edge cases; querying a date range before IPO; code normalized incorrectly upstream (e.g. Shenzhen code treated as Shanghai).","solutions":["Verify the code exists on the inferred market and correct the market mapping in _get_market_code logic if codes are misrouted.","Confirm the stock actually has trading history within [start_date, end_date] (listed, not long-suspended).","Fall back to Akshare/Tushare which have authoritative symbol metadata, then reconcile your code normalization."],"exampleFix":"# before\ndf = pytdx_fetcher.get_stock_data(\"000001\", start, end)  # empty bars -> DataFetchError if market misrouted\n\n# after\n# ensure market inference matches listing venue; verify via akshare\ninfo = ak.stock_individual_info_em(symbol=\"000001\")\ndf = manager.get_stock_data(\"000001\", start, end)  # let manager fail over","handlingStrategy":"try-catch","validationCode":"# cheap existence check via a provider with symbol metadata before pytdx\ninfo = ak.stock_individual_info_em(symbol=code)  # raises if not listed\n# also confirm listing date precedes start_date","typeGuard":null,"tryCatchPattern":"try:\n    df = pytdx_fetcher.get_stock_data(code, start, end)\nexcept DataFetchError as e:\n    if \"未查询到\" in str(e):\n        logger.warning(f\"{code}: no bars from pytdx; verifying symbol and window\")\n        df = manager.get_stock_data(code, start, end)\n    else:\n        raise","preventionTips":["Validate codes against an authoritative symbol list before fetching history.","Clamp request windows to the stock's listing period.","Double-check market inference (SH vs SZ) for edge-case 6-digit codes."],"tags":["pytdx","empty-response","symbol-resolution","market-code"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}