Search code examples
pine-scriptpine-script-v5tradingview-apipine-script-v4

Running a function every second in Pinescript


I want to call a function every second in pinescript. This function can have an alert. So basically this script would fire an alert every second.

I have tried a security method but it doesn't work.

something like this:

oneSecondInterval() =>
    alert("1s alert")
    1 //just returning value 1 to fulffil the condition that expression in security must return a value.

myvar = request.security(syminfo.tickerid,'1S',oneSecondInterval())

The above code only issues alert at start of the bar and not every second.


Solution

  • This is super unpractical and wouldn't even work.

    If your script triggeres more than 15 alerts in 3 minutes, the alerts will automatically stop.

    If you want to check if it is a new second, you should use a varip variable but still, your script will be executed on each new tick. If there is no trade activity, it might take a while.

    Here is an idea for you:

    varip last_second = second(timenow)
    
    is_new_second = second(timenow) != last_second
    last_second := is_new_second ? second(timenow) : last_second
    

    Then use is_new_second as a condition to trigger your alert.