Resource icon

thinkScript double.NaN function in ThinkorSwim

NaN means Not a Number. The double.NaN function in thinkScript code can be used to prevent a line, signal, or bubble from being displayed. In other words, if a plot's condition is no longer true, the plot stops displaying on your chart.

Usage

Code:
plot Data = if condition then value else double.nan;

Example

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2017-2020
#

input price = close;
input length = 200;
input displace = 0;

plot SMA = Average(price[-displace], length);
plot UpSignal = if price > SMA then close else double.nan;

SMA.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

This code plot an up arrow at every candle when the closing price is above the 200 simple moving average. If the closing price is below the moving average, then nothing will be displayed.

Additional scripts related to double.NaN

Useful Notes from the thinkScript Lounge

You can use Double.Nan to keep a plot from appearing. There may be times when you may want to keep a plot line from displaying on the chart...Here is an example of using double.nan This code finds a high spot in the price based on a PSAR, and plots the high as a straight line. The code assumes that the new high is a point of resistance. If the price closes over the Resistance line, the code stops displaying the line. [source]

Code:

Code:
def ClsUp = close > open;
def HiLw2 = hl2;

def PSAR_Line = parabolicSAR(0.02,0.2);

def hl2XDwn = HiLw2[1] >= PSAR_Line[1] && HiLw2 < PSAR_Line;

Rec Rzstnce = if IsNan(Rzstnce[1]) or (close[1] < Rzstnce[1] and close > Rzstnce[1]) then 0 else if hl2XDwn then PSAR_Line else Rzstnce[1];

plot TrndLine = PSAR_Line;

Plot Resistance = if Rzstnce > 0 then Rzstnce else double.nan;
Resistance.SetLineWeight(3);

Plot ResistanceBroken = Rzstnce[1] > 0 and Rzstnce == 0;
ResistanceBroken.SetPaintingStrategy(PaintingStrategy.boolean_arrow_Up);
ResistanceBroken.SetLineWeight(3);
ResistanceBroken.SetDefaultColor(color.green);
  • Like
Reactions: ivctrader
Author
BenTen
Views
5,436
First release
Last update
Rating
5.00 star(s) 2 ratings

More resources from BenTen

Latest reviews

Appreciated the simple explanation, along with the examples. I was having trouble getting the double.nan into my brain....good job!
Simply explained

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Top