1. Home
  2. Constants
  3. How to Use Double.NaN in thinkScript (With Examples)

How to Use Double.NaN in thinkScript (With Examples)

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;

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;

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.

Happy testing!

Unlock the Power of ThinkorSwim

Get the latest news and updates on TD Ameritrade, ThinkorSwim indicators, thinkScript tutorials, and trading strategies delivered straight to your inbox every week.

We don’t spam! Unsubscribe at anytime.

One thought on “How to Use Double.NaN in thinkScript (With Examples)

  1. Hi, i am looking a script to auto populate horizontal line point to exponential moving average closing price which i need to draw it manually daily. I found a script below only plot the EAM line but would like to top up the auto populate horizontal line, would appreciate you could help

    input price = FundamentalType.CLOSE;
    input aggregationPeriod = AggregationPeriod.DAY;
    input length = 20;
    input averageType = AverageType. EXPONENTIAL;

    plot MovAvg = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod), length);

Leave a Reply

Your email address will not be published. Required fields are marked *