replace n/a result with a number

mluttph

Member
I'm using
DailySMA("length" = 3)[1]/DailySMA("length" = 3)
in a strategy for spy on a 5min chart. It works all week except from Friday at 8pm till Monday about 1am it results in "n/a" which crashes my script.
I have tried multiple things but not sure what I'm doing, for example
CompoundValue(1,if !IsNaN(DailySMA("length" = 3)[1]/DailySMA("length" = 3)) then DailySMA("length" = 3)[1]/DailySMA("length" = 3) else 2,1)
Which still gives me "n/a" but for some reason when my curser is off the chart gives me "2"
I just need to figure out how when it gives me a "n/a" to change it to a number, like "1".
Thanks for any help.
 
Solution
Yeah something else might work. I noticed when I plot the DailySMA there is no line for the problem days.
Thanks
See if this helps. The bubbles display the current day close, 2 days before, the Avg of those days, and the Avg[1]. For comparison, the last two numbers are the DailySMA and DailySMA[1] indicators. These compare favorably, even when there is a gap in the plots of the DailySMA indicators as the gaps do not appear to give rise to a missing DailySMA as the periods, at least on the SPY and /ES and a few stocks that I checked.

To extend the plot of the DailySMA through the missing periods, our usual methods to handle those periods with the prior bars does not appear to work. So I used the enableapproximation() to extend...
it seems that spy has candles on sunday, which are causing the errors with DailySMA() ?
maybe because there is no data on saturdays?

i used to think this was true,
if you want to trap an error, you have to check for it before the data is used.
but it doesn't work in this case.


test code that produces bubbles with value errors
Code:
def d_ma = DailySMA("length" = 3);
def d_map = d_ma[1];
def ratio = d_map/d_ma;

input test1a = no;
addchartbubble(test1a, low, d_ma + "\n" + d_map + "\n" + ratio, (if isnan(ratio) then color.yellow else color.gray), no);

input test1b = yes;
addchartbubble(test1b and isnan(ratio), low, d_ma + "\n" + d_map + "\n" + ratio, (if isnan(ratio) then color.yellow else color.gray), no);

# --------------------------------

# error checking, bubbles above candles
def d_ma2 = if isnan(DailySMA("length" = 3)) then 1 else DailySMA("length" = 3);
def d_map2 = if isnan(d_ma2[1]) then 1 else d_ma2[1];
def ratio2 = d_map2/d_ma2;

input test2b = yes;
addchartbubble(test2b and isnan(ratio2), high, d_ma2 + "\n" + d_map2 + "\n" + ratio2, (if isnan(ratio2) then color.violet else color.gray), yes);


SPY 30D 30min
errors on sundays
2 sets of formulas
s5fzRC6.jpg



------------------------------------

so the next step would be to identify when the error happens, then avoid that time period.
the errors seem to happen on sundays. so if we look for the day of week and do something different on sundays, the error should be avoided.

Code:
def d_ma;
def d_map;
if (GetDayofWeek(GetYYYYMMDD()) == 7) then {
  d_ma = 1;
  d_map = 1;
} else {
  d_ma = DailySMA("length" = 3);
  d_map = d_ma[1];
}

def ratio = d_map/d_ma;

input test1b = yes;
addchartbubble(test1b and (GetDayofWeek(GetYYYYMMDD()) == 7), low, d_ma + "\n" + d_map + "\n" + ratio, (if isnan(ratio) then color.yellow else color.gray), no);


SPY 30D 30min
errors trapped , changed to 1 on sundays
zsYikTv.jpg
 
Hi
It works great for sundays, It looks like I have a problem with holidays, May 31,2021 crashes. I'm hoping you have another miracle for me. If not thanks for the this great fix and I will have to make allowances for holidays and probably short days.
 
It doesn't appear to be every holiday. Good Friday was ok. So it looks like anytime there is no data for the DailySMA its going to fail.
 
Last edited:
Hi
It works great for sundays, It looks like I have a problem with holidays, May 31,2021 crashes. I'm hoping you have another miracle for me. If not thanks for the this great fix and I will have to make allowances for holidays and probably short days.
maybe the error happens when a day has a different quanty of bars?
maybe using a different type of average will work?
 
I tried this change and it seems to work.
from
if (GetDayofWeek(GetYYYYMMDD()) == 7) then {
to
if (CountTradingDays(GetYYYYMMDD(), GetYYYYMMDD()) == 0) then {

Thanks halcyonguy for getting me through this!!!
 
Yeah something else might work. I noticed when I plot the DailySMA there is no line for the problem days.
Thanks
See if this helps. The bubbles display the current day close, 2 days before, the Avg of those days, and the Avg[1]. For comparison, the last two numbers are the DailySMA and DailySMA[1] indicators. These compare favorably, even when there is a gap in the plots of the DailySMA indicators as the gaps do not appear to give rise to a missing DailySMA as the periods, at least on the SPY and /ES and a few stocks that I checked.

To extend the plot of the DailySMA through the missing periods, our usual methods to handle those periods with the prior bars does not appear to work. So I used the enableapproximation() to extend the DailySMA.

Capture.jpg
Code:
def y  = close(period = AggregationPeriod.DAY);
def y1 = close(period = AggregationPeriod.DAY)[1];
def y2 = close(period = AggregationPeriod.DAY)[2];
def sumy = y + y1 + y2;
def sumy1 = sumy[1];
def x  = DailySMA("length" = 3);
def x1 = DailySMA("length" = 3)[1];
plot c = y;
c.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

def closetime = GetTime() crosses above RegularTradingEnd(GetYYYYMMDD());
plot z = x;
z.SetPaintingStrategy(PaintingStrategy.LINE);
z.EnableApproximation();

AddChartBubble(closetime, y,
"1D " + AsText(y) +
"\n2D " + AsText(y1) +
"\n3D " + AsText(y2) +
"\n3DAvg   " + AsText(sumy / 3) +
"\n3DAvg1  " + AsText(sumy1 / 3) +
"\nDSMA    " + AsText(x) +
"\nDSMA1  " + AsText(x1));
 
Solution

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
339 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