MMTMMT Docs
Diagnostics

T428

stateful calculation, randomness, or drawing inside a lambda

T428

A lambda runs a varying number of times per update, so it cannot call anything that keeps history, draws random numbers, or writes output, even through helper functions. Compute the values in the lambda, then plot or draw in the handler body.

Failing example

script "conf"
data chart = subscribe(data.ohlcv)
plot out = plot.line(title: "out")
state xs: float[] = float[](4)
fn mark(x: float) -> float {
  out.plot(x)
  return x
}
on chart.update {
  let ys = xs.map(|x| mark(x))
}

Corrected example

script "fixed"
data chart = subscribe(data.ohlcv)
plot out = plot.line(title: "out")
state xs: float[] = float[](4)
on chart.update {
  let ys = xs.map(|x| x * 2.0)
  out.plot(ys.last() ?? 0.0)
}

On this page