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

badshah_e_alam

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

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

@e2e4 感谢您的回复,我想在更高的时间范围内进行测试(即使存在不一致)。包含代码的链接旨在仅在柱之间提供延迟,但我想在蜡烛关闭后延迟 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 秒。但上面的代码似乎不起作用。请帮忙。

badshah_e_alam

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

下面我计算了关闭柱线的剩余时间,并为每个警报设置了不同的“i_qtyTimeUnits”,这会导致警报延迟,即使同时生成 100 个警报。有5s的差距不会导致订单取消。

//@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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章