You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pine Script V5中将非叠加振荡器转为带RSI-SMA交叉买卖标签的叠加指标

解决Pine Script V5中"Undeclared identifier 'cross'"错误并实现交叉信号标签

错误原因

Pine Script V5已移除旧版本中的cross函数,需使用官方提供的专用交叉检测函数:

  • ta.crossover(x, y):检测指标x上穿指标y
  • ta.crossunder(x, y):检测指标x下穿指标y

修正后的完整代码

//@version=5
indicator("RSI-SMA 交叉信号", overlay=true)

// 计算核心指标值
rsi21 = ta.rsi(close, 21)
sma5_rsi = ta.sma(rsi21, 5)

// 检测交叉信号
long_signal = ta.crossover(rsi21, sma5_rsi)
short_signal = ta.crossunder(rsi21, sma5_rsi)

// 在价格柱上下绘制信号标签
if (long_signal)
    // 做多标签:价格柱下方,用ATR偏移避免重叠
    label.new(bar_index, low - ta.atr(10)*0.1, "↑", 
              color=color.green, textcolor=color.white, 
              style=label.style_label_up, size=size.small)

if (short_signal)
    // 做空标签:价格柱上方,用ATR偏移避免重叠
    label.new(bar_index, high + ta.atr(10)*0.1, "↓", 
              color=color.red, textcolor=color.white, 
              style=label.style_label_down, size=size.small)

代码关键说明

  • overlay=true:将指标设置为叠加模式,直接显示在K线图上
  • 未调用plot()函数:完全隐藏RSI和SMA的线条,仅保留信号标签
  • 使用ta.crossover/ta.crossunder:替换旧的cross函数,解决编译器错误
  • ATR偏移标签位置:利用平均真实波幅设置标签与价格柱的间距,避免标签和K线重叠,提升可读性
  • 标签样式适配:label.style_label_up和label.style_label_down分别对应价格柱下方和上方的箭头样式

内容的提问来源于stack exchange,提问作者Emmanuel Okenwa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 13:35:01