You might have heard of the term NaN. It is short for “not a number,” and it means that a value is undefined.
In thinkScript, the Double.NaN syntax “returns the value that indicates that the result of an operation is not a number.”
When we have a NaN value in our result, thinkScript does not display the output.
The Double.NaN syntax is valid when working with a defined condition. It can help remove/hide a plot, line, chart bubble, and other data from appearing on your chart if the value of the condition is unpresentable.
How to Use Double.NaN in thinkScript
In the following example script, your chart will have a blue point drawn on top of each green candle. If the candle is red, the script skips over and stops displaying until it reaches another green candle.
def green = close > open; plot data = if green then highest(high,1) else double.nan; data.SetPaintingStrategy(PaintingStrategy.POINTS);
Here is yet another use case for Double.NaN in thinkScript.
declare lower; input length = 50; plot VolAvg = Average(volume, length); plot Vol = if volume > VolAvg then volume else double.nan; Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
The script above plots volume along with a 50-period Simple Moving Average that is based on volume.
The Double.NaN syntax is utilized in this example to hide volume bars that are below the simple moving average line.
- (1) The default VolumeAvg indicator. It shows all the data.
- (2) The script above. It only displays volume bars greater than the moving average line.
In plain English, Double.NaN is the equivalent of if something is undefined or not true, then don’t display it on the chart.