hsliuping/TradingAgents-CN · error · ValueError

无法获取股票 {symbol} 的财务数据。已尝试所有数据源(MongoDB、AKShare、Tushare)均失败。

Error message

无法获取股票 {symbol} 的财务数据。已尝试所有数据源(MongoDB、AKShare、Tushare)均失败。

What it means

Raised by _estimate_financial_metrics in optimized_china_data when real financial metrics could not be retrieved from any backend (MongoDB cache, AKShare, Tushare). The module deliberately refuses to fabricate estimates and surfaces a ValueError naming the symbol.

Source

Thrown at tradingagents/dataflows/optimized_china_data.py:842

    def _estimate_financial_metrics(self, symbol: str, current_price: str) -> dict:
        """获取真实财务指标(从 MongoDB、AKShare、Tushare 获取,失败则抛出异常)"""

        # 提取价格数值
        try:
            price_value = float(current_price.replace('¥', '').replace(',', ''))
        except:
            price_value = 10.0  # 默认值

        # 尝试获取真实财务数据
        real_metrics = self._get_real_financial_metrics(symbol, price_value)
        if real_metrics:
            logger.info(f"✅ 使用真实财务数据: {symbol}")
            return real_metrics

        # 如果无法获取真实数据,抛出异常
        error_msg = f"无法获取股票 {symbol} 的财务数据。已尝试所有数据源(MongoDB、AKShare、Tushare)均失败。"
        logger.error(f"❌ {error_msg}")
        raise ValueError(error_msg)

    def _get_real_financial_metrics(self, symbol: str, price_value: float) -> dict:
        """获取真实财务指标 - 优先使用数据库缓存,再使用API"""
        try:
            # 🔥 优先从 market_quotes 获取实时股价,替换传入的 price_value
            from tradingagents.config.database_manager import get_database_manager
            db_manager = get_database_manager()
            db_client = None

            if db_manager.is_mongodb_available():
                try:
                    db_client = db_manager.get_mongodb_client()
                    db = db_client['tradingagents']

                    # 标准化股票代码为6位
                    code6 = symbol.replace('.SH', '').replace('.SZ', '').zfill(6)

                    # 从 market_quotes 获取实时股价

View on GitHub (pinned to 74783e8817)

Solutions

  1. Verify the symbol is a valid A-share code (e.g. 600519.SH / 000001.SZ format expected by the module)
  2. Check connectivity and try the AKShare fetch standalone; fix network/proxy
  3. Configure Tushare credentials so the last fallback works
  4. Populate the MongoDB cache for the symbol once data is available

Example fix

# before
report = _generate_fundamentals_report('XXXXXX', '2025-01-01')
# after
report = _generate_fundamentals_report('600519.SH', '2025-01-01')
Defensive patterns

Strategy: fallback

Validate before calling

from tradingagents.config.database_manager import get_database_manager
try:
    get_database_manager()
except Exception:
    raise SystemExit('Backends unavailable; fix MongoDB/AKShare/Tushare first')

Try / catch

try:
    report = _generate_fundamentals_report(symbol, date)
except ValueError as e:
    if '财务数据' in str(e):
        report = cached_report(symbol) or 'Financial data unavailable'
    else:
        raise

Prevention

When it happens

Trigger: Generating a fundamentals report (_generate_fundamentals_report) for a symbol with no data in MongoDB, an AKShare call that fails/times out, and no valid Tushare token — all three failing in sequence.

Common situations: Obscure/delisted A-share tickers, AKShare network issues, Tushare token missing or rate-limited, MongoDB cache empty for that symbol.

Related errors


AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28). Data as JSON: /api/errors/c1b47a964540b35c. Report an issue: GitHub.