Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS5303

error Loops ran more than max times in total on one bar.

#What it means

All loops together ran more than the allowed number of passes on a single bar (the message gives the limit, 1,000,000). This often happens with nested loops, where the passes multiply.

#Typical cause

  • Nested loops such as 2,000 x 1,000 passes on every bar.

#How to fix it

Reduce the work per bar: shorten the loops, compute once and reuse the result, or keep running values from bar to bar. See Performance.

#Example

ChartScriptOpen in editor
//@chartscript 1
indicator("Nested loops")
let total = 0.0
for (let i = 0; i <= 1999; i++) {
    for (let j = 0; j <= 999; j++) {
        total += 1
    }
}
plot(total)

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Nested loops")
const total = 2000.0 * 1000.0
plot(total)
  • CS5301 — A loop ran more than max times on one bar.
  • CS5401 — Stopped: the script took too long (ms ms; the limit is max ms).

The ChartScript documentation is in English for now; translations will follow.