在Pine Script中为RSI指标添加交叉警报的技术求助
Pine Script RSI交叉警报设置问题
我正在用Pine Script搭建交易机器人策略,目前RSI信号运行正常,但在警报设置环节遇到问题。需要为RSI添加交叉警报:当RSI上穿30时触发买入警报,下穿70时触发卖出警报。以下是我的代码:
//input len = input.int(title='RSI len', defval=14, inline='1') src = input.source(title='Source', defval=close, inline='1') upli = input.int(title='Up line limit', defval=70, inline='2') lowli = input.int(title='Down line limit', defval=30, inline='2') theme = input.string(title='Mode', defval='Dark', options=['Dark', 'Light']) //rsi rsilevel = ta.rsi(src, len) //calculations RSI lines ob = 100 upper = ob - lowli lower = ob - upli center = ob - 50 zero = 0 //mode settings color colorred = na color colorgreen = na color coloryellow = na color colorwhite = na color coloraqua = na modeDark = if theme == 'Dark' colorred := color.red colorgreen := color.green coloryellow := color.yellow colorwhite := color.white coloraqua := color.aqua coloraqua modeLight = if theme == 'Light' colorred := color.red colorgreen := color.green coloryellow := color.blue colorwhite := color.green coloraqua := color.yellow coloraqua //display RSI lines (center, overbought, oversold) upline = plot(upper, color=colorred, title='Up line', linewidth=2) dnline = plot(lower, color=colorgreen, title='Down line', linewidth=2) ctr = plot(center, color=colorwhite, title='Center line', transp=50) //display rsi rsiover = plot(rsilevel, color=color.rgb(148, 41, 148), title='RSI', transp=50) //calculations RSI overbought and oversold lines for filling lineupper = rsilevel > upper ? upper : rsilevel linelower = rsilevel < lower ? lower : rsilevel lu = plot(lineupper, color=colorred, title='Overbought', transp=100) ll = plot(linelower, color=colorgreen, title='Oversold', transp=100) //filling lines fill(ll, rsiover, color=colorgreen, title='Fill background oversold zone', transp=20) fill(lu, rsiover, color=colorred, title='Fill background overbought zone', transp=20) fill(ctr, upline, color=colorred, title='Fill background from center line to Up line', transp=100) fill(ctr, dnline, color=colorgreen, title='Fill background from center line to Down line', transp=100) ma1(source, length, type) => switch type "SMA" => ta.sma(source, length) "Bollinger Bands1" => ta.sma(source, length) "EMA1" => ta.ema(source, length) "SMMA (RMA)1" => ta.rma(source, length) "WMA1" => ta.wma(source, length) "VWMA1" => ta.vwma(source, length) rsiLengthInput = input.int(100, minval=1, title="RSI Length", group="RSI Settings") rsiSourceInput = input.source(close, "Source", group="RSI Settings") maTypeInput1 = input.string("SMA1", title="MA Type", options=["SMA1", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings") maLengthInput1 = input.int(14, title="MA Length", group="MA Settings") bbMultInput1 = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings") up1 = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput) down1 = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput) rsi1 = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - (100 / (1 + up1 / down1)) rsiMA1 = ma1(rsi1, maLengthInput1, maTypeInput1) isBB1 = maTypeInput1 == "Bollinger Bands" plot(rsi1, "RSI 2", color.rgb(199, 226, 47)) rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86) hline(50, "RSI Middle Band", color=color.new(#787B86, 50)) rsiLowerBand1 = hline(30, "RSI Lower Band", color=#787B86) fill(rsiUpperBand, rsiLowerBand1, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill") bbUpperBand1 = plot(isBB1 ? rsiMA1 + ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Upper Bollinger Band", color=color.green) bbLowerBand1 = plot(isBB1 ? rsiMA1 - ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Lower Bollinger Band", color=color.green) fill(bbUpperBand1, bbLowerBand1, color= isBB1 ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill") if ta.crossover(upli,70) label.new(bar_index, 75, text = 'Sell', color= color.rgb(201, 60, 55), textcolor = color.rgb(252, 249, 249), style=label.style_label_down) if ta.crossover(30, len) label.new(bar_index, 20, text = 'Buy', color= color.rgb(40, 180, 45), textcolor = #ffffff, style=label.style_label_up)
问题分析
现有代码的警报触发逻辑存在两处核心错误:
ta.crossover(upli,70):参数顺序颠倒,且upli默认值就是70,该条件永远不会触发。正确逻辑应为判断RSI数值下穿上限阈值。ta.crossover(30, len):错误用RSI周期长度len代替RSI实时数值,逻辑完全不成立,正确逻辑应为判断RSI数值上穿下限阈值。
另外,Pine Script需通过alertcondition()函数定义警报条件,才能在TradingView的警报设置中选择对应信号。
修正后的代码
替换原代码末尾的标签判断部分为以下内容,同时添加警报条件定义:
// 定义正确的交叉触发条件 buyCondition = ta.crossover(rsilevel, lowli) // RSI上穿下限阈值(默认30) sellCondition = ta.crossunder(rsilevel, upli) // RSI下穿上限阈值(默认70) // 添加警报条件,用于TradingView警报设置 alertcondition(buyCondition, title="RSI买入警报", message="RSI上穿" + str.tostring(lowli) + ",触发买入信号") alertcondition(sellCondition, title="RSI卖出警报", message="RSI下穿" + str.tostring(upli) + ",触发卖出信号") // 显示触发标签 if buyCondition label.new(bar_index, 20, text = 'Buy', color= color.rgb(40, 180, 45), textcolor = #ffffff, style=label.style_label_up) if sellCondition label.new(bar_index, 75, text = 'Sell', color= color.rgb(201, 60, 55), textcolor = color.rgb(252, 249, 249), style=label.style_label_down)
使用说明
修正后,在TradingView中打开脚本的警报设置界面,即可选择“RSI买入警报”或“RSI卖出警报”作为触发条件,设置对应的通知方式。
内容的提问来源于stack exchange,提问作者rony sen
相关产品推荐
相关产品推荐

