Search code examples
pine-scriptpine-script-v5tradingview-api

Run code at custom duration timeout intervals


I want to execute code in a TradingView indicator at custom timeout intervals, specifically when a new bar appears for multiple custom timeframes. I'm using Pine Script and encountering strange runtime behavior.

Initially, the script seems to wait correctly for the interval, but then it starts logging every second instead of respecting the specified timeframe.

Here's my code:

tf = input.timeframe("30S", "Timeframe", group="RSI Settings")
var int lastAlertTimeBull = na
var int lastAlertTimeBear = na

intervalMillis = timeframe.in_seconds(tf) * 1000

if barstate.isrealtime
    currentMillis = time(tf)

    if na(lastAlertTimeBull)
        lastAlertTimeBull := currentMillis
    else
        if (currentMillis - lastAlertTimeBull >= intervalMillis)
            log.info(str.format("{0} - {1}", currentMillis - lastAlertTimeBull, intervalMillis))
            lastAlertTimeBull := currentMillis // Update the last alert time only once

Actual Behavior Instead of logging only at the specified intervals (30 seconds in this case), the logs behave as follows:

Initially, there’s a delay (which is expected for the first interval). Then, the script starts logging every second:

[2024-12-07T12:10:18.434-05:00]: 30,000 - 30,000
[2024-12-07T12:10:19.305-05:00]: 30,000 - 30,000
[2024-12-07T12:10:20.236-05:00]: 30,000 - 30,000
[2024-12-07T12:10:21.435-05:00]: 30,000 - 30,000
[2024-12-07T12:10:22.334-05:00]: 30,000 - 30,000
[2024-12-07T12:10:23.206-05:00]: 30,000 - 30,000
[2024-12-07T12:10:24.405-05:00]: 30,000 - 30,000
[2024-12-07T12:10:25.335-05:00]: 30,000 - 30,000
[2024-12-07T12:10:26.234-05:00]: 30,000 - 30,000
[2024-12-07T12:10:27.434-05:00]: 30,000 - 30,000
[2024-12-07T12:10:29.235-05:00]: 30,000 - 30,000

Expected Behavior The script should log messages only at the specified interval (30 seconds) without spamming logs every second.

What I've Tried

  • Ensuring the lastAlertTimeBull variable updates only when the condition is met.
  • Using time(tf) for accurate timestamp comparison.
  • Debugging the initialization of lastAlertTimeBull to handle na values properly. However, the issue persists, and the logging frequency increases unexpectedly after the first interval.

Question How can I achieve consistent logging at the specified timeframe intervals (e.g., 30 seconds) without this unexpected behavior? Is there something wrong with my logic or the way Pine Script handles time(tf)? Any suggestions or alternative approaches are welcome!


Solution

  • When you run your indicator in real time and want to save some intra-bar activities, you need to use the varip keyword, instead of var.

    varip

    varip (var intrabar persist) is the keyword used for the assignment and one-time initialization of a variable or a field of a user-defined type. It’s similar to the var keyword, but variables and fields declared with varip retain their values between executions of the script on the same bar.

    Change your variable types to varip and it will work.

    varip int lastAlertTimeBull = na
    varip int lastAlertTimeBear = na
    

    enter image description here