You need to enable JavaScript to run this app. Pine Script 如何在RSI背离曲线变色时设置触发警报
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script 如何在RSI背离曲线变色时设置触发警报

RSI背离指标警报逻辑修正方案

原脚本失效核心原因

  • 你编写的两个触发条件存在逻辑矛盾,不存在能让条件成立的数值场景:
    • divlong = divergence < 0 and divergence >=0 要求同一个背离值同时小于0、大于等于0,永远为假
    • divshort = divergence > 0 and divergence <=0 要求同一个背离值同时大于0、小于等于0,永远为假
      这就是警报和标注完全没有反应的根本原因。

颜色切换触发的实现逻辑

原指标的信号线颜色和数值的绑定规则为:

  • 背离值大于0 → 信号线显示为lime绿色
  • 背离值小于等于0 → 信号线显示为red红色
    需要捕捉的两类触发场景,正好对应颜色跳变的两个方向:
  1. 红转绿触发:上一个K线的背离值≤0(对应之前的红色线),当前K线的背离值>0(对应刚切换为绿色线) → 触发Long方向警报,在背离指标下方绘制向上三角
  2. 绿转红触发:上一个K线的背离值>0(对应之前的绿色线),当前K线的背离值≤0(对应刚切换为红色线) → 触发Short方向警报,在背离指标上方绘制向下三角

通过divergence[1]引用上一个周期的背离值,就能精准定位跨周期的颜色切换节点,不会在同色区间内重复触发。

修正后可直接运行的完整脚本

study(title="RSI Divergence", shorttitle="RSI Divergence", overlay=false)

src_fast = close
len_fast = input(5, minval=1, title="Length Fast RSI")
src_slow = close
len_slow = input(14, minval=1, title="Length Slow RSI")

up_fast = rma(max(change(src_fast), 0), len_fast)
down_fast = rma(-min(change(src_fast), 0), len_fast)
rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast))

up_slow = rma(max(change(src_slow), 0), len_slow)
down_slow = rma(-min(change(src_slow), 0), len_slow)
rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow))

divergence = rsi_fast - rsi_slow
plotdiv = plot(divergence, color = divergence > 0 ? lime : red, linewidth = 2)
band = hline(0)

// 修正后的警报触发逻辑
// 红转绿:上一根≤0(红),当前>0(绿)
divlong = divergence[1] <= 0 and divergence > 0
// 绿转红:上一根>0(绿),当前≤0(红)
divshort = divergence[1] > 0 and divergence <= 0

alertcondition(divlong, title='Div Long', message='RSI背离:红转绿,Div Long触发')
alertcondition(divshort, title='Div Short', message='RSI背离:绿转红,Div Short触发')

plotshape(divlong, style=shape.triangleup, location=location.bottom, color=green, title="DivUp")
plotshape(divshort, style=shape.triangledown, location=location.top, color=red, title="DivDown")

运行效果说明

  • 信号触发点和理论上应当触发警报的点位完全匹配,都是信号线穿越0轴发生颜色切换的瞬间
  • 不会在单方向的颜色持续阶段重复触发警报,仅在换色的当根K线触发一次
  • 脚本运行后前1根K线因为没有历史周期值不会触发信号,属于正常现象
  • 警报的提示文字可根据自身需求在alertcondition的message参数中自行修改

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.13 17:36:07