{"record":{"id":"5e7e7c70a79201b7","repo":"hsliuping/TradingAgents-CN","slug":"dataframe-close-col","errorCode":null,"errorMessage":"DataFrame缺少收盘价列: {close_col}","messagePattern":"DataFrame缺少收盘价列: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"tradingagents/tools/analysis/indicators.py","lineNumber":318,"sourceCode":"        - ma5, ma10, ma20, ma60: 移动平均线\n        - rsi: RSI指标（14日，国际标准）\n        - rsi6, rsi12, rsi24: RSI指标（中国风格，仅当 rsi_style='china' 时）\n        - rsi14: RSI指标（14日，简单移动平均，仅当 rsi_style='china' 时）\n        - macd_dif, macd_dea, macd: MACD指标\n        - boll_mid, boll_upper, boll_lower: 布林带\n\n    示例：\n        >>> df = pd.DataFrame({'close': [100, 101, 102, 103, 104]})\n        >>> df = add_all_indicators(df)\n        >>> print(df[['close', 'ma5', 'rsi']].tail())\n        >>>\n        >>> # 中国风格\n        >>> df = add_all_indicators(df, rsi_style='china')\n        >>> print(df[['close', 'rsi6', 'rsi12', 'rsi24']].tail())\n    \"\"\"\n    # 检查必要的列\n    if close_col not in df.columns:\n        raise ValueError(f\"DataFrame缺少收盘价列: {close_col}\")\n\n    # 计算移动平均线（MA5, MA10, MA20, MA60）\n    df['ma5'] = ma(df[close_col], 5, min_periods=1)\n    df['ma10'] = ma(df[close_col], 10, min_periods=1)\n    df['ma20'] = ma(df[close_col], 20, min_periods=1)\n    df['ma60'] = ma(df[close_col], 60, min_periods=1)\n\n    # 计算RSI指标\n    if rsi_style == 'china':\n        # 中国风格：RSI6, RSI12, RSI24（使用中国式SMA）\n        df['rsi6'] = rsi(df[close_col], 6, method='china')\n        df['rsi12'] = rsi(df[close_col], 12, method='china')\n        df['rsi24'] = rsi(df[close_col], 24, method='china')\n        # 保留RSI14作为国际标准参考（使用简单移动平均）\n        df['rsi14'] = rsi(df[close_col], 14, method='sma')\n        # 为了兼容性，也添加 'rsi' 列（指向 rsi12）\n        df['rsi'] = df['rsi12']\n    else:","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/tools/analysis/indicators.py#L300-L336","documentation":"add_all_indicators computes a full indicator suite (ma/rsi/macd/boll/atr/kdj columns) from a single close-price column, defaulting to close_col (typically 'close'). If that column is absent it raises immediately with this ValueError, since every downstream computation depends on it. Callers include the stock data tools (_format_stock_data, get_hk_stock_data_akshare), so raw data lacking a normalized 'close' column triggers it deep in formatting paths.","triggerScenarios":"Calling add_all_indicators(df) where df has 'Close', '收盘', 'adj_close', or no price column at all; passing close_col='adj_close' when only 'close' exists (or vice versa). Also hit indirectly via _format_stock_data or get_hk_stock_data_akshare on data whose columns were not normalized.","commonSituations":"Feeding DataFrames from different akshare endpoints with Chinese column names; renaming for storage and forgetting to map back; using adjusted vs raw close inconsistently across the pipeline.","solutions":["Rename the price column to match: df = df.rename(columns={'Close': 'close'}) or {'收盘': 'close'}.","Or pass the actual column explicitly: add_all_indicators(df, close_col='adj_close').","Ensure loaders (_format_stock_data / get_hk_stock_data_akshare paths) normalize column names before calling add_all_indicators."],"exampleFix":"# before\ndf = add_all_indicators(raw_df)  # raw_df has '收盘'\n\n# after\ndf = add_all_indicators(raw_df.rename(columns={\"收盘\": \"close\"}), close_col=\"close\")","handlingStrategy":"validation","validationCode":"close_col = \"close\" if \"close\" in df.columns else next((c for c in (\"Close\", \"收盘\", \"adj_close\") if c in df.columns), None)\nif close_col is None:\n    raise ValueError(\"no price column found\")\ndf = add_all_indicators(df, close_col=close_col)","typeGuard":"def has_close_column(df: pd.DataFrame, close_col: str = \"close\") -> bool:\n    \"\"\"True if the DataFrame has the close column add_all_indicators needs.\"\"\"\n    return close_col in df.columns","tryCatchPattern":"try:\n    df = add_all_indicators(df, close_col=close_col)\nexcept ValueError as e:\n    if \"缺少收盘价列\" in str(e):\n        df = df.rename(columns={\"Close\": \"close\", \"收盘\": \"close\"})\n        df = add_all_indicators(df, close_col=\"close\")\n    else:\n        raise","preventionTips":["Rename source columns to lowercase close/high/low/open right after fetching data.","When using adjusted prices consistently, pass close_col='adj_close' everywhere in your pipeline.","Add a column-normalization step in loaders like _format_stock_data so downstream calls never see raw schemas."],"tags":["pandas","indicators","dataframe","column-mismatch","precondition"],"backgroundTag":"missing-dataframe-column","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}