Pharos
TAMomentum

TA.RSI()

相对强弱指标 (Relative Strength Index)

衡量价格上涨和下跌动能的震荡指标,值域 0-100。

语法

python
TA.RSI(data, timeperiod=14)

参数

参数名类型必选默认值说明
dataany-价格数据数组(收盘价)
timeperiodany-时间周期,默认 14

返回值

返回 RSI 值数组,范围 0-100

计算方法

RSI = 100 - (100 / (1 + RS))

其中 RS = 平均上涨幅度 / 平均下跌幅度

解读

  • RSI > 70: 超买区域,可能回调
  • RSI < 30: 超卖区域,可能反弹
  • RSI = 50: 中性区域
  • RSI > 50: 上升动能占优
  • RSI < 50: 下降动能占优

基础示例

python
def main():
    records = exchange.GetRecords()
    closes = [r['Close'] for r in records]
    
    rsi = TA.RSI(closes, 14)
    current_rsi = rsi[-1]
    
    if current_rsi > 70:
        Log("RSI 超买:", current_rsi, "考虑卖出")
    elif current_rsi < 30:
        Log("RSI 超卖:", current_rsi, "考虑买入")
    else:
        Log("RSI 正常区间:", current_rsi)

高级应用

1. RSI 背离检测

python
def detect_rsi_divergence(prices, rsi_values, period=10):
    """检测 RSI 背离"""
    if len(prices) < period or len(rsi_values) < period:
        return None
    
    # 顶背离:价格创新高,RSI 未创新高
    if (prices[-1] > max(prices[-period:-1]) and 
        rsi_values[-1] < max(rsi_values[-period:-1])):
        return "顶背离"
    
    # 底背离:价格创新低,RSI 未创新低
    if (prices[-1] < min(prices[-period:-1]) and 
        rsi_values[-1] > min(rsi_values[-period:-1])):
        return "底背离"
    
    return None

def main():
    records = exchange.GetRecords()
    closes = [r['Close'] for r in records]
    
    rsi = TA.RSI(closes, 14)
    divergence = detect_rsi_divergence(closes, rsi, 10)
    
    if divergence == "顶背离":
        Log("RSI 顶背离,价格可能回调")
    elif divergence == "底背离":
        Log("RSI 底背离,价格可能反弹")

2. RSI 多周期确认

python
def main():
    # 获取不同周期的K线
    records_1h = exchange.GetRecords(PERIOD_H1)
    records_4h = exchange.GetRecords(PERIOD_H4)
    records_1d = exchange.GetRecords(PERIOD_D1)
    
    # 计算不同周期的 RSI
    rsi_1h = TA.RSI([r['Close'] for r in records_1h], 14)
    rsi_4h = TA.RSI([r['Close'] for r in records_4h], 14)
    rsi_1d = TA.RSI([r['Close'] for r in records_1d], 14)
    
    # 多周期共振
    if rsi_1h[-1] < 30 and rsi_4h[-1] < 30 and rsi_1d[-1] < 30:
        Log("多周期RSI共振超卖,强买入信号")
    elif rsi_1h[-1] > 70 and rsi_4h[-1] > 70 and rsi_1d[-1] > 70:
        Log("多周期RSI共振超买,强卖出信号")

3. RSI 趋势线突破

python
def main():
    records = exchange.GetRecords()
    closes = [r['Close'] for r in records]
    
    rsi = TA.RSI(closes, 14)
    
    # RSI 突破 50 中轴线
    if rsi[-1] > 50 and rsi[-2] <= 50:
        Log("RSI 上穿 50,多头动能增强")
    elif rsi[-1] < 50 and rsi[-2] >= 50:
        Log("RSI 下穿 50,空头动能增强")

参数优化建议

周期推荐参数特点
短线RSI(7) 或 RSI(9)更敏感,信号更多
中线RSI(14)标准配置,平衡性好
长线RSI(21) 或 RSI(25)更平滑,假信号少

与其他指标配合

RSI + MACD

python
rsi = TA.RSI(closes, 14)
macd, signal, hist = TA.MACD(closes, 12, 26, 9)

# 双重确认买入
if rsi[-1] < 30 and macd[-1] > signal[-1] and macd[-2] <= signal[-2]:
    Log("RSI超卖 + MACD金叉,强买入信号")

RSI + BOLL

python
rsi = TA.RSI(closes, 14)
upper, middle, lower = TA.BOLL(closes, 20, 2)

# 超卖 + 触及下轨
if rsi[-1] < 30 and closes[-1] <= lower[-1]:
    Log("RSI超卖 + 触及布林下轨,强支撑")

注意事项

⚠️ 重要提醒

  1. 趋势市场中: 强趋势下,RSI 可能长期处于超买或超卖区域
  2. 震荡市场中: RSI 超买超卖信号更可靠
  3. 参数选择: 周期越短越灵敏,但假信号越多
  4. 配合使用: 不要单独依赖 RSI,需与其他指标配合

相关指标

返回目录

← 返回动量指标目录