Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS2604

error A field default must be a constant (field name).

#What it means

The default value of a field is worked out each time an object is created, so it may not call a function that changes from bar to bar (such as ta.sma). Simple values, including bar values like time or close, are allowed.

#Typical cause

  • A field default that calls a ta.* function or another bar-dependent function.

#How to fix it

Leave the field without a default (it starts as na) and pass the value when you create the object: Level.new(ta.sma(close, 20)).

#Example

ChartScriptError: CS2604
//@chartscript 1
indicator("Level")
type Level {
    price: float = ta.sma(close, 20)
}
const lv = Level.new()
plot(lv.price)

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Level")
type Level {
    price: float
}
const sma20 = ta.sma(close, 20)
const lv = Level.new(sma20)
plot(lv.price)
  • CS2601 — A field cannot be named name.

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