hsliuping/TradingAgents-CN · error · ValueError
DataFrame缺少收盘价列: {close_col}
Error message
DataFrame缺少收盘价列: {close_col} What it means
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.
Source
Thrown at tradingagents/tools/analysis/indicators.py:318
- ma5, ma10, ma20, ma60: 移动平均线
- rsi: RSI指标(14日,国际标准)
- rsi6, rsi12, rsi24: RSI指标(中国风格,仅当 rsi_style='china' 时)
- rsi14: RSI指标(14日,简单移动平均,仅当 rsi_style='china' 时)
- macd_dif, macd_dea, macd: MACD指标
- boll_mid, boll_upper, boll_lower: 布林带
示例:
>>> df = pd.DataFrame({'close': [100, 101, 102, 103, 104]})
>>> df = add_all_indicators(df)
>>> print(df[['close', 'ma5', 'rsi']].tail())
>>>
>>> # 中国风格
>>> df = add_all_indicators(df, rsi_style='china')
>>> print(df[['close', 'rsi6', 'rsi12', 'rsi24']].tail())
"""
# 检查必要的列
if close_col not in df.columns:
raise ValueError(f"DataFrame缺少收盘价列: {close_col}")
# 计算移动平均线(MA5, MA10, MA20, MA60)
df['ma5'] = ma(df[close_col], 5, min_periods=1)
df['ma10'] = ma(df[close_col], 10, min_periods=1)
df['ma20'] = ma(df[close_col], 20, min_periods=1)
df['ma60'] = ma(df[close_col], 60, min_periods=1)
# 计算RSI指标
if rsi_style == 'china':
# 中国风格:RSI6, RSI12, RSI24(使用中国式SMA)
df['rsi6'] = rsi(df[close_col], 6, method='china')
df['rsi12'] = rsi(df[close_col], 12, method='china')
df['rsi24'] = rsi(df[close_col], 24, method='china')
# 保留RSI14作为国际标准参考(使用简单移动平均)
df['rsi14'] = rsi(df[close_col], 14, method='sma')
# 为了兼容性,也添加 'rsi' 列(指向 rsi12)
df['rsi'] = df['rsi12']
else:View on GitHub (pinned to 74783e8817)
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.
Example fix
# before
df = add_all_indicators(raw_df) # raw_df has '收盘'
# after
df = add_all_indicators(raw_df.rename(columns={"收盘": "close"}), close_col="close") Defensive patterns
Strategy: validation
Validate before calling
close_col = "close" if "close" in df.columns else next((c for c in ("Close", "收盘", "adj_close") if c in df.columns), None)
if close_col is None:
raise ValueError("no price column found")
df = add_all_indicators(df, close_col=close_col) Type guard
def has_close_column(df: pd.DataFrame, close_col: str = "close") -> bool:
"""True if the DataFrame has the close column add_all_indicators needs."""
return close_col in df.columns Try / catch
try:
df = add_all_indicators(df, close_col=close_col)
except ValueError as e:
if "缺少收盘价列" in str(e):
df = df.rename(columns={"Close": "close", "收盘": "close"})
df = add_all_indicators(df, close_col="close")
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- DataFrame缺少必要列: {missing}, 现有列: {list(df.columns)[:10]}...
- 不支持的RSI计算方法: {method},支持的方法: 'ema', 'sma', 'china'
- Indicator {indicator} is not supported. Please choose from:
- 不支持的指标: {name}
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/5e7e7c70a79201b7.
Report an issue: GitHub.