如何设置Pine Script仅显示单个红绿交易入场信号,避免连续重复
Pine Script 指标信号去重修改方案
修改核心逻辑
新增状态变量记录历史信号方向,仅当信号方向切换时才输出新信号,避免同方向信号连续绘制。
完整修改后代码
//@version=5 indicator("Master Chef", shorttitle="Master Chef", overlay=true, timeframe="", timeframe_gaps=true) //===== Moving Averages =====// ma(source, length, type) => type == "SMA" ? ta.sma(source, length) : type == "EMA" ? ta.ema(source, length) : type == "SMMA (RMA)" ? ta.rma(source, length) : type == "WMA" ? ta.wma(source, length) : type == "VWMA" ? ta.vwma(source, length) : na show_ma1 = input(true , "MA №1", inline="MA #1") ma1_type = input.string("SMA" , "" , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"]) ma1_source = input(close , "" , inline="MA #1") ma1_length = input.int(20 , "" , inline="MA #1", minval=1) ma1_color = input(#f6c309, "" , inline="MA #1") ma1 = ma(ma1_source, ma1_length, ma1_type) plot(show_ma1 ? ma1 : na, color = ma1_color, title="MA №1") show_ma2 = input(true , "MA №2", inline="MA #2") ma2_type = input.string("SMA" , "" , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"]) ma2_source = input(close , "" , inline="MA #2") ma2_length = input.int(50 , "" , inline="MA #2", minval=1) ma2_color = input(#fb9800, "" , inline="MA #2") ma2 = ma(ma2_source, ma2_length, ma2_type) plot(show_ma2 ? ma2 : na, color = ma2_color, title="MA №2") show_ma3 = input(true , "MA №3", inline="MA #3") ma3_type = input.string("SMA" , "" , inline="MA #3", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"]) ma3_source = input(close , "" , inline="MA #3") ma3_length = input.int(100 , "" , inline="MA #3", minval=1) ma3_color = input(#fb6500, "" , inline="MA #3") ma3 = ma(ma3_source, ma3_length, ma3_type) plot(show_ma3 ? ma3 : na, color = ma3_color, title="MA №3") show_ma4 = input(true , "MA №4", inline="MA #4") ma4_type = input.string("SMA" , "" , inline="MA #4", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"]) ma4_source = input(close , "" , inline="MA #4") ma4_length = input.int(200 , "" , inline="MA #4", minval=1) ma4_color = input(#f60c0c, "" , inline="MA #4") ma4 = ma(ma4_source, ma4_length, ma4_type) plot(show_ma4 ? ma4 : na, color = ma4_color, title="MA №4") //===== RSI =====// lenRSI = input.int(14, minval=1, title="Length") src = input(close, "Source") upRSI = ta.rma(math.max(ta.change(src), 0), lenRSI) downRSI = ta.rma(-math.min(ta.change(src), 0), lenRSI) rsi = downRSI == 0 ? 100 : upRSI == 0 ? 0 : 100 - (100 / (1 + upRSI / downRSI)) //===== DMI+/-=====// lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50) lenDMI = input.int(14, minval=1, title="DI Length") upDMI = ta.change(high) downDMI = -ta.change(low) plusDMI = na(upDMI) ? na : (upDMI > downDMI and upDMI > 0 ? upDMI : 0) minusDMI = na(downDMI) ? na : (downDMI > upDMI and downDMI > 0 ? downDMI : 0) trur = ta.rma(ta.tr, lenDMI) plus = fixnan(100 * ta.rma(plusDMI, lenDMI) / trur) minus = fixnan(100 * ta.rma(minusDMI, lenDMI) / trur) sum = plus + minus adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig) //===== Defining parameters =====// //Long long = rsi >= 50 and rsi <= 90 and plusDMI > minusDMI and adx >= 25 //Short short = rsi <= 50 and rsi >= 10 and plusDMI < minusDMI and adx >= 25 //===== 新增:信号去重逻辑 =====// // 标记上一次触发的信号方向:1=多,-1=空,0=无历史信号 var int last_signal_dir = 0 // 仅当信号方向与上一次不同时才触发输出 long_trigger = long and last_signal_dir != 1 short_trigger = short and last_signal_dir != -1 // 更新信号方向标记 if long_trigger last_signal_dir := 1 if short_trigger last_signal_dir := -1 //===== Plot signals to chart =====// //Long plotshape(long_trigger, title = "Long", location = location.belowbar, color = color.rgb(0, 250, 0), size = size.tiny, style = shape.diamond) //Short plotshape(short_trigger, title = "Short", location = location.abovebar, color = color.rgb(250, 0, 0), size = size.tiny, style = shape.diamond) // //===== Create Alerts =====// alertcondition(long_trigger or short_trigger, title = "Master Chef", message= "🍴 Signal: {{ticker}}, {{interval}}")
效果说明
修改后同方向信号仅会在首次触发时显示1次,直到反向信号出现后才会输出新的信号,不会连续出现多个同方向菱形标记。
内容的提问来源于stack exchange,提问作者EdmasterEd
相关产品推荐
相关产品推荐

