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

Pine Script v5指标优化:实现单次BUY/SELL信号输出

Pine Script v5 解决连续重复信号问题

你的指标当前会在条件持续满足时连续输出多次买卖信号,要实现仅在条件首次触发时输出1次信号,核心思路是判断「当前K线满足条件,且前一根K线不满足该条件」,确保信号只在条件转变的瞬间触发。

修改方案

在原代码基础上,新增两个信号变量过滤重复触发:

  • 做多信号:添加not long[1]判断,仅当当前首次满足做多条件时触发
  • 做空信号:添加not short[1]判断,仅当当前首次满足做空条件时触发

修改后的完整代码

//@version=5
indicator(title='Bot 1 Indicateurs BUY / SELL', shorttitle='Bot 1 Indicateurs BUY / SELL', overlay=true)
UseHAcandles = input(true, title='Use Heikin Ashi Candles in Algo Calculations')

// === INPUTS ===
AlerteBuy = input('voir buy')
AlerteSell = input('voir buy')

// === BASE FUNCTIONS ===
haClose = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close) : close
haOpen = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open) : open
haHigh = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high) : high
haLow = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low) : low

// 原条件判断
long = haClose > haHigh[1] and haClose > haOpen and haHigh[1] > haHigh[2] and close > high[3]
short = haClose < haOpen and haClose < haLow[1]

// 过滤重复信号:仅当前满足条件且前一根K线不满足时触发
longSignal = long and not long[1]
shortSignal = short and not short[1]

// 告警与绘图使用过滤后的信号
alertcondition(condition=longSignal, title='Buy Signal', message='Short TP Hit @ ${{plot("Short TP")}}')
alertcondition(condition=shortSignal, title='Sell Signal', message='Short TP Hit @ ${{plot("Short TP")}}')

plotshape(longSignal, title='plotbuy', style=shape.labelup, location=location.belowbar, color=color.new(color.blue, 0), size=size.tiny, text='BUY ↗️', textcolor=color.new(color.white, 0) )
plotshape(shortSignal, title='plotsell', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), size=size.tiny, text='SELL ↘️', textcolor=color.new(color.white, 0) )

关键说明

  • longSignal和shortSignal是过滤后的最终信号,仅在条件从「不满足」变为「满足」的那根K线触发
  • 原long和short变量保留作为基础条件判断,不直接用于信号输出
  • 这种方式能彻底避免连续K线满足条件时的重复信号,确保每个信号仅触发一次

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 02:57:18