Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS5201

error A history offset cannot be negative or na (got offset).

#What it means

A history offset (x[n]) worked out while the script ran was negative or na. The script can only look back, never forward, and needs a number to do it.

#Typical cause

  • An offset computed from the bar, such as 10 - bar_index, which turns negative.
  • An offset from a calculation that gives na on the first bars.

#How to fix it

Clamp the offset, for example math.max(0, n), and handle na with nz(n). See Execution model.

#Example

ChartScriptOpen in editor
//@chartscript 1
indicator("Look back")
const offset = 10 - bar_index
plot(close[offset])

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Look back")
const offset = math.max(0, 10 - bar_index)
plot(close[offset])
  • CS2302 — A history offset must be a whole number of 0 or more.
  • CS5202 — name[...] reached offset bars back; raise max_bars_back to suggest in indicator().

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