Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS5101

error Index index is out of range (size size).

#What it means

The script read or wrote an element of an array (or a matrix, or a table cell) at a position that does not exist. The message gives the index and the size. Positions count from 0, so the last element of an array of size 3 is at index 2.

#Typical cause

  • Reading get(size) instead of get(size - 1).
  • A loop that runs one step too far.
  • Reading from an array that is still empty on the first bars.

#How to fix it

Check the size before reading (if xs.size() > i), or use negative indices to count from the end (xs.get(-1) is the last element). See Collections.

#Example

ChartScriptOpen in editor
//@chartscript 1
indicator("Last level")
const levels = array.from(10.0, 20.0, 30.0)
plot(levels.get(3))

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Last level")
const levels = array.from(10.0, 20.0, 30.0)
plot(levels.get(levels.size() - 1))
  • CS5102 — name is na.
  • CS5106 — The matrix sizes do not fit (detail).

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