Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS2301

warning name is the same object on every bar, so name[n] returns the same object.

#What it means

This is a warning. A state collection (array, map or matrix) is one object that lives across bars, so name[1] gives you the same object, not a copy of how it looked on the previous bar.

#Typical cause

  • History ([n]) on a collection declared with state or live state.

#How to fix it

Read the past element from the collection itself (for example closes.get(closes.size() - 2)), or keep a separate series of the number you need and use history on that.

#Example

ChartScriptWarning: CS2301
//@chartscript 1
indicator("Stored closes")
state closes: float[] = array.new<float>()
closes.push(close)
const prev = closes[1]
plot(prev.size())

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Stored closes")
state closes: float[] = array.new<float>()
closes.push(close)
plot(closes.size() - 1)
  • CS2302 — A history offset must be a whole number of 0 or more.
  • CS5302 — The array is changed inside the loop that goes through it; the loop goes on with its new size. An index loop says that more clearly.

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