ZhuLinsen/daily_stock_analysis · warning · DataFetchError

BaostockFetcher 不支持港股 {raw_code},请使用 AkshareFetcher

Error message

BaostockFetcher 不支持港股 {raw_code},请使用 AkshareFetcher

What it means

A guard DataFetchError raised by BaostockFetcher._convert_stock_code when _is_hk_market(raw_code) is True. Baostock only serves mainland A-shares, so HK codes are rejected at code-conversion time with an explicit pointer to AkshareFetcher. This is a routing error by design: the manager should catch it and dispatch to an HK-capable source.

Source

Thrown at data_provider/baostock_fetcher.py:151

        """
        转换股票代码为 Baostock 格式
        
        Baostock 要求的格式:
        - 沪市:sh.600519
        - 深市:sz.000001
        
        Args:
            stock_code: 原始代码,如 '600519', '000001'
            
        Returns:
            Baostock 格式代码,如 'sh.600519', 'sz.000001'
        """
        raw_code = stock_code.strip()
        upper = raw_code.upper()

        # HK stocks are not supported by Baostock
        if _is_hk_market(raw_code):
            raise DataFetchError(f"BaostockFetcher 不支持港股 {raw_code},请使用 AkshareFetcher")

        # 保留既有小写 baostock 格式输入的内部容错,但用户配置仍推荐 6 位裸代码。
        if raw_code.startswith(('sh.', 'sz.')):
            return raw_code.lower()

        exchange_hint = None
        if upper.startswith(('SH', 'SS')) or upper.endswith(('.SH', '.SS')):
            exchange_hint = 'sh'
        elif upper.startswith('SZ') or upper.endswith('.SZ'):
            exchange_hint = 'sz'

        code = normalize_stock_code(raw_code)

        if exchange_hint in ('sh', 'sz') and code.isdigit() and len(code) == 6:
            return f"{exchange_hint}.{code}"
        
        # ETF: Shanghai ETF (51xx, 52xx, 56xx, 58xx) -> sh; Shenzhen ETF (15xx, 16xx, 18xx) -> sz
        if len(code) == 6:

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Route by market before fetching: only send 6-digit A-share codes (or sh./sz. prefixed) to BaostockFetcher.
  2. Catch this DataFetchError in the manager and try AkshareFetcher for the HK code.
  3. Normalize HK codes with the 'hk' prefix at config load so market detection is unambiguous.

Example fix

# before
df = baostock_fetcher.fetch('hk00700', start, end)  # raises

# after
if _is_hk_market(code):
    df = akshare_fetcher.fetch(code, start, end)
else:
    df = baostock_fetcher.fetch(code, start, end)
Defensive patterns

Strategy: validation

Validate before calling

from data_provider.baostock_fetcher import _is_hk_market
assert not _is_hk_market(code), f'{code} is HK — route to AkshareFetcher, not Baostock'

Type guard

from data_provider.baostock_fetcher import _is_hk_market

def is_baostock_code(code: str) -> bool:
    """Baostock serves mainland A-shares only."""
    return not _is_hk_market(code)

Try / catch

try:
    df = baostock_fetcher.fetch(code, start, end)
except DataFetchError as e:
    if '不支持港股' in str(e):
        df = akshare_fetcher.fetch(code, start, end)
    else:
        raise

Prevention

When it happens

Trigger: Passing HK-style codes ('hk00700', '00700', '01810') into any BaostockFetcher fetch path that calls _convert_stock_code. The check runs before the sh./sz. prefix handling, so even 'hk...' formatted inputs are caught.

Common situations: Mixed-market watchlists where Baostock is first in the source chain; HK codes stored without the 'hk' prefix colliding with 5-digit ambiguity; legacy configs that previously routed everything to Baostock.

Related errors


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