Search code examples
pine-script

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


I am getting too many alerts in one second (same code/indicator applied to different stocks/symbols) and i want to delay each alert by at least 5 second. I have tried using this indicator code provided by the pinecoders.com https://www.pinecoders.com/faq_and_code/#how-can-i-implement-a-time-delay-between-orders to give delay(5sec) to my 10m strategy but its not working because I am using 10m chart for and i want to get alert delayed by 5sec.

can anyone help in delaying alerts by 1-5 seconds?

@e2e4 thank you for replying, i want to test that in higher timeframe(even having inconsistency) . The link containing the code is designed to give delay only in between the bar, but i want to delay 5 sec when after the close of the candle. I tried the following code

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

        

i just want to delay the alert by 5 seconds. but the above code does not seems to work. please help.


Solution

  • i got the solution to this problem. The solution lies in calc_on_every_tick=true and the below updated code ,it worked for me. My basic idea was to set alert in different stocks, since the alerts are generated at the same time (I have some time based criteria) in one second there were too many request resulting in rejection of the order. below code is the Answer to this problem.

    Below I calculated the time left to close of the bar and set "i_qtyTimeUnits" different for each alert, which results in delay of alert even if 100 alerts generate on the same time. having a gap of 5s will not result in cancelation of the orders.

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