ZhuLinsen/daily_stock_analysis · error · DataFetchError

PytdxFetcher 不支持美股 ,请使用 AkshareFetcher 或 YfinanceFetcher

Error message

PytdxFetcher 不支持美股 {stock_code},请使用 AkshareFetcher 或 YfinanceFetcher

What it means

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.

Solutions

  1. Route US symbols to AkshareFetcher or YfinanceFetcher before they reach PytdxFetcher.
  2. If using DataFetcherManager, ensure PytdxFetcher is only in the A-share provider chain, not the US chain.
  3. Add a market pre-check with _is_us_code in your caller if you cannot rely on the manager.

Example fix

# before
df = pytdx_fetcher.get_stock_data("AAPL", start, end)  # DataFetchError

# after
from data_provider.pytdx_fetcher import _is_us_code
if _is_us_code(code):
    df = yfinance_fetcher.get_stock_data(code, start, end)
else:
    df = pytdx_fetcher.get_stock_data(code, start, end)
Defensive patterns

Strategy: type-guard

Validate before calling

from data_provider.pytdx_fetcher import _is_us_code

if _is_us_code(stock_code):
    fetcher = yfinance_fetcher  # or akshare_fetcher
else:
    fetcher = pytdx_fetcher

Type guard

from data_provider.pytdx_fetcher import _is_us_code

def is_us_symbol(code: str) -> bool:
    """True when the code is a US ticker unsupported by pytdx."""
    return _is_us_code(code)

Try / catch

try:
    df = pytdx_fetcher.get_stock_data(code, start, end)
except DataFetchError as e:
    if "不支持美股" in str(e):
        df = yfinance_fetcher.get_stock_data(code, start, end)
    else:
        raise

Prevention

When it happens

Trigger: Passing a US ticker (e.g. AAPL, US.AAPL, formats matched by _is_us_code) to PytdxFetcher._fetch_raw_data for historical daily data.

Common situations: 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.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/a15d2a0f977c1586. Report an issue: GitHub.

Appendix: source

Thrown at data_provider/pytdx_fetcher.py:324

        wait=wait_exponential(multiplier=1, min=2, max=30),
        retry=retry_if_exception_type((ConnectionError, TimeoutError)),
        before_sleep=before_sleep_log(logger, logging.WARNING),
    )
    def _fetch_raw_data(self, stock_code: str, start_date: str, end_date: str) -> pd.DataFrame:
        """
        从通达信获取原始数据
        
        使用 get_security_bars() 获取日线数据
        
        流程:
        1. 检查是否为美股(不支持)
        2. 使用上下文管理器管理连接
        3. 判断市场代码
        4. 调用 API 获取 K 线数据
        """
        # 美股不支持,抛出异常让 DataFetcherManager 切换到其他数据源
        if _is_us_code(stock_code):
            raise DataFetchError(f"PytdxFetcher 不支持美股 {stock_code},请使用 AkshareFetcher 或 YfinanceFetcher")

        # 港股不支持,抛出异常让 DataFetcherManager 切换到其他数据源
        if _is_hk_market(stock_code):
            raise DataFetchError(f"PytdxFetcher 不支持港股 {stock_code},请使用 AkshareFetcher")

        # 北交所不支持,抛出异常让 DataFetcherManager 切换到其他数据源
        if is_bse_code(stock_code):
            raise DataFetchError(
                f"PytdxFetcher 不支持北交所 {stock_code},将自动切换其他数据源"
            )
        
        market, code = self._get_market_code(stock_code)
        
        # 计算需要获取的交易日数量(估算)
        from datetime import datetime as dt
        start_dt = dt.strptime(start_date, '%Y-%m-%d')
        end_dt = dt.strptime(end_date, '%Y-%m-%d')
        days = (end_dt - start_dt).days

View on GitHub (pinned to 5159bd72e8)