Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS2503

warning name inside a condition only updates on the bars where this line runs. Move it out of the condition for the usual result.

#What it means

This is a warning. Functions such as ta.sma keep a running state that advances each time the line runs. Inside a condition (an if, or one side of ?:) the line does not run on every bar, so the function only sees the bars where the condition held and its result differs from the usual one.

#Typical cause

  • A ta.* call (or another call with state, or history [n]) inside an if or a ternary.

#How to fix it

Compute the value on every bar at the top level, store it in a variable, and use the variable inside the condition. See Execution model.

#Example

ChartScriptWarning: CS2503
//@chartscript 1
indicator("Conditional average", { overlay: true })
const avg = close > open ? ta.sma(close, 10) : close
plot(avg)

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Conditional average", { overlay: true })
const sma10 = ta.sma(close, 10)
const avg = close > open ? sma10 : close
plot(avg)
  • CS2504 — name inside a loop keeps one value per bar: only its last call on each bar counts.

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