VVIX SMA "NaN" Issue

irontrades

New member
I created a strategy that had been working fine all last year. This year, it suddenly stopped working. I noticed that on 1/10/25, the VVIX SMA calculation begins to return "NaN". I believe this is the root of my problem. I've done alot of digging on this and have not found a solution yet.

Code:
# IVTS and VVIX Strategy for VIX Options (Enter/Exit on Open)
input ivtsSymbol1 = "VIX";
input ivtsSymbol2 = "VIX3M";
input ivtsLength = 5;
input vvixSMALength = 200;
def ivtsRatio = close(ivtsSymbol1) / close(ivtsSymbol2);
def ivtsMedian = Median(ivtsRatio, ivtsLength);

# Request daily aggregation for VVIX so it updates as expected
def vvix = close("VVIX", period = AggregationPeriod.DAY);
def vvixSMA = SimpleMovingAvg(vvix, vvixSMALength);

# Entry and exit conditions based on previous day's data
def entryCondition = ivtsMedian[1] < 1 and vvix[1] < vvixSMA[1];
def exitCondition = ivtsMedian[1] > 1 or vvix[1] > vvixSMA[1];

# Trading logic
rec inTrade = CompoundValue(1, if entryCondition and !inTrade[1] then 1 else if exitCondition and inTrade[1] then 0 else inTrade[1], 0);

# Generate signals for next open
def enterSignal = entryCondition and !inTrade[1];
def exitSignal = inTrade[1] and exitCondition;

# Plot signals
plot EnterPlot = enterSignal;
plot ExitPlot = exitSignal;
plot InTradePlot = inTrade;
EnterPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
EnterPlot.SetLineWeight(3);
EnterPlot.SetDefaultColor(Color.GREEN);
ExitPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
ExitPlot.SetLineWeight(3);
ExitPlot.SetDefaultColor(Color.RED);
InTradePlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
InTradePlot.SetLineWeight(3);
InTradePlot.SetDefaultColor(Color.CYAN);

# Labels
AddLabel(yes, "IVTS Median: " + AsText(ivtsMedian[1], NumberFormat.TWO_DECIMAL_PLACES), Color.WHITE);
AddLabel(yes, "VVIX: " + AsText(vvix[1], NumberFormat.TWO_DECIMAL_PLACES), Color.WHITE);
AddLabel(yes, "VVIX SMA: " + AsText(vvixSMA[1], NumberFormat.TWO_DECIMAL_PLACES), Color.WHITE);
AddLabel(yes, "Enter Next Open: " + if enterSignal then "Yes" else "No", Color.GREEN);
AddLabel(yes, "Exit Next Open: " + if exitSignal then "Yes" else "No", Color.RED);
AddLabel(yes, "In Trade: " + (if inTrade then "Yes" else "No"), Color.YELLOW);

# Alerts for entry and exit signals
Alert(enterSignal, "IVTS-VVIX Entry Signal for Next Open", Alert.BAR, Sound.Ding);
Alert(exitSignal, "IVTS-VVIX Exit Signal for Next Open", Alert.BAR, Sound.Chimes);
 
I’d guard VIX and VVIX against NaN values. Try something like this for both.

Python:
def na = Double.NaN;
def c = close("VIX");
def VIX = if !isNaN(c) then c else
          if isNan(c) then c[1] else
            na;
 

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