Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS2403

error A function cannot change the global name unless it is declared with kw.

#What it means

A function can read global variables, but it can only change a global declared with state (or live state). A global declared with let is calculated again on every bar, so a change made inside a function would be lost or confusing.

#Typical cause

  • A function that assigns (=, +=, ++ …) to a top-level variable declared with let.

#How to fix it

Declare the global with state if it should keep its value across bars, or return the new value from the function and assign it at the top level. See Functions.

#Example

ChartScriptError: CS2403
//@chartscript 1
indicator("Counter")
let count = 0
function bump() {
    count = count + 1
}
bump()
plot(count)

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Counter")
state count = 0
function bump() {
    count = count + 1
}
bump()
plot(count)
  • CS2404 — name cannot be changed (it is what).
  • CS2401 — name is not declared; declare it with decl before assigning to it.

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