Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS5301

error A loop ran more than max times on one bar.

#What it means

One loop ran more than the allowed number of passes on a single bar (the message gives the limit, 100,000). The script was stopped so it cannot hang the chart.

#Typical cause

  • A loop bound that is far larger than intended.
  • A while loop whose condition never becomes false.

#How to fix it

Check the loop's bounds and exit condition. Often a running total kept from bar to bar replaces a big loop. See Loops and Performance.

#Example

ChartScriptOpen in editor
//@chartscript 1
indicator("Big loop")
let total = 0.0
for (let i = 0; i <= 200000; i++) {
    total += i
}
plot(total)

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Big loop")
const n = 200000
const total = n * (n + 1) / 2
plot(total)
  • CS5303 — Loops ran more than max times in total 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.