Pine Script error: Loop is too long (> 500 ms) and how to fix it
Fix the Pine Script error "Loop is too long (> 500 ms)": cut the iterations, swap loops for built-ins such as math.sum, or keep a running value.
TradingView stops a script with "Loop is too long (> 500 ms)" when a single for or while loop takes longer than 500 milliseconds on one bar. The code itself is valid. It takes too long to run. The fix is to make the loop do less work per bar, usually by replacing it with a built-in function or by keeping a running value instead of recounting everything.
What causes it
Pine runs your script once per bar. Each loop on a bar has a time limit of 500 ms on TradingView, and a loop that runs past it is stopped with this message. Three patterns cause most cases.
Too many iterations
The plainest case is a loop with a huge number of steps. This code FAILS on TradingView:
//@version=6
indicator("Loop")
s = 0.0
for i = 0 to 100000000
s += i
plot(s)
A hundred million additions on every bar is far more work than one bar allows.
Nested loops
A loop inside a loop multiplies the work: a 1,000-step loop inside another 1,000-step loop is a million steps per bar. TradingView counts the outer loop together with everything inside it as one loop, so the outer loop is the one that runs out of time, even though the inner one does most of the work.
Recounting history on every bar
Many scripts loop back over the last N bars on every bar to find a sum, a high or a count. The result for this bar is almost the same as for the last one, so most of that work is repeated. On long charts and large N it adds up.
How to fix it
Pick the fix that matches what the loop is for.
Replace the loop with a built-in
Pine has built-in functions for the jobs loops are most often written for. math.sum() adds up the last N values, ta.highest() and ta.lowest() find the extremes, and ta.sma() averages. Tested version of a rolling sum without a loop:
//@version=6
indicator("Rolling sum without a loop")
len = input.int(50, "Length")
s = math.sum(close, len)
plot(s, "Sum of last closes")
And the highest high and lowest low over a window, again with no loop:
//@version=6
indicator("Highest and lowest without a loop", overlay = true)
len = input.int(200, "Length")
plot(ta.highest(high, len), "Highest high", color.green)
plot(ta.lowest(low, len), "Lowest low", color.red)
Update a running value one bar at a time
If you are counting or adding something since the start of the chart, do not loop over all the past bars. Keep a var variable and change it by one step on each bar. Tested:
//@version=6
indicator("Green bars so far")
var int greens = 0
if close > open
greens += 1
plot(greens, "Green bars")
The work per bar stays the same however long the chart is.
Compute once
If the loop builds something that never changes, such as a table of weights or a list of levels, build it once. Declare the result with var, or wrap the loop in if barstate.isfirst, so it runs on the first bar only.
Cut the iterations
When you need the loop, make it shorter. Loop over the bars you need and no more, and take the length from an input so it can be lowered. For nested loops, see whether the inner loop can be replaced by a built-in, which removes the multiplication.
What SuperCharting's Script Editor shows
SuperCharting stops on this one too. When you run the broken example, the console in the Script Editor shows:
Runtime: line 5, bar 0: loop too long
Line 5 is the s += i line inside the loop, so the number leads you straight to the slow code. The message starts with Runtime: because it comes from the run check, which happens when the script runs on bars after you press Add to chart.
The built-in Auto-fix did not repair this error in our test, so plan to rewrite the loop yourself with one of the fixes above. On accounts where the site owner has enabled it, the deeper AI check can attempt repairs that simple rules cannot. Read every change it makes before you use the script.
To check a script:
- Open a chart and open the Script Editor with Ctrl + E.
- Paste the script into the code area.
- Press Add to chart and read the console for a line that starts with Runtime:.
The Pine Script code checker page explains the difference between the compile check and the run check.
Related errors
Pine cannot determine the referencing length of a series is another limit you meet when a script reaches far back in history. If the loop body will not even compile, the cause is often indentation; see Mismatched input. The Pine Script v6 basics page covers for loops and var.
Frequently asked questions
Is the 500 ms limit for the whole script?
The message is about a single loop on a single bar. Each loop on a bar has to finish within 500 ms on TradingView. The script as a whole also has its own time limit across all bars, so many loops that each pass can still make a script too slow overall.
My script has two loops. Which one is too slow?
Read the line number in the message first. With nested loops, the outer loop is the one that times out because its time includes the inner loop, so look at the inner loop as the likely cause.
Will a while loop behave differently from a for loop?
No. The limit applies to any loop on a single bar.
Will Auto-fix in SuperCharting fix a slow loop?
The built-in Auto-fix did not repair it in testing. Rewrite the loop with one of the fixes above, or try the deeper AI check if your account has it, and read its changes before you rely on them.
TradingView and Pine Script are trademarks of TradingView, Inc. SuperCharting is independent and not affiliated with it.