ZhuLinsen/daily_stock_analysis · error · DataFetchError
PytdxFetcher 不支持港股 {stock_code},请使用 AkshareFetcher
Error message
PytdxFetcher 不支持港股 {stock_code},请使用 AkshareFetcher What it means
DataFetchError raised in PytdxFetcher._fetch_raw_data when _is_hk_market(stock_code) is true. The TDX servers this fetcher connects to do not carry Hong Kong quotes, so HK symbols are rejected and the manager is directed to AkshareFetcher for HK data.
Source
Thrown at data_provider/pytdx_fetcher.py:328
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
count = min(max(days * 5 // 7 + 10, 30), 800) # 估算交易日,最大 800 条
logger.debug(f"调用 Pytdx get_security_bars(market={market}, code={code}, count={count})")
View on GitHub (pinned to 5159bd72e8)
Solutions
- Route HK symbols to AkshareFetcher (or another HK-capable provider) before calling PytdxFetcher.
- Ensure the market dispatcher maps hk* codes to the HK provider chain.
- Guard the call site with _is_hk_market if the manager is not in play.
Example fix
# before
df = pytdx_fetcher.get_stock_data("hk00700", start, end) # DataFetchError
# after
from data_provider.pytdx_fetcher import _is_hk_market
if _is_hk_market(code):
df = akshare_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_hk_market
if _is_hk_market(stock_code):
fetcher = akshare_fetcher
else:
fetcher = pytdx_fetcher Type guard
from data_provider.pytdx_fetcher import _is_hk_market
def is_hk_symbol(code: str) -> bool:
"""True when the code is an HK-market symbol unsupported by pytdx."""
return _is_hk_market(code) Try / catch
try:
df = pytdx_fetcher.get_stock_data(code, start, end)
except DataFetchError as e:
if "不支持港股" in str(e):
df = akshare_fetcher.get_stock_data(code, start, end)
else:
raise Prevention
- Route hk* codes to the HK provider chain at dispatch time.
- Watch for normalization changes that re-shape HK codes before they hit pytdx.
When it happens
Trigger: Passing an HK-format code (e.g. 00700, hk00700, 00700.HK — anything matched by _is_hk_market) to PytdxFetcher for daily historical data.
Common situations: Analyzing 港股 watchlists through the A-share pipeline; normalization upstream turning HK codes into a shape that slips past earlier filters; single fetcher reused across markets.
Related errors
- PytdxFetcher 不支持美股 {stock_code},请使用 AkshareFetcher 或 Yfinanc
- PytdxFetcher 不支持北交所 {stock_code},将自动切换其他数据源
- TushareFetcher 不支持美股 {raw_code},请使用 AkshareFetcher 或 Yfinanc
- AkshareFetcher 不支持美股 {stock_code},请使用 YfinanceFetcher 获取正确的复
- Akshare 获取港股数据失败: {e}
AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15).
Data as JSON: /api/errors/c513f5274d7efcdb.
Report an issue: GitHub.