Skip to content
ChartDeck ChartScript docs Account Open the terminal

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, const or state.
  • 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

ChartScriptError: CS2401
//@chartscript 1
indicator("Up bars")
const total = 0
if (close > open) {
    totl = total + 1
}
plot(total)

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Up bars")
let total = 0
if (close > open) {
    total = total + 1
}
plot(total)
  • CS2001 — name is not defined.
  • CS2402 — This declares a new name that hides the one from line line; did you mean to change it with op ?

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