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 ofget(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
//@chartscript 1
indicator("Last level")
const levels = array.from(10.0, 20.0, 30.0)
plot(levels.get(3))Fixed
//@chartscript 1
indicator("Last level")
const levels = array.from(10.0, 20.0, 30.0)
plot(levels.get(levels.size() - 1))