help. Plot shows blank when cant compute value.. isnan not working for me

drive9

New member
I am trying to get an exception going where when the plot cant compute, to show a 0 or a 1 instead of blanking the whole indicator chart.
Here I'm attaching my code. Using ticker ACIU on todays , 08/31/2021 , it shows fine for length 5, 50, but blanks when using 5000.
Code:
declare lower;
def price = close;
def length1 = 5000; # 5, 50 work, 5000 blanks it.

def prelrs1 = ( WMA(price, length1) -  Average(price, length1) ) ;
plot LRS1 = if isnan(prelrs1) then 1 else prelrs1 ; # here Im trying to catch the exception to make it plot 1 when error.
plot zeroline = 0;

lNbnxgY.png
 
Solution
thanks for the input @nashume. Let me clarify, I dont plan on using a 5000 moving average, it was just an example to show that the variable of 5k breaks the plot. I want to totaly void this variable/plot when a null plot is generated, not to draw it in a different aggregation time.
if I have 3 or 10 moving averages going on , and one of them happens to be null, the entire indicator script will show blank along with the other 9 moving averages. I want the other 9 MA to still show even if 1 or two plots have a variable that rendered NA.
I see.

There isn't a way. By the nature of ThinkScript, when one plot breaks, they all break. ThinkScript is a functional language rather than procedural. What you're trying to do is more likely...
I doubt whether you'll have much luck getting a moving average of that length to work at all. (5000 minutes is 83 1/3 hours or nearly 3 1/2 days -- which is a LOT of 1 minute candles. If you had 5000 days on the chart, you'd be looking at nearly 20 years of data at 253 trading days per year.) You'd be better off, in my opinion at least, doing something like using a secondary aggregation to get a moving average over that length of time.
Code:
def agg = AggregationPeriod.HOUR; 
def data = close(period = agg);
input length = 83;
def prelrs = ( WMA(data, length) -  Average(data, length) ) ;
details of the periods available here:
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AggregationPeriod
This may, or may not give you what you want at all, but it will likely give you something.


Happy Trading,
Mashume
 
I doubt whether you'll have much luck getting a moving average of that length to work at all. (5000 minutes is 83 1/3 hours or nearly 3 1/2 days -- which is a LOT of 1 minute candles. If you had 5000 days on the chart, you'd be looking at nearly 20 years of data at 253 trading days per year.) You'd be better off, in my opinion at least, doing something like using a secondary aggregation to get a moving average over that length of time.
Code:
def agg = AggregationPeriod.HOUR;
def data = close(period = agg);
input length = 83;
def prelrs = ( WMA(data, length) -  Average(data, length) ) ;
details of the periods available here:
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AggregationPeriod
This may, or may not give you what you want at all, but it will likely give you something.


Happy Trading,
Mashume
thanks for the input @nashume. Let me clarify, I dont plan on using a 5000 moving average, it was just an example to show that the variable of 5k breaks the plot. I want to totaly void this variable/plot when a null plot is generated, not to draw it in a different aggregation time.
if I have 3 or 10 moving averages going on , and one of them happens to be null, the entire indicator script will show blank along with the other 9 moving averages. I want the other 9 MA to still show even if 1 or two plots have a variable that rendered NA.
 
thanks for the input @nashume. Let me clarify, I dont plan on using a 5000 moving average, it was just an example to show that the variable of 5k breaks the plot. I want to totaly void this variable/plot when a null plot is generated, not to draw it in a different aggregation time.
if I have 3 or 10 moving averages going on , and one of them happens to be null, the entire indicator script will show blank along with the other 9 moving averages. I want the other 9 MA to still show even if 1 or two plots have a variable that rendered NA.
I see.

There isn't a way. By the nature of ThinkScript, when one plot breaks, they all break. ThinkScript is a functional language rather than procedural. What you're trying to do is more likely to work in a procedural language. It's not that the value is NaN, or anything else... it just broke with your value. When that happens, it's kind of like a kernel panic for this one function and it just breaks. ToS doesn't have error trapping in the sense of a pythonic "Try: ... Except: ..." statement.

You can, of course, put multiple plots on a single chart by overlapping them, but that may not do what you need.

-mashume
 
Solution
I see.

There isn't a way. By the nature of ThinkScript, when one plot breaks, they all break. ThinkScript is a functional language rather than procedural. What you're trying to do is more likely to work in a procedural language. It's not that the value is NaN, or anything else... it just broke with your value. When that happens, it's kind of like a kernel panic for this one function and it just breaks. ToS doesn't have error trapping in the sense of a pythonic "Try: ... Except: ..." statement.

You can, of course, put multiple plots on a single chart by overlapping them, but that may not do what you need.

-mashume
This is the answer. Thank you so much Mashume. I will section my code so that section fails on its own.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
429 Online
Create Post

Similar threads

Similar threads

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