Stop aggregation until first available data point


New member
Hi, I am pretty new to Thinkscript so this might be a dumb question. I have recently written a simple script to track the IV percentile of a ticker, it basically counts how many days in the past 252 days have higher IV than today. The problem is with tickers that have data for less than 252 days, for some reason even if I terminate the aggregation loop when I detect a NaN, the script just outputs nothing altogether. It is hard to troubleshoot Thinkscript so I would like to see if there is any insight provided here. Thanks in advance!

declare lower;
input overboughtlvl = 80.0;
input oversoldlvl = 20.0;

def spotVol = IMP_VOLATILITY();
def greater = 0;
def percentile = fold i = 1 to 252 with volAbove = 0.0 while !IsNaN(IMP_volatility()[i]) do if spotVol > IMP_volatility()[i] then volAbove + 1.0 else volAbove;

plot graph = percentile / 252.0;
plot h = overboughtlvl / 100.0;
plot l = oversoldlvl / 100.0;

graph.DefineColor("OverBought", Color.RED);
graph.DefineColor("Normal", Color.GRAY);
graph.DefineColor("OverSold", Color.CYAN);
graph.AssignValueColor(if graph * 100.0 > overboughtlvl then graph.color("OverBought") else if graph * 100.0 < oversoldlvl then graph.color("OverSold") else graph.color("Normal"));

Similar threads