Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS2501

error name calls itself (through path); recursion is not allowed.

#What it means

A function calls itself, directly or through other functions (the message shows the chain). Recursion is not allowed in ChartScript, because every call keeps its own state per bar.

#Typical cause

  • A recursive definition, such as a factorial or a tree walk.

#How to fix it

Rewrite it with a loop. See Loops and Functions.

#Example

ChartScriptError: CS2501
//@chartscript 1
indicator("Factorial")
const fact = (n) => n <= 1 ? 1 : n * fact(n - 1)
plot(fact(5))

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Factorial")
function fact(n) {
    let result = 1
    for (let i = 2; i <= n; i++) {
        result *= i
    }
    return result
}
plot(fact(5))
  • CS2502 — Functions cannot be declared inside other functions.

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