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
//@chartscript 1
indicator("Factorial")
const fact = (n) => n <= 1 ? 1 : n * fact(n - 1)
plot(fact(5))Fixed
//@chartscript 1
indicator("Factorial")
function fact(n) {
let result = 1
for (let i = 2; i <= n; i++) {
result *= i
}
return result
}
plot(fact(5))#Related
- CS2502 — Functions cannot be declared inside other functions.