armybender
Active member
Hello,
Can someone help eliminate the NaN values in these defs / plots?
More detail:
I adapted an indicator from THIS PAGE. What I'm finding is that the upperChannelData and lowerChannelData defs have NaN values. I'm trying to build something on top of this, and the NaN values make it impossible to do.
EnableApproximation is turned on for the plots, so you don't see the gaps, but if you disable that line of code the gaps become very clear.
Here's the indicator code.
Thanks!
Can someone help eliminate the NaN values in these defs / plots?
More detail:
I adapted an indicator from THIS PAGE. What I'm finding is that the upperChannelData and lowerChannelData defs have NaN values. I'm trying to build something on top of this, and the NaN values make it impossible to do.
EnableApproximation is turned on for the plots, so you don't see the gaps, but if you disable that line of code the gaps become very clear.
Here's the indicator code.
Ruby:
# Adapted from https://usethinkscript.com/threads/supertrend-channels-luxalgo-for-thinkorswim.15377/
#DECLARATIONS
declare upper;
#USER INPUTS
input showSuperTrendChannel = yes;
input show2XChannel = no;
input showCloud = no;
input atrLength = 14;
input atrMultiple = 3.0;
input smoothLength = 3;
input price = close;
input showDotsOnBreakout = no;
input dotSeparationTicks = 2;
input alertOnTouch = no;
#GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Blue" , CreateColor(33, 150, 243));
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));
#DEFINITIONS AND CALCULATIONS
## These variables must be declared early as they are used in preceeding calculations
def upperBand;
def lowerBand;
def max;
def min;
## Standard variable calculations
def nATR = MovingAverage(AverageType.WILDERS, TrueRange(high, close, low), atrLength) * atrMultiple; #ATR
def up = hl2 + nATR;
def dn = hl2 - nATR;
def up1 = if (IsNaN(upperBand[1]) or upperBand[1] == 0) then up else upperBand[1];
def dn1 = if (IsNaN(lowerBand[1]) or lowerBand[1] == 0) then dn else lowerBand[1];
upperBand = if price[1] < up1 then Min(up, up1) else up;
lowerBand = if price[1] > dn1 then Max(dn, dn1) else dn;
def os = if price > upperBand then yes else if price < lowerBand then no else os[1];
def spt = if os == 1 then lowerBand else upperBand;
def cross = (price > spt and price[1] <= spt[1]) or (price < spt and price[1] >= spt[1]);
def max1 = if (IsNaN(max[1]) or max[1] == 0) then price else max[1];
def min1 = if (IsNaN(min[1]) or min[1] == 0) then price else min[1];
max = if cross then Max(max1, price) else if os == 1 then Max(price, max1) else Min(spt, max1);
min = if cross then Min(min1, price) else if os == 0 then Min(price, min1) else Max(spt, min1);
def avg = (max + min) / 2;
def color = if avg > avg[1] and price > avg then 1 else if avg < avg[1] and price < avg then -1 else 0;
def upperChannelData = ExpAverage(if max != max[1] and os or !showSuperTrendChannel then Double.NaN else max, smoothLength);
def avgChannelData = ExpAverage(avg, smoothLength);
def lowerChannelData = ExpAverage(if min != min[1] and os or !showSuperTrendChannel then Double.NaN else min, smoothLength);
def upper2XChannelData = avgChannelData + (2 * (upperChannelData - avgChannelData));
def lower2XChannelData = avgChannelData - (2 * (avgChannelData - lowerChannelData));
def longDotData = if high >= upperChannelData then high + (dotSeparationTicks * TickSize()) else Double.NaN;
def shortDotData = if low <= lowerChannelData then low - (dotSeparationTicks * TickSize()) else Double.NaN;
#PLOTS
plot UpperChannel = upperChannelData;
plot AvgChannel = avgChannelData;
plot LowerChannel = lowerChannelData;
plot Upper2XChannel = upper2XChannelData;
plot Lower2XChannel = lower2XChannelData;
plot LongDot = longDotData;
plot shortDot = shortDotData;
#FORMATTING
UpperChannel.AssignValueColor(GlobalColor("Green"));
UpperChannel.EnableApproximation();
UpperChannel.HideBubble();
UpperChannel.HideTitle();
LowerChannel.AssignValueColor(GlobalColor("Red"));
LowerChannel.EnableApproximation();
LowerChannel.HideBubble();
LowerChannel.HideTitle();
Upper2XChannel.AssignValueColor(GlobalColor("Green"));
Upper2XChannel.EnableApproximation();
Upper2XChannel.HideBubble();
Upper2XChannel.HideTitle();
Upper2XChannel.SetHiding(!show2XChannel);
Lower2XChannel.AssignValueColor(GlobalColor("Red"));
Lower2XChannel.EnableApproximation();
Lower2XChannel.HideBubble();
Lower2XChannel.HideTitle();
Lower2XChannel.SetHiding(!show2XChannel);
AvgChannel.HideBubble();
AvgChannel.HideTitle();
AvgChannel.EnableApproximation();
AvgChannel.AssignValueColor(
if color > 0 then GlobalColor("Green")
else if color < 0 then GlobalColor("Red")
else Color.DARK_GRAY
);
LongDot.AssignValueColor(GlobalColor("Green"));
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.SetHiding(!showDotsOnBreakout);
LongDot.HideBubble();
LongDot.HideTitle();
shortDot.AssignValueColor(GlobalColor("Red"));
shortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
shortDot.SetHiding(!showDotsOnBreakout);
shortDot.HideBubble();
shortDot.HideTitle();
#CLOUDS
AddCloud(if showCloud then UpperChannel else Double.NaN, AvgChannel, GlobalColor("Green"));
AddCloud(if showCloud then AvgChannel else Double.NaN, LowerChannel, GlobalColor("Red"));
Thanks!
Last edited: