请求协助:实现Pine V5中RSI背离线颜色切换的警报功能
解决RSI背离线颜色切换的警报问题
你的代码已经计算出快慢RSI的差值,但缺少**检测差值穿越0轴(颜色切换)**的核心逻辑,因此无法触发警报。以下是修复后的完整代码,同时添加了符合需求的警报触发条件:
//@version=5 indicator(title='RSI Divergence', shorttitle='RSI Divergence') src_fast = close len_fast = input.int(5, minval=1, title='Length Fast RSI') src_slow = close len_slow = input.int(14, minval=1, title='Length Slow RSI') // 计算快慢RSI up_fast = ta.rma(math.max(ta.change(src_fast), 0), len_fast) down_fast = ta.rma(-math.min(ta.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 = ta.rma(math.max(ta.change(src_slow), 0), len_slow) down_slow = ta.rma(-math.min(ta.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 // 获取上一根K线的差值状态,用于对比切换 prev_divergence = ta.valuewhen(ta.barssince(ta.close != ta.close), divergence, 1) // 定义两种颜色切换的触发条件 red_to_lime = prev_divergence <= 0 and divergence > 0 // 从红转浅绿 lime_to_red = prev_divergence >= 0 and divergence < 0 // 从浅绿转红 // 保持原有绘图逻辑 plotdiv = plot(divergence, color=divergence > 0 ? color.lime : color.red, linewidth=2) band = hline(0) // 注册警报条件 alertcondition(red_to_lime, title="红转浅绿", message="RSI背离线从红色切换为浅绿") alertcondition(lime_to_red, title="浅绿转红", message="RSI背离线从浅绿切换为红色")
关键修改说明:
prev_divergence:捕获上一根K线的差值数据,作为判断状态切换的基准。red_to_lime/lime_to_red:明确判断差值穿越0轴的两种场景,这就是颜色切换的核心触发逻辑。alertcondition():将两个切换条件注册为可触发的警报,你可以在TradingView的警报设置面板中,选择对应的条件来启用警报。
使用时,进入TradingView警报设置,选择你的指标后,在「条件」下拉菜单中就能看到新增的两个警报选项。
内容的提问来源于stack exchange,提问作者CantIhavecrypto
相关产品推荐
相关产品推荐

