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 withlet.
#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
//@chartscript 1
indicator("Counter")
let count = 0
function bump() {
count = count + 1
}
bump()
plot(count)Fixed
//@chartscript 1
indicator("Counter")
state count = 0
function bump() {
count = count + 1
}
bump()
plot(count)