Edgoro
Runtime error · Runtime limits

Pine Script: Loop is too long (> 500 ms) (fix)

Pine Script stops any loop that runs longer than 500 ms on one bar. Why nested loops hit the limit and how to replace them with built-in functions.

Runtime error on the chart
Loop is too long (> 500 ms)

Cause

A for or while loop took more than 500 milliseconds on a single bar. Nested loops over hundreds of past bars, repeated on every bar of the chart, are the usual cause.

Broken code

This script compiles, but stops on the chart with the error above.

Pine Script v6
//@version=6
indicator("Breakouts in the last N bars")
lookback = input.int(2000, "Bars to scan")
length   = input.int(500, "Breakout length")
int breakouts = 0
for i = 0 to lookback - 1
    float hh = high[i + 1]
    for j = 2 to length
        hh := math.max(hh, high[i + j])
    if close[i] > hh
        breakouts += 1
plot(breakouts)

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
indicator("Breakouts in the last N bars")
lookback = input.int(2000, "Bars to scan")
length   = input.int(500, "Breakout length")
prevHigh   = ta.highest(high, length)[1]
isBreakout = close > prevHigh
breakouts  = math.sum(isBreakout ? 1 : 0, lookback)
plot(breakouts)

Why it works

The broken version recalculates a 500-bar maximum for each of 2,000 past bars, on every bar of the chart: about a million operations per bar. ta.highest() keeps the rolling maximum as a series and math.sum() keeps a rolling count, so the fixed script does a handful of operations per bar and gives the same count once enough history is loaded.

The general rule: a value you recompute in a loop over past bars can usually be calculated once per bar as a series, then read with [] or rolled with a built-in function.

What the limit covers

TradingView's limitations page sets the limit at 500 ms for any loop on any single bar, and the outer loop of nested loops counts as one loop, so it times out first. A separate limit on the script's total run time across all bars also applies and depends on your TradingView plan.

Other ways to stay under it:

  • Exit early with break once the loop has found what it needs.
  • Cap user inputs that set loop lengths with minval and maxval.
  • Run expensive loops only where you need them, for example if barstate.islast for a table.
  • Avoid loops whose length grows with bar_index (for i = 0 to bar_index): they get slower on every bar.

Questions

What is the loop time limit in Pine Script?

500 milliseconds per loop on each bar, according to TradingView's limitations page. The outer loop of nested loops counts as one loop.

Why does the error appear on some charts but not others?

Loops whose length depends on the data, such as for i = 0 to bar_index, take longer on charts with more history or a lower timeframe. A loop that passes on a short chart can fail on a longer one.

The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Still getting an error?

Paste your script and the message into the free Pine Script error fixer: it explains what went wrong and returns a corrected v6 version. Or describe the indicator in plain English and let Edgoro write the code.