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 anifor 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
//@chartscript 1
indicator("Conditional average", { overlay: true })
const avg = close > open ? ta.sma(close, 10) : close
plot(avg)Fixed
//@chartscript 1
indicator("Conditional average", { overlay: true })
const sma10 = ta.sma(close, 10)
const avg = close > open ? sma10 : close
plot(avg)#Related
- CS2504 — name inside a loop keeps one value per bar: only its last call on each bar counts.