Double.nan question

hockeycoachdoug

Active member
2019 Donor
VIP
I'm trying to create a scan based on an indicator I'm already using. The indicator creates a buy alert when the mark crosses an entry line it generates. This entry line stays active until the mark reaches the stop exit line also generated by the indicator.

I'd like to be able to scan watchlists for stocks that have triggered the alert in the last 15 minute bar, but the scan should not return results if the entry alert was generated in an earlier bar. For example, if the entry line was crossed 4 bars ago and is still active because the position hasn't hit the exit line, the scan shouldn't alert it as a new trigger.

Basically I just want it to alert me with the ticker whenever the entry line is crossed in the most recent bar, but ignore it if the alert already occurred.

I think it has something to do with the double.nan function, but I'm not sure how to implement it.
 
Solution
I'm trying to create a scan based on an indicator I'm already using. The indicator creates a buy alert when the mark crosses an entry line it generates. This entry line stays active until the mark reaches the stop exit line also generated by the indicator.

I'd like to be able to scan watchlists for stocks that have triggered the alert in the last 15 minute bar, but the scan should not return results if the entry alert was generated in an earlier bar. For example, if the entry line was crossed 4 bars ago and is still active because the position hasn't hit the exit line, the scan shouldn't alert it as a new trigger.

Basically I just want it to alert me with the ticker whenever the entry line is crossed in the most recent bar, but...
I'm trying to create a scan based on an indicator I'm already using. The indicator creates a buy alert when the mark crosses an entry line it generates. This entry line stays active until the mark reaches the stop exit line also generated by the indicator.

I'd like to be able to scan watchlists for stocks that have triggered the alert in the last 15 minute bar, but the scan should not return results if the entry alert was generated in an earlier bar. For example, if the entry line was crossed 4 bars ago and is still active because the position hasn't hit the exit line, the scan shouldn't alert it as a new trigger.

Basically I just want it to alert me with the ticker whenever the entry line is crossed in the most recent bar, but ignore it if the alert already occurred.

I think it has something to do with the double.nan function, but I'm not sure how to implement it.
you can do something like:
Code:
def sell = if condition[1] and condition == double.nan then 1 else double.nan;
so that you reference the previous bar state (condition[1]) as True and the current bar as a NaN.

May get you where you want to be.

happy trading,
mashume
 
Solution
you can do something like:
Code:
def sell = if condition[1] and condition == double.nan then 1 else double.nan;
so that you reference the previous bar state (condition[1]) as True and the current bar as a NaN.

May get you where you want to be.

happy trading,
mashume
Hi... was looking something else up and came across this so wanted to give you a heads up on a correction.
This statement will always evaluate to false or NaN
Code:
def sell = if condition[1] and condition == double.nan then 1 else double.nan;

The proper test for NaN in a conditional statement is to use the function isNan()
so you would write it like this:
Code:
def sell = if condition[1] and isNaN(condition)==1 then 1 else double.nan;

reference:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/IsNaN
 
Last edited:
Hi... was looking something else up and came across this so wanted to give you a heads up on a correction.
This statement will always evaluate to false or NaN
Code:
def sell = if condition[1] and condition == double.nan then 1 else double.nan;

The proper test for NaN in a conditional statement is to use the function isNan()
so you would write it like this:
Code:
def sell = if condition[1] and isNaN(condition)==1 then 1 else double.nan;

reference:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/IsNaN
Quite right and correct. I bounce between ThinkScript, Python, and any combination of PLSQL, Julia, Haskel and R on any given day. Syntax sometimes gets away from me. But I should have known something looked funny with that one. Good catch!

-mashume
 
I have a related question. I want to assign a value of linear regression = 0 if NaN. How do i do that?
Here is the code I'd like help with modifying (i am using this code in a custom column):
def regr = Inertia(price, 100);
plot z = if (price > regr) then 1 else if (price < regr) then -1 else 0;

I n this case, if any security is less than 100 days old regr returns NaN, so the comparison of price with regr returns NaN in the custom column.
 
I have a related question. I want to assign a value of linear regression = 0 if NaN. How do i do that?
Here is the code I'd like help with modifying (i am using this code in a custom column):
def regr = Inertia(price, 100);
plot z = if (price > regr) then 1 else if (price < regr) then -1 else 0;

I n this case, if any security is less than 100 days old regr returns NaN, so the comparison of price with regr returns NaN in the custom column.
If I follow what you want, it should look something like this:
Code:
def t_regr = Inertia(price, 100);
def regr = if isNaN(t_regr) then 0 else t_regr;

The isNaN() function does what you want in this case.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/IsNaN

-mashume
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
324 Online
Create Post

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.
Back
Top