{"record":{"id":"a15d2a0f977c1586","repo":"ZhuLinsen/daily_stock_analysis","slug":"pytdxfetcher-stock-code-aksharefetcher","errorCode":null,"errorMessage":"PytdxFetcher 不支持美股 {stock_code}，请使用 AkshareFetcher 或 YfinanceFetcher","messagePattern":"PytdxFetcher 不支持美股 (.+?)，请使用 AkshareFetcher 或 YfinanceFetcher","errorType":"exception","errorClass":"DataFetchError","httpStatus":null,"severity":"error","filePath":"data_provider/pytdx_fetcher.py","lineNumber":324,"sourceCode":"        wait=wait_exponential(multiplier=1, min=2, max=30),\n        retry=retry_if_exception_type((ConnectionError, TimeoutError)),\n        before_sleep=before_sleep_log(logger, logging.WARNING),\n    )\n    def _fetch_raw_data(self, stock_code: str, start_date: str, end_date: str) -> pd.DataFrame:\n        \"\"\"\n        从通达信获取原始数据\n        \n        使用 get_security_bars() 获取日线数据\n        \n        流程：\n        1. 检查是否为美股（不支持）\n        2. 使用上下文管理器管理连接\n        3. 判断市场代码\n        4. 调用 API 获取 K 线数据\n        \"\"\"\n        # 美股不支持，抛出异常让 DataFetcherManager 切换到其他数据源\n        if _is_us_code(stock_code):\n            raise DataFetchError(f\"PytdxFetcher 不支持美股 {stock_code}，请使用 AkshareFetcher 或 YfinanceFetcher\")\n\n        # 港股不支持，抛出异常让 DataFetcherManager 切换到其他数据源\n        if _is_hk_market(stock_code):\n            raise DataFetchError(f\"PytdxFetcher 不支持港股 {stock_code}，请使用 AkshareFetcher\")\n\n        # 北交所不支持，抛出异常让 DataFetcherManager 切换到其他数据源\n        if is_bse_code(stock_code):\n            raise DataFetchError(\n                f\"PytdxFetcher 不支持北交所 {stock_code}，将自动切换其他数据源\"\n            )\n        \n        market, code = self._get_market_code(stock_code)\n        \n        # 计算需要获取的交易日数量（估算）\n        from datetime import datetime as dt\n        start_dt = dt.strptime(start_date, '%Y-%m-%d')\n        end_dt = dt.strptime(end_date, '%Y-%m-%d')\n        days = (end_dt - start_dt).days","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/data_provider/pytdx_fetcher.py#L306-L342","documentation":"DataFetchError raised in PytdxFetcher._fetch_raw_data when _is_us_code(stock_code) is true. TDX protocol only serves Chinese markets, so US symbols are rejected up front; the message explicitly tells the DataFetcherManager (and the developer) to switch to AkshareFetcher or YfinanceFetcher.","triggerScenarios":"Passing a US ticker (e.g. AAPL, US.AAPL, formats matched by _is_us_code) to PytdxFetcher._fetch_raw_data for historical daily data.","commonSituations":"Single-provider pipelines pointed at PytdxFetcher while analyzing multi-market portfolios; a mixed watchlist (A股+美股) routed to the A-share fast path; upstream market detection failing to filter US codes.","solutions":["Route US symbols to AkshareFetcher or YfinanceFetcher before they reach PytdxFetcher.","If using DataFetcherManager, ensure PytdxFetcher is only in the A-share provider chain, not the US chain.","Add a market pre-check with _is_us_code in your caller if you cannot rely on the manager."],"exampleFix":"# before\ndf = pytdx_fetcher.get_stock_data(\"AAPL\", start, end)  # DataFetchError\n\n# after\nfrom data_provider.pytdx_fetcher import _is_us_code\nif _is_us_code(code):\n    df = yfinance_fetcher.get_stock_data(code, start, end)\nelse:\n    df = pytdx_fetcher.get_stock_data(code, start, end)","handlingStrategy":"type-guard","validationCode":"from data_provider.pytdx_fetcher import _is_us_code\n\nif _is_us_code(stock_code):\n    fetcher = yfinance_fetcher  # or akshare_fetcher\nelse:\n    fetcher = pytdx_fetcher","typeGuard":"from data_provider.pytdx_fetcher import _is_us_code\n\ndef is_us_symbol(code: str) -> bool:\n    \"\"\"True when the code is a US ticker unsupported by pytdx.\"\"\"\n    return _is_us_code(code)","tryCatchPattern":"try:\n    df = pytdx_fetcher.get_stock_data(code, start, end)\nexcept DataFetchError as e:\n    if \"不支持美股\" in str(e):\n        df = yfinance_fetcher.get_stock_data(code, start, end)\n    else:\n        raise","preventionTips":["Classify market before choosing a provider; never blast one fetcher with all markets.","Cover market-routing guards in integration tests for every enabled fetcher."],"tags":["pytdx","market-coverage","us-stocks","routing"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}