Cause
The length passed to ta.ema() changes from bar to bar (a "series" value), but the function needs a length that is fixed once the script starts (a "simple" value), such as a number or an input.
Broken code
TradingView reports the error on line 4.
//@version=6
indicator("Adaptive EMA", overlay = true)
len = close > ta.sma(close, 50) ? 10 : 30
plot(ta.ema(close, len))Fixed code
The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.
//@version=6
indicator("Adaptive EMA", overlay = true)
fastEma = ta.ema(close, 10)
slowEma = ta.ema(close, 30)
plot(close > ta.sma(close, 50) ? fastEma : slowEma, "EMA")Why it works
Every Pine Script value has a qualifier that says when it is known: const (when the script compiles), input (from the settings), simple (on the first bar) and series (can change on every bar). A value can be used where a later qualifier is accepted, never an earlier one: a series can't go where simple is required.
len depends on close, so it is a series. An EMA carries its state from one bar to the next, so its length has to stay the same for the whole chart. Calculating both EMAs on every bar and choosing between the results keeps each length fixed and gives the switch you wanted.
Which functions accept a series length
In Pine Script v6, ta.sma(), ta.highest(), ta.lowest() and math.sum() accept a series length, while ta.ema() and ta.rsi() require a simple one. The reference manual lists the qualifier of every parameter.
If you need an EMA whose length truly changes per bar, write it yourself:
emaVar(src, len) =>
float alpha = 2.0 / (len + 1)
var float result = na
result := na(result) ? src : alpha * src + (1 - alpha) * result
result
Questions
What is the difference between simple and series in Pine Script?
A simple value is fixed once the script starts on the first bar, like an input or the chart's timeframe. A series value can be different on each bar, like close or anything calculated from it. Functions whose internal state depends on a parameter staying the same, such as the length of ta.ema(), require simple.
Can ta.ema use a dynamic length?
Not directly. Either calculate an EMA for each length you need and pick one per bar, or write your own EMA function that takes a series length. In Pine Script v6, ta.sma() already accepts a series length.
The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.