N001
value may be null here
Severity Error
A nullable value (T?) is used where a non-null value is required: as a condition, as an argument to a parameter of type T, as a collection element that cannot be null, or assigned to a non-nullable variable. Market fields are nullable because a bar can be missing, so this is the most common diagnostic in new scripts.
Check the value first (if x != null { ... }, after which x counts as non-null inside the block), or supply a fallback with x ?? fallback. Conditions can use ?? false.
Failing example
script "Close in ticks"
data chart = subscribe(data.ohlcv)
fn toTicks(price: float) -> float {
return price / context.tickSize
}
pane p = pane(title: "Ticks", height: 0.2)
plot t = plot.line(title: "Close (ticks)", on: p)
on chart.close {
t.plot(toTicks(chart.close))
}Fixed example
script "Close in ticks"
data chart = subscribe(data.ohlcv)
fn toTicks(price: float) -> float {
return price / context.tickSize
}
pane p = pane(title: "Ticks", height: 0.2)
plot t = plot.line(title: "Close (ticks)", on: p)
on chart.close {
let c = chart.close
if c != null {
t.plot(toTicks(c))
}
}