ITMScript
Missing values
What na is, where it comes from, how it moves through arithmetic and comparisons, and how to test for it, replace it or fill it.
A missing value is written na. It means there is no value on this bar. It is not zero, and it is not false. Every type has one.
Where it comes from
- An operator that needs history returns
nauntil it has enough. This is its warm-up. The operator reference gives the warm-up of each operator. - A division by zero is
na. So is any result outside the range of a number. - A series has a gap where its data has no bar.
- You can write
nayourself, as incond ? value : na.
How it moves
Arithmetic with a missing value is missing. A rolling operator such as ta.sma returns na until it has seen a full window of values, and one missing value in the window starts the count again.
//@itmscript 2
study("Gap", overlay=false)
gap = close > open ? close : na
plot(gap, "Close on up bars", color=color.blue, width=1, style=circles)
plot(gap + 1, "Plus one", color=color.gray, width=1, style=circles)
plot(ta.sma(gap, 3), "Three bar average", color=color.orange, width=2, style=line)The average is missing on every bar whose last three values were not all present. Each operator's page states its own rule under Missing values.
Comparisons and logic
A comparison with a missing value is false. It is never missing. In and and or, a missing value counts as false, and not of a missing value is true.
This differs from languages in which a comparison with a missing value is itself missing. A study written for those needs a second look wherever it compares a value that can be missing.
//@itmscript 2
study("Compare", overlay=true)
slow = ta.sma(close, 50)
above = close > slow
plotshape(above, style=circle, location=belowbar, color=color.green, text="Above")On the first 49 bars slow is missing, so above is false and nothing is marked.
Test, replace and fill
Three tools work with missing values.
| Write | Gives |
|---|---|
na(x) | True where x is missing, and false elsewhere. It is never missing. |
nz(x, r) | x, or r where x is missing. |
fixnan(x) | x, or the last value x had where it is missing. It is missing until the first value. |
//@itmscript 2
study("Fill", overlay=false)
gap = close > open ? close : na
plot(fixnan(gap), "Last close on an up bar", color=color.blue, width=2, style=stepline)
plot(nz(gap, 0), "Zero for a gap", color=color.gray, width=1, style=columns)
plot(na(gap) ? 1 : 0, "Gap flag", color=color.red, width=1, style=histogram)A number is never a condition
A true or false value is never turned into a number, and a number is never turned into a true or false value. ta.sma(close > open, 5) is refused. Write the condition as a number first.
//@itmscript 2
study("Share", overlay=false)
plot(ta.sma(close > open, 5), "Share of up bars", color=color.green)//@itmscript 2
study("Share", overlay=false)
plot(ta.sma(close > open ? 1 : 0, 5), "Share of up bars", color=color.green)