hsliuping/TradingAgents-CN · error · ValueError
不支持的RSI计算方法: {method},支持的方法: 'ema', 'sma', 'china'
Error message
不支持的RSI计算方法: {method},支持的方法: 'ema', 'sma', 'china' What it means
The rsi function supports exactly three computation methods: 'ema' (exponential smoothing), 'sma' (simple moving average of gains/losses), and 'china' (同花顺/通达信-style SMA(X,N,1) via ewm(com=n-1, adjust=True)). Passing any other method string reaches the final else branch and raises this ValueError. The method parameter exists because different charting platforms produce visibly different RSI values.
Source
Thrown at tradingagents/tools/analysis/indicators.py:117
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
if method == 'ema':
# 国际标准:Wilder's指数移动平均
avg_gain = gain.ewm(alpha=1 / float(n), adjust=False).mean()
avg_loss = loss.ewm(alpha=1 / float(n), adjust=False).mean()
elif method == 'sma':
# 简单移动平均
avg_gain = gain.rolling(window=int(n), min_periods=1).mean()
avg_loss = loss.rolling(window=int(n), min_periods=1).mean()
elif method == 'china':
# 中国式SMA:同花顺/通达信风格
# SMA(X, N, 1) = ewm(com=N-1, adjust=True).mean()
# 参考:https://blog.csdn.net/u011218867/article/details/117427927
avg_gain = gain.ewm(com=int(n) - 1, adjust=True).mean()
avg_loss = loss.ewm(com=int(n) - 1, adjust=True).mean()
else:
raise ValueError(f"不支持的RSI计算方法: {method},支持的方法: 'ema', 'sma', 'china'")
rs = avg_gain / (avg_loss.replace(0, np.nan))
rsi_val = 100 - (100 / (1 + rs))
return rsi_val
def boll(close: pd.Series, n: int = 20, k: float = 2.0, min_periods: int = None) -> pd.DataFrame:
"""
计算布林带指标(Bollinger Bands)
Args:
close: 收盘价序列
n: 周期,默认20
k: 标准差倍数,默认2.0
min_periods: 最小周期数,默认为1(允许前期数据不足时也计算)
Returns:
包含 boll_mid, boll_upper, boll_lower 的 DataFrameView on GitHub (pinned to 74783e8817)
Solutions
- Use one of the exact strings 'ema', 'sma', or 'china' (lowercase).
- If you want Wilder's RSI (TA-Lib default), note this library's 'china' method uses ewm(com=n-1) which is equivalent to Wilder smoothing — use that.
- If a genuinely different smoothing is needed, compute it manually with pandas ewm rather than passing an unsupported method string.
Example fix
# before rsi_val = rsi(df["close"], n=14, method="wilder") # after rsi_val = rsi(df["close"], n=14, method="china") # ewm(com=n-1) == Wilder-style smoothing
Defensive patterns
Strategy: validation
Validate before calling
VALID_RSI_METHODS = {"ema", "sma", "china"}
method = (method or "ema").lower()
if method not in VALID_RSI_METHODS:
method = "china" # or raise your own config error
rsi_val = rsi(df["close"], n=14, method=method) Type guard
def is_valid_rsi_method(m: str) -> bool:
"""Narrow to the library's supported RSI methods."""
return isinstance(m, str) and m in {"ema", "sma", "china"} Try / catch
try:
rsi_val = rsi(close, n, method=method)
except ValueError as e:
if "不支持的RSI计算方法" in str(e):
rsi_val = rsi(close, n, method="china") # sensible default
else:
raise Prevention
- Whitelist method strings from user/config input against {'ema','sma','china'}.
- Remember 'china' == Wilder-style smoothing via ewm(com=n-1) if porting TA-Lib code.
- Method strings are case-sensitive; lowercase before passing.
When it happens
Trigger: Calling rsi(close, n, method='wilder'), method='Wilder', method='EMA' (case-sensitive), or compute_indicator(df, 'rsi', method='wma'). Also passing method=None explicitly if the default handling doesn't catch it before the else.
Common situations: Porting code from other libraries where the Wilder/smoothing method is named differently ('rma', 'wilder', 'cutler'); case mismatches; typos like 'cn' or 'zh' instead of 'china'; assuming TradingView/TA-Lib naming applies here.
Related errors
- DataFrame缺少必要列: {missing}, 现有列: {list(df.columns)[:10]}...
- DataFrame缺少收盘价列: {close_col}
- 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/fb4e7f4e0fc77088.
Report an issue: GitHub.