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

求助:为Pine Script添加RSI突破HH/LL趋势线的警报功能

在Pine Script中实现RSI突破HH/LL线的警报

问题分析

原代码能绘制HH/LL的水平线,但未持续跟踪当前有效的HH、LL数值,也缺少穿越检测逻辑,导致警报无法触发。需要添加变量保存最新未被突破的HH和LL值,再通过穿越检测函数触发警报。

修改后的完整代码

//@version=5
indicator("RSI Lines with Alerts", overlay=false, max_bars_back = 500)

////INPUTs
inputgroup = "**********INPUTs**********"
shdiv = input.bool(defval = true, title = "Show Divergences", group=inputgroup)
shpoints = input.bool(defval = true, title = "Show Points", group=inputgroup)
shdate = input.bool(defval = true, title = "Show Dates", group=inputgroup)
shtline = input.bool(defval = true, title = "Show Trend Lines", group=inputgroup)
shbg = input.bool(defval = false, title = "Show StdDev Background", group=inputgroup)

////RSI
rsigroup = "**********RSI**********"
len = input.int(14, minval=1, title='RSI Length', group = rsigroup)
src = input(close, 'RSI Source', group = rsigroup)
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
plot(rsi, 'RSI', color=color.new(#7E57C2, 0))

band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")

////STDDEV
drawLine(X1, Y1, X2, Y2, ExtendType, Color, LineStyle) =>
    var line Line = na
    Line := line.new(X1, Y1, X2, Y2, xloc.bar_index, ExtendType, Color, LineStyle, 1)
    line.delete(Line[1])

rsdcr2(PeriodMinusOne, Deviations, Estimate) =>
    var period = PeriodMinusOne + 1
    var devDenominator = Estimate == 'Unbiased' ? PeriodMinusOne : period
    Ex = 0.0
    Ex2 = 0.0
    Exy = 0.0
    Ey = 0.0
    for i = 0 to PeriodMinusOne by 1
        closeI = nz(rsi[i])
        Ex := Ex + i
        Ex2 := Ex2 + i * i
        Exy := Exy + closeI * i
        Ey := Ey + closeI
        Ey
    ExEx = Ex * Ex
    slope = Ex2 == ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
    linearRegression = (Ey - slope * Ex) / period
    intercept = linearRegression + bar_index * slope
    deviation = 0.0
    for i = 0 to PeriodMinusOne by 1
        deviation := deviation + math.pow(nz(rsi[i]) - (intercept - bar_index[i] * slope), 2.0)
        deviation
    deviation := Deviations * math.sqrt(deviation / devDenominator)
    correlate = ta.correlation(rsi, bar_index, period)
    r2 = math.pow(correlate, 2.0)
    [linearRegression, slope, deviation, correlate, r2]
periodTrend = 140
deviationsAmnt = 1.1
estimatorType = 'Unbiased'
var extendType = 'Right'
periodMinusOne = periodTrend - 1
[linReg, slope, deviation, correlate, r2] = rsdcr2(periodMinusOne, deviationsAmnt, estimatorType)
endPointBar = bar_index - periodTrend + 1
endPointY = linReg + slope * periodMinusOne

////TRENDLINEs & ALERTS
trendgroup = "**********TREND LINE & ALERTS**********"
length = input(21, title='Zigzag Length', group=trendgroup)

// 保存当前有效的HH和LL线数值
var float current_hh_line = na
var float current_ll_line = na

r = rsi[length]
l = rsi[14]

ph = ta.pivothigh(rsi,length,length)
pl = ta.pivotlow(rsi,length,length)
valH = ta.valuewhen(ph,r,0)
valL = ta.valuewhen(pl,r,0)
valpH = ta.valuewhen(ph,r,1)
valpL = ta.valuewhen(pl,r,1)

d = math.abs(r)
n = bar_index
label lbl = na
HIH = valH > valpH ? "HH" : na 
HIL = valH < valpH ? "LH" : na
LOL = valL < valpL ? "LL" : na
LOH = valL > valpL ? "HL" : na

// 更新当前HH线值并绘制
if ph and valH > valpH and shtline
    lbl := label.new(n[length],math.max(r,l),HIH,color=#ff1100  ,
      style=label.style_label_down,textcolor=color.white,size=size.tiny)
    label.delete(lbl[1])
    linehh = line.new(n[length],math.max(r,l), bar_index, math.max(r,l), extend=extend.right, color=color.red)
    line.delete(linehh[1])
    current_hh_line := math.max(r,l)  // 保存最新HH线数值

// 更新当前LL线值并绘制
else if pl and valL < valpL and shtline
    lbl := label.new(n[length],math.min(r,l),LOL,color=#009c0c   ,  
      style=label.style_label_up,textcolor=color.white,size=size.tiny)
    label.delete(lbl[1]) 
    linell = line.new(n[length],math.min(r,l), bar_index, math.min(r,l), extend=extend.right, color=color.green)
    line.delete(linell[1])
    current_ll_line := math.min(r,l)  // 保存最新LL线数值

// 检测RSI上穿HH线
rsi_cross_hh = ta.crossover(rsi, current_hh_line)
// 检测RSI下穿LL线
rsi_cross_ll = ta.crossunder(rsi, current_ll_line)

// 定义警报条件
alertcondition(rsi_cross_hh, title="RSI上穿HH线", message="RSI突破最近未被突破的HH水平线")
alertcondition(rsi_cross_ll, title="RSI下穿LL线", message="RSI突破最近未被突破的LL水平线")

// 可选:绘制当前HH/LL线的参考点(方便验证)
plot(current_hh_line, color=color.red, style=plot.style_circles, display=display.data_window)
plot(current_ll_line, color=color.green, style=plot.style_circles, display=display.data_window)

关键修改点

  • 新增current_hh_line和current_ll_line变量,持续保存最新的未被突破的HH、LL水平线数值。
  • 使用ta.crossover和ta.crossunder函数检测RSI与HH/LL线的穿越行为。
  • 通过alertcondition定义警报规则,触发时发送对应提示消息。
  • 可选添加HH/LL线的参考点绘制,方便在数据窗口查看当前跟踪的线值。

使用说明

  1. 将代码复制到TradingView的Pine编辑器中,保存并添加到图表。
  2. 在图表的"警报"面板中,可找到新增的两个警报条件:"RSI上穿HH线"和"RSI下穿LL线"。
  3. 根据需求设置警报的通知方式(邮件、APP推送等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 18:24:57