FlowscopeDocs

P006

`while` is not supported

Severity Error

Flowscope Script only has bounded loops, so that every handler call is guaranteed to finish within the step budget. A while loop could run an unknown number of times and is therefore not part of the language.

Rewrite the loop as for i in 0..n { } over a known range, or as for x in xs { } over a collection.

Failing example

Flowscope Script
script "Five-bar volume"
data chart = subscribe(data.ohlcv)
plot vol5 = plot.histogram(title: "Volume, last 5 bars")
state acc: float = 0.0
state i: int = 0
on chart.close {
  acc = 0.0
  i = 0
  while i < 5 {
    acc += chart.volume[i] ?? 0.0
    i += 1
  }
  vol5.plot(acc)
}

Fixed example

Flowscope Script
script "Five-bar volume"
data chart = subscribe(data.ohlcv)
plot vol5 = plot.histogram(title: "Volume, last 5 bars")
state acc: float = 0.0
on chart.close {
  acc = 0.0
  for i in 0..5 {
    acc += chart.volume[i] ?? 0.0
  }
  vol5.plot(acc)
}