FlowscopeDocs

T413

indicator length must be known before the script runs

Severity Error

The length (window) argument of a ta.* call is computed while the script runs, for example from state or bar data. Indicator memory is reserved once at start, so the length must be fixed by then.

Use a literal, a setup value or an input. To vary the effective window at run time, keep values in a rolling buffer and aggregate it yourself.

Failing example

Flowscope Script
script "Adaptive SMA"
data chart = subscribe(data.ohlcv)
state len: int = 20
plot sma = plot.line(title: "Adaptive SMA")
on chart.close {
  len = chart.isRealtime ? 10 : 20
  sma.plot(ta.sma(chart.close, len))
}

Fixed example

Flowscope Script
script "Adaptive SMA"
input len = input.int(20, title: "Length", min: 2, max: 500)
data chart = subscribe(data.ohlcv)
plot sma = plot.line(title: "SMA")
on chart.close {
  sma.plot(ta.sma(chart.close, len))
}