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

PineScript技术求助:获取UT Bot最新买卖信号并触发条件警报

PineScript 策略开发求助:多指标联动警报逻辑问题

我刚接触PineScript,正在开发一个结合三类指标的自动化买卖警报策略,目前在UT Bot指标的逻辑处理上遇到瓶颈:

核心需求

  • 当STC指标(绿色/红色状态)、Linear Regression Candles(收盘价位于信号白线上下)的条件满足时,回溯获取UT Bot指标最后一次发出的buy或sell信号,再触发对应警报
  • 具体警报触发规则:
    • 触发buy警报:UT Bot发出过Buy信号 + STC指标为绿色 + Linear Regression Candle收盘价在信号白线上方
    • 触发sell警报:UT Bot发出过Sell信号 + STC指标为红色 + Linear Regression Candle收盘价在信号白线下方
    • 边缘情况:若UT Bot发出信号,但STC和Linear Regression Candles条件不满足,则不触发警报

现有尝试的缺陷

  1. 第一种实现:仅在UT Bot信号发出的同时满足其他条件才触发警报,后续K线中其他条件满足时无法触发
  2. 第二种实现:用布尔变量记录UT Bot的信号状态,但一旦状态设为true,会在每根K线收盘时持续触发警报

完整代码

//@version=5

strategy(title='Combined indicators', shorttitle='COMBIND', overlay=true)
// UT BOT indicator START
// Inputs
a = input(1, title='Key Vaule. \'This changes the sensitivity\'')
c = input(10, title='ATR Period')
h = input(false, title='Signals from Heikin Ashi Candles')

xATR = ta.atr(c)
nLoss = a * xATR

src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close

xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2

pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3

xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue

ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)

buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below

barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop

plotshape(buy, title='Buy', text='Buy', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.tiny)
plotshape(sell, title='Sell', text='Sell', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), size=size.tiny)

barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)

alertcondition(buy, 'UT Long', 'UT Long')
alertcondition(sell, 'UT Short', 'UT Short')

// ******** My Logic *********
// First Approach: Fails to trigger Alert if conditions are met after the chart continues plotting, because the `buy/sell` signal was flashed x candles ago

// send Bot alert
if sell and close < signal and stcRed // only focus on the sell variable here
    alert("UT Sell+ LIN REG bellow + STC is RED", alert.freq_once_per_bar_close)

if buy and close > signal and stcGreen // only focus on the buy variable here
    alert("UT Buy + LIN REG above + STC is GREEN", alert.freq_once_per_bar_close)


// Second Approach: Change State of buy and sell variables via a boolean check. It fails because once set to true, these will trigger alerts on every single bar close.

var bool buy_signal = false
var bool sell_signal = false 

if buy 
    buy_signal := true 
    sell_signal:= false
    

if sell 
    sell_signal := true
    buy_signal := false


log.info("Buy is: " + str.tostring(buy_signal))
log.info("Sell is: " + str.tostring(sell_signal))



// send Bot alert
if sell_signal and close < signal and stcRed
    alert("UT Sell+ LIN REG bellow + STC is RED", alert.freq_once_per_bar_close)

if buy_signal and close > signal and stcBuy
    alert("UT Buy + LIN REG above + STC is GREEN", alert.freq_once_per_bar_close)

若无法解决该问题,策略将存在缺陷并易导致亏损。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 14:40:58