我试图在 TradingView 中生成警报一段时间(例如 15 秒)后延迟警报。但警报未存储在变量中。它们同时触发。还有其他方法可以做到这一点吗?.
//@version=5
indicator("Delayed Alert Example", overlay=true)
delay = input(15, 'Time Delay')*1000 // convert seconds to milliseconds
// Define a variable to store the time of the last alert
var lastBuySignalTime = 0
var lastSellSignalTime = 0
// Condition for the signals
BUY = open < close
SELL = open > close
plotshape(BUY , title='Long', style=shape.labelup, location=location.belowbar)
plotshape(SELL , title='Short', style=shape.labeldown, location=location.abovebar)
// Delay the alert by given time
if BUY
alert("Buy Signal", alert.freq_once_per_bar)
lastBuySignalTime := timenow
if BUY and (lastBuySignalTime >= delay)
alert("Delayed Buy Signal", alert.freq_once_per_bar)
if SELL
alert("SELL Signal", alert.freq_once_per_bar)
lastSellSignalTime := timenow
if SELL and (lastSellSignalTime >= delay)
alert("Delayed SELL Signal", alert.freq_once_per_bar)
感谢您的宝贵时间。
我认为延迟申报有问题。尝试以下操作:
if BUY and (timenow - lastBuySignalTime >= delay)
alert("Delayed Buy Signal", alert.freq_once_per_bar)
if SELL and (timenow - lastSellSignalTime >= delay)
alert("Delayed SELL Signal", alert.freq_once_per_bar)