CS2401
error name is not declared; declare it with decl before assigning to it.
#What it means
= without let, const or state in front (and +=, -=, ++ and the other assignment operators) changes a variable that already exists. This name was never declared.
#Typical cause
- A first assignment without
let,constorstate. - A typo in the variable name.
#How to fix it
Declare the variable first, let x = … (or state x = … to keep it across bars), then change it with x = …. The editor offers "Declare with let" as a quick fix. See Variables.
#Example
//@chartscript 1
indicator("Up bars")
const total = 0
if (close > open) {
totl = total + 1
}
plot(total)Fixed
//@chartscript 1
indicator("Up bars")
let total = 0
if (close > open) {
total = total + 1
}
plot(total)