为TradingView PineScript指标添加RSI线变色警报请求
需求实现:RSI颜色切换时触发警报
我需要在TradingView的PineScript指标中添加警报条件,当RSI线从白色变为红色或绿色时触发警报。图中白色箭头标注了警报触发的位置,尝试自行添加但无法实现,请求技术协助。
原代码
//@version=3 study(shorttitle = "TTS", title="Complete Trend Trading System [Fhenry0331]", overlay=false) myemalength = input(26) myema = ema(low, 26) mvwaplength = input(13) mvwap= ema(vwap, mvwaplength) price=close src = close, len = input(14, minval=1, title="Length") up = rma(max(change(src), 0), len) down = rma(-min(change(src), 0), len) rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down)) plot(rsi, color=white) change1= rsi > 60 ? lime :na change2= rsi < 40 ? red: na p1 = plot(rsi, style=linebr, linewidth=4, color=change1) p2 = plot(rsi, style=linebr, linewidth=4, color=change2) band1 = hline(70) band2 = hline(30) band3 = hline(55) band4 = hline (45) midline = hline (50) fill(band1, band3, color=lime, transp=90) fill(band2, band4, color=red, transp=90) fill(band3, band4, color=black, transp=90) cond1 = rsi > 55 and close > myema ? 1 : 0 barcolor(cond1 ? lime : na) cond2 = rsi < 45 and close < myema ? 1 : 0 barcolor(cond2 ? red : na) cond3 = rsi < 50 and close < myema ? 1 : 0 barcolor(cond3 ? yellow : na) cond4 = rsi > 50 and close > myema ? 1 : 0 barcolor(cond4 ? orange : na) cond5 = rsi < 50 and close > myema ? 1 : 0 barcolor(cond5 ? black : na) cond6 = rsi > 50 and close < myema ? 1 : 0 barcolor(cond6 ? black : na) cond7 = rsi > 50 and close < mvwap and close > myema ? 1 : 0 plotchar(cond7, title="Buy pullback", offset=0, char='BuyPB',location=location.top, color=lime, transp=0) cond8 = rsi < 50 and close > mvwap and close < myema ? 1 : 0 plotchar(cond8, title="Sell pullback", offset=0, char='SellPB', location=location.top, color=red, transp=0) cond9 = rsi > 50 and close < mvwap and close > myema ? 1 : 0 plotshape(cond9, style=shape.triangleup, location=location.bottom, color=lime, transp=0, offset=0) cond10 = rsi < 50 and close > mvwap and close < myema ? 1 : 0 plotshape(cond10, style=shape.triangledown, location=location.bottom, color=red, transp=0, offset=0) cond11 = rsi > 75 and close > myema ? 1 : 0 plotchar(cond11, title="take some profits", offset=0, char='TP', color=#90bff9, transp=0) cond12 = rsi < 25 and close < myema ? 1 : 0 plotchar(cond12, title="take some profits", offset=0, char='TP', color=#b39ddb, transp=0) //Alerts alertcondition (cond11, title="Close Trade") alertcondition (cond12, title="Close Trade") alertcondition (cond1, title="Open Buy") alertcondition (cond2, title="Open Sell")
修改后的代码(新增RSI颜色切换警报)
//@version=3 study(shorttitle = "TTS", title="Complete Trend Trading System [Fhenry0331]", overlay=false) myemalength = input(26) myema = ema(low, 26) mvwaplength = input(13) mvwap= ema(vwap, mvwaplength) price=close src = close, len = input(14, minval=1, title="Length") up = rma(max(change(src), 0), len) down = rma(-min(change(src), 0), len) rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down)) plot(rsi, color=white) change1= rsi > 60 ? lime :na change2= rsi < 40 ? red: na p1 = plot(rsi, style=linebr, linewidth=4, color=change1) p2 = plot(rsi, style=linebr, linewidth=4, color=change2) band1 = hline(70) band2 = hline(30) band3 = hline(55) band4 = hline (45) midline = hline (50) fill(band1, band3, color=lime, transp=90) fill(band2, band4, color=red, transp=90) fill(band3, band4, color=black, transp=90) cond1 = rsi > 55 and close > myema ? 1 : 0 barcolor(cond1 ? lime : na) cond2 = rsi < 45 and close < myema ? 1 : 0 barcolor(cond2 ? red : na) cond3 = rsi < 50 and close < myema ? 1 : 0 barcolor(cond3 ? yellow : na) cond4 = rsi > 50 and close > myema ? 1 : 0 barcolor(cond4 ? orange : na) cond5 = rsi < 50 and close > myema ? 1 : 0 barcolor(cond5 ? black : na) cond6 = rsi > 50 and close < myema ? 1 : 0 barcolor(cond6 ? black : na) cond7 = rsi > 50 and close < mvwap and close > myema ? 1 : 0 plotchar(cond7, title="Buy pullback", offset=0, char='BuyPB',location=location.top, color=lime, transp=0) cond8 = rsi < 50 and close > mvwap and close < myema ? 1 : 0 plotchar(cond8, title="Sell pullback", offset=0, char='SellPB', location=location.top, color=red, transp=0) cond9 = rsi > 50 and close < mvwap and close > myema ? 1 : 0 plotshape(cond9, style=shape.triangleup, location=location.bottom, color=lime, transp=0, offset=0) cond10 = rsi < 50 and close > mvwap and close < myema ? 1 : 0 plotshape(cond10, style=shape.triangledown, location=location.bottom, color=red, transp=0, offset=0) cond11 = rsi > 75 and close > myema ? 1 : 0 plotchar(cond11, title="take some profits", offset=0, char='TP', color=#90bff9, transp=0) cond12 = rsi < 25 and close < myema ? 1 : 0 plotchar(cond12, title="take some profits", offset=0, char='TP', color=#b39ddb, transp=0) // 新增RSI颜色切换警报条件 rsi_white_prev = rsi[1] >= 40 and rsi[1] <= 60 // 上一根K线RSI为白色(40-60区间) rsi_turn_green = rsi > 60 and rsi_white_prev // 当前从白色变为绿色 rsi_turn_red = rsi < 40 and rsi_white_prev // 当前从白色变为红色 // 绘制切换标记(可选,方便验证) plotshape(rsi_turn_green, style=shape.arrowup, location=location.belowbar, color=lime, size=size.small) plotshape(rsi_turn_red, style=shape.arrowdown, location=location.abovebar, color=red, size=size.small) // 添加警报 alertcondition(rsi_turn_green, title="RSI从白转绿", message="RSI线从白色变为绿色,触发警报") alertcondition(rsi_turn_red, title="RSI从白转红", message="RSI线从白色变为红色,触发警报") // 原有警报 alertcondition (cond11, title="Close Trade") alertcondition (cond12, title="Close Trade") alertcondition (cond1, title="Open Buy") alertcondition (cond2, title="Open Sell")
新增逻辑说明
rsi_white_prev:判断上一根K线的RSI是否处于40-60区间(对应白色线)rsi_turn_green:当前K线RSI突破60,且上一根是白色,即从白转绿rsi_turn_red:当前K线RSI跌破40,且上一根是白色,即从白转红- 新增的
plotshape用于在K线上标记切换位置,方便验证警报触发点是否正确 - 最后添加两个
alertcondition,分别对应两种颜色切换的警报触发
内容的提问来源于stack exchange,提问作者Benjamin N
相关产品推荐
相关产品推荐

