如何延迟 pine 脚本中生成的警报,如果图表时间范围为 10 米,任何人都可以帮助将警报延迟 n 秒吗?

How to delay alerts generated in pine script, can anyone help in delaying alerts by n number of seconds if chart timeframe is 10m?

我在一秒钟内收到太多警报(相同的 code/indicator 应用于不同的 stocks/symbols),我想将每个警报延迟至少 5 秒。 我已经尝试使用 pinecoders.com https://www.pinecoders.com/faq_and_code/#how-can-i-implement-a-time-delay-between-orders 提供的这个指标代码来为我的 10m 策略提供延迟(5 秒)但是 它不起作用 因为我使用的是 10m 图表因为我想得到延迟 5 秒的警报。

任何人都可以帮助将警报延迟 1-5 秒吗?

@e2e4 谢谢你的回复,我想在更长的时间范围内测试一下(即使有不一致)。包含代码的 link 旨在仅在柱之间提供延迟,但我想在蜡烛收盘后延迟 5 秒。我尝试了以下代码

//@version=4
strategy("Strat with time delay", overlay=true)

i_qtyTimeUnits  = - input(5, "Quantity", minval = 0)
i_timeUnits     = input("seconds", "Delay ", options = ["seconds", "minutes", "hours", "days", "months", "years"])


int _timeFrom_ = na        
_year_   =  (i_timeUnits == "year"   ? int(i_qtyTimeUnits) : 0)
_month_  =(i_timeUnits == "month"  ? int(i_qtyTimeUnits) : 0)
_day_   =  (i_timeUnits == "day"    ? int(i_qtyTimeUnits) : 0)
_hour_   = (i_timeUnits == "hour"   ? int(i_qtyTimeUnits) : 0)
_minute_ =  (i_timeUnits == "minute" ? int(i_qtyTimeUnits) : 0)
_second_ =  (i_timeUnits == "second"  ? int(i_qtyTimeUnits) : 0)
// Return the resulting time in ms Unix time format.
_timeFrom_ := timestamp(_year_, _month_, _day_, _hour_, _minute_, _second_)
// Entry conditions.
ma = sma(close, 5)
goLong = close > ma?1:0
goShort = close < ma?1:0

// Time delay filter
var float lastTradeTime = na
if nz(change(goLong), time)
    // An order has been executed; save the bar's time.
    lastTradeTime := timenow
var float lastTradeTime_s = na
if nz(change(goShort), time)
    // An order has been executed; save the bar's time.
    lastTradeTime_s := timenow

delayElapsed_long = timenow > (lastTradeTime+_timeFrom_)
delayElapsed_short = timenow >  (lastTradeTime_s+_timeFrom_)

if goLong==1 and delayElapsed_long and barstate.isconfirmed
    strategy.entry("Long", strategy.long, comment="Long")
if goShort==1 and delayElapsed_short and barstate.isconfirmed
    strategy.entry("Short", strategy.short, comment="Short")

plot(ma, "MA", goLong ? color.lime : color.red)

        

我只想将警报延迟 5 秒。但上面的代码似乎不起作用。请帮忙

我找到了这个问题的解决方案。 解决方案在于 calc_on_every_tick=true 和下面更新的代码,它对我有用。 我的基本想法是在不同的股票中设置警报,因为警报是同时生成的(我有一些基于时间的标准)在一秒钟内有太多请求导致订单被拒绝。 下面的代码是这个问题的答案。

下面我计算了 bar 的剩余关闭时间,并为每个警报设置了不同的“i_qtyTimeUnits”,这导致警报延迟,即使同时产生 100 个警报。 5秒的间隔不会导致取消订单。

//@version=4
strategy("Strat with time delay", overlay=true,calc_on_every_tick=true)

i_qtyTimeUnits  = input(5, "Quantity", minval = 0)
i_timeUnits     = input("seconds", "Delay between entries", options = ["seconds", "minutes", "hours", "days", "months", "years"])


int _timeFrom_ = na        
_year_   =  (i_timeUnits == "year"   ? int(i_qtyTimeUnits) : 0)
_month_  =(i_timeUnits == "month"  ? int(i_qtyTimeUnits) : 0)
_day_   =  (i_timeUnits == "day"    ? int(i_qtyTimeUnits) : 0)
_hour_   = (i_timeUnits == "hour"   ? int(i_qtyTimeUnits) : 0)
_minute_ =  (i_timeUnits == "minute" ? int(i_qtyTimeUnits) : 0)
_second_ =  (i_timeUnits == "second"  ? int(i_qtyTimeUnits) : 0)
// Return the resulting time in ms Unix time format.
_timeFrom_ := timestamp(_year_, _month_, _day_, _hour_, _minute_, _second_)
// Entry conditions.
ma = sma(close, 5)
goLong = crossover(close, ma)
goShort = crossunder(close , ma)

timeLeft = (time_close - timenow) / 1000 
vol=label.new(bar_index, na,text=tostring(timeLeft), color=color.red,style=label.style_labeldown, yloc=yloc.abovebar)
label.delete(vol[1])

if_t=timeLeft<=i_qtyTimeUnits?1:0

vol1=label.new(bar_index[10], na,text=tostring(i_qtyTimeUnits)+'\n '+tostring(if_t), color=color.lime,style=label.style_labelup, yloc=yloc.abovebar)
label.delete(vol1[1])


if goLong and timeLeft<=i_qtyTimeUnits
    strategy.entry("Long", strategy.long, comment="Long")
    
if goShort and timeLeft<=i_qtyTimeUnits
    strategy.entry("Short", strategy.short, comment="Short")

plot(ma, "MA", goLong ? color.lime : color.red)