Issues with TOS Qustom Cuotes

D

d0wnby0ne

New member
The below custom quote is outputting "NaN" seemingly at random. For about 75% of the scanned stocks, I get the NaN output but otherwise, I get the intended output. Whenever I relaunch TOS, different stocks output "NaN" and some of the ones that were outputting before started working. This is very frustrating and I need this quote to work to execute my trading plan. Any input or help would be greatly appreciated.
Code:
script AlgorithmIndicatorNNFXplot {
#INPUTS
def CMO_length = 20;
def kijun_period = 26;
def CCI_longLength = 14;
def accountValue = 285;
def ATR_length = 8;
def ATRaverageType = AverageType.SIMPLE;
def vol_length = 30;
#KIJUN
def Kijun = (Highest(high, kijun_period) + Lowest(low, kijun_period)) / 2;
#CMO
def curClose = close;
def prevClose = close[1];
def inc = if curClose > prevClose then curClose - prevClose else 0;
def dec = if prevClose > curClose then prevClose - curClose else 0;
def sumInc = Sum(inc, CMO_length);
def sumDec = Sum(dec, CMO_length);
def CMO = if sumInc + sumDec == 0 then 0 else (sumInc - sumDec) / (sumInc + sumDec) * 100;
#CCI
def CCI = CCI(length = CCI_longLength);
def "CCI Hist" = CCI;
#ATR
def ATR = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATR_length);
#END OF DAY
def TimeToStopSignal = 1545;
def TradeTimeFilter = if SecondsFromTime(TimeToStopSignal) == 0 then 1 else 0;
#EARNINGS
def earnings = if GetEventOffset(Events.EARNINGS, 0) > -3 then 0 else 1;
#TNB
def fastLength = 7;
def slowLength = 15;
def trendLength = 4;
input noiseType = {default linear, squared};
def noiseLength = 250;
def correctionFactor = 2;
Assert(trendLength > 0, "'trend length' must be positive: " + trendLength);
Assert(correctionFactor > 0, "'correction factor' must be positive: " + correctionFactor);
def smf = 2 / (1 + trendLength);
def reversal = TrendPeriods(fastLength, slowLength);
def cpc = if IsNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else cpc[1] + close - close[1];
def trend = if IsNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else trend[1] * (1 - smf) + cpc * smf;
def noise;
def diff = AbsValue(cpc - trend);
switch (noiseType) {
case linear:
    noise = correctionFactor * Average(diff, noiseLength);
case squared:
    noise = correctionFactor * Sqrt(Average(diff * diff, noiseLength));
}
def denom = AbsValue(trend) + AbsValue(noise);
def TNB = if denom == 0 then 0 else 100 * AbsValue(trend) / denom;
#QQE
def RSI_Period = 8;
def Slow_Factor = 5;
def QQE = 4.236;
def Wilder_Period = RSI_Period * 2 - 1;
def vClose = close;
def rsi = RSI(price = vClose, length = RSI_Period).RSI;
def rsi_ma = MovingAverage(AverageType.EXPONENTIAL, rsi, Slow_Factor);
def atr_rsi = AbsValue(rsi_ma[1] - rsi_ma);
def atr_rsi_ma = MovingAverage(AverageType.EXPONENTIAL, atr_rsi, Wilder_Period);
def dar = MovingAverage(AverageType.EXPONENTIAL, atr_rsi_ma, Wilder_Period) * QQE;
def DeltaFastAtrRsi = dar;
def RSIndex = rsi_ma;
def newshortband =  RSIndex + DeltaFastAtrRsi;
def newlongband = RSIndex - DeltaFastAtrRsi;
def longband = if RSIndex[1] > longband[1] and RSIndex > longband[1]
               then Max(longband[1], newlongband)
               else newlongband;
def shortband = if RSIndex[1] < shortband[1] and  RSIndex < shortband[1]
                then Min(shortband[1], newshortband)
                else newshortband;
def trend1 = if Crosses(RSIndex, shortband[1])
            then 1
            else if Crosses(longband[1], RSIndex)
            then -1
            else if !IsNaN(trend1[1])
            then trend1[1]
            else 1;
def FastAtrRsiTL = if trend1 == 1
                   then longband
                   else shortband;
def pFastAtrRsiTL = FastAtrRsiTL;
def pRsiMa = rsi_ma;
#EHLERSHIGHPASS
def price = close;
def roofCutoffLength = 125;
Assert(roofCutoffLength > 0, "roofCutoffLength must be positive: " + roofCutoffLength);
def alpha1 = (Cos(Sqrt(2) * Double.Pi / roofCutoffLength) + Sin (Sqrt(2) * Double.Pi / roofCutoffLength) - 1) / Cos(Sqrt(2) * Double.Pi / roofCutoffLength);
def highpass = if IsNaN(price + price[1] + price[2]) then 0 else Sqr(1 - alpha1 / 2) * (price - 2 * price[1] + price[2]) + 2 * (1 - alpha1) * GetValue(highpass, 1) - Sqr(1 - alpha1) * GetValue(highpass, 2);
def HighpassFilter = if !IsNaN(price) then highpass else Double.NaN;
#TIME
def yClose = close(period = "day" )[1];
def yhi = high(period = "day" )[1];
def ylo = low(period = "day" )[1];
def dOpen =  open(period = "day" )[0];
#BUY AND SELL CONDITIONS
#LONG
def ENTRY_SIGNAL = if CMO crosses above 0 or CCI crosses above 0 or TNB crosses above 50 then 1 else 0;
def ENTRY_FILTER = if CMO > 0 and CCI > 0 and TNB > 50 then 1 else 0;
def BASELINE = if close > Kijun and close > dOpen and HighpassFilter > 0 && close > yClose && close > ylo then 1 else 0;
def FILTER = if ENTRY_FILTER == 1 && BASELINE == 1 then 1 else 0;
def buy = if ENTRY_SIGNAL == 1 && FILTER == 1 then 1 else 0;
plot buy_signal = buy;
}
plot x = if AlgorithmIndicatorNNFXplot()."buy_signal" is equal to 1 then 1 else 0;
AssignBackgroundColor(if x == 1 then color.green else color.pink);
x.AssignValueColor(if x == 1 then color.green else color.pink);
 

Similar threads

Top