I'm not sure why, is there a simple reason and fix that I'm missing? (The study is deliberately written to only plot in the expansion area, to clarify.)
Code:
input price = FundamentalType.CLOSE;
input aggregationPeriodhour = AggregationPeriod.HOUR;
input aggregationPeriodFourhour = aggregationPeriod.FOUR_HOURS;
input lengthtwenty = 20;
input lengthfifty = 50;
input lengthtwohundred = 200;
input offset = 1;
input length2 = 25;
input displace=0;
def SMAtwenty_hour_pre = Average(fundamental(price, period = aggregationPeriodhour)[-displace], lengthtwenty) ;
def SMAtwenty_hour_pre_line = SimpleMovingAvg(SMAtwenty_hour_pre, 1, length2);
rec SMAtwenty_hour_pre_line_2 = if IsNaN(SMAtwenty_hour_pre_line) then SMAtwenty_hour_pre_line_2[1] else SMAtwenty_hour_pre_line[offset];
plot SMAtwenty_hour= if !isNan(close) then double.nan else if isnan(SMAtwenty_hour_pre_line) then SMAtwenty_hour_pre_line_2 else double.nan;
SMAtwenty_hour.SetDefaultColor(CreateColor(143,239,191));
SMAtwenty_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwenty_hour.SetLineWeight(1);
def SMAfifty_hour_pre = Average(fundamental(price, period = aggregationPeriodhour)[-displace], lengthfifty);
def SMAfifty_hour_pre_line = SimpleMovingAvg(SMAfifty_hour_pre, 1, length2);
rec SMAfifty_hour_pre_line_2 = if IsNaN(SMAfifty_hour_pre_line) then SMAfifty_hour_pre_line_2[1] else SMAfifty_hour_pre_line[offset];
plot SMAfifty_hour= if !isNan(close) then double.nan else if isnan(SMAfifty_hour_pre_line) then SMAfifty_hour_pre_line_2 else double.nan;
SMAfifty_hour.SetDefaultColor(CreateColor(0,0,255));
SMAfifty_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAfifty_hour.SetLineWeight(1);
def SMAtwohundred_hour_pre = Average(fundamental(price, period = aggregationPeriodhour)[-displace], lengthtwohundred);
def SMAtwohundred_hour_pre_line = SimpleMovingAvg(SMAtwohundred_hour_pre, 1, length2);
rec SMAtwohundred_hour_pre_line_2 = if IsNaN(SMAtwohundred_hour_pre_line) then SMAtwohundred_hour_pre_line_2[1] else SMAtwohundred_hour_pre_line[offset];
plot SMAtwohundred_hour= if !isNan(close) then double.nan else if isnan(SMAtwohundred_hour_pre_line) then SMAtwohundred_hour_pre_line_2 else double.nan;
SMAtwohundred_hour.SetDefaultColor(CreateColor(255,102,255));
SMAtwohundred_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwohundred_hour.SetLineWeight(1);
def SMAtwenty_fourhour_pre =Average(fundamental(price, period = aggregationPeriodfourhour)[-displace], lengthtwenty) ;
def SMAtwenty_fourhour_pre_line = SimpleMovingAvg(SMAtwenty_fourhour_pre, 1, length2);
rec SMAtwenty_fourhour_pre_line_2 = if IsNaN(SMAtwenty_fourhour_pre_line) then SMAtwenty_fourhour_pre_line_2[1] else SMAtwenty_fourhour_pre_line[offset];
plot SMAtwenty_fourhour= if !isNan(close) then double.nan else if isnan(SMAtwenty_fourhour_pre_line) then SMAtwenty_fourhour_pre_line_2 else double.nan;
SMAtwenty_fourhour.SetDefaultColor(CreateColor(143,239,191));
SMAtwenty_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwenty_fourhour.SetLineWeight(2);
def SMAfifty_fourhour_pre =Average(fundamental(price, period = aggregationPeriodfourhour)[-displace], lengthfifty) ;
def SMAfifty_fourhour_pre_line = SimpleMovingAvg(SMAfifty_fourhour_pre, 1, length2);
rec SMAfifty_fourhour_pre_line_2 = if IsNaN(SMAfifty_fourhour_pre_line) then SMAfifty_fourhour_pre_line_2[1] else SMAfifty_fourhour_pre_line[offset];
plot SMAfifty_fourhour= if !isNan(close) then double.nan else if isnan(SMAfifty_fourhour_pre_line) then SMAfifty_fourhour_pre_line_2 else double.nan;
SMAfifty_fourhour.SetDefaultColor(CreateColor(0,0,255));
SMAfifty_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAfifty_fourhour.SetLineWeight(2);
def SMAtwohundred_fourhour_pre = Average(fundamental(price, period = aggregationPeriodfourhour)[-displace], lengthtwohundred) ;
def SMAtwohundred_fourhour_pre_line = SimpleMovingAvg(SMAtwohundred_fourhour_pre, 1, length2);
rec SMAtwohundred_fourhour_pre_line_2 = if IsNaN(SMAtwohundred_fourhour_pre_line) then SMAtwohundred_fourhour_pre_line_2[1] else SMAtwohundred_fourhour_pre_line[offset];
plot SMAtwohundred_fourhour= if !isNan(close) then double.nan else if isnan(SMAtwohundred_fourhour_pre_line) then SMAtwohundred_fourhour_pre_line_2 else double.nan;
SMAtwohundred_fourhour.SetDefaultColor(CreateColor(255,102,255));
SMAtwohundred_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwohundred_fourhour.SetLineWeight(2);