How to delay TradingView Alerts after sometime when the alert has been generated

53 views Asked by At

I'm trying to delay the alert after sometime (say 15 seconds) the alert was generated in TradingView. But the alerts is not storing in the variable. They trigger simultaneously. Is there some other way to do this?.

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

Thanks for your time.

1

There are 1 answers

5
Rakesh Poluri On

I think there is an issue in the declaration of the time delay. Try the following:

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)