I wrote and indicator that adds up to 5 Daily SMAs to Intraday Charts with labels that adjuster for the user input on the length of the SMA and the price action above or below the moving average. I am very happy with the indicator as it can give me a very quick assessment of the current price action. But when I am doing premarket analysis, the values for the daily moving averages are not calculating. Can someone help me get the previous day for the SMA in my code while we are in the premarket.
def price = FundamentalType.CLOSE;
def aggregationPeriod = AggregationPeriod.DAY;
input SMA5_length = 5;
input SMA9_length = 9;
input SMA20_length = 20; # New input for SMA20
input SMA50_length = 50; # New input for SMA50
input SMA200_length = 200; # New input for SMA200
def displace = 0;
input showOnlyLastPeriod = yes;
input showDailySMA_labels = yes;
def preMkt = SecondsFromTime(0930) > GetTime();
plot DailySMA5;
plot DailySMA9;
plot DailySMA20;
plot DailySMA50;
plot DailySMA200;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
DailySMA5 = Double.NaN;
DailySMA9 = Double.NaN;
DailySMA20 = Double.NaN;
DailySMA50 = Double.NaN;
DailySMA200 = Double.NaN;
} else {
DailySMA9 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA5_length);
DailySMA5 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA9_length);
DailySMA20 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA20_length);
DailySMA50 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA50_length);
DailySMA200 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA200_length);
}
DailySMA5.SetDefaultColor(GetColor(5));
DailySMA5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA9.SetDefaultColor(GetColor(1));
DailySMA9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA20.SetDefaultColor(GetColor(2)); # Setting color for SMA20
DailySMA20.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA50.SetDefaultColor(GetColor(3)); # Setting color for SMA50
DailySMA50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA200.SetDefaultColor(GetColor(4)); # Setting color for SMA200
DailySMA200.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def aboveSMA5 = close > DailySMA5;
def aboveSMA9 = close > DailySMA9;
def aboveSMA20 = close > DailySMA20;
def aboveSMA50 = close > DailySMA50;
def aboveSMA200 = close > DailySMA200;
AddLabel(showDailySMA_labels, "Daily SMAs", Color.GRAY);
AddLabel(showDailySMA_labels, "5 SMA: " + Round(DailySMA5, 2), if aboveSMA5 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "9 SMA: " + Round(DailySMA9, 2), if aboveSMA9 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "20 SMA: " + Round(DailySMA20, 2), if aboveSMA20 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "50 SMA: " + Round(DailySMA50, 2), if aboveSMA50 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "200 SMA: " + Round(DailySMA200, 2), if aboveSMA200 then Color.LIGHT_GREEN else Color.LIGHT_RED);
#addlabel(1, DailySMA50, color.gray);
def price = FundamentalType.CLOSE;
def aggregationPeriod = AggregationPeriod.DAY;
input SMA5_length = 5;
input SMA9_length = 9;
input SMA20_length = 20; # New input for SMA20
input SMA50_length = 50; # New input for SMA50
input SMA200_length = 200; # New input for SMA200
def displace = 0;
input showOnlyLastPeriod = yes;
input showDailySMA_labels = yes;
def preMkt = SecondsFromTime(0930) > GetTime();
plot DailySMA5;
plot DailySMA9;
plot DailySMA20;
plot DailySMA50;
plot DailySMA200;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
DailySMA5 = Double.NaN;
DailySMA9 = Double.NaN;
DailySMA20 = Double.NaN;
DailySMA50 = Double.NaN;
DailySMA200 = Double.NaN;
} else {
DailySMA9 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA5_length);
DailySMA5 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA9_length);
DailySMA20 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA20_length);
DailySMA50 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA50_length);
DailySMA200 = Average(fundamental(price, period = aggregationPeriod)[-displace], SMA200_length);
}
DailySMA5.SetDefaultColor(GetColor(5));
DailySMA5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA9.SetDefaultColor(GetColor(1));
DailySMA9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA20.SetDefaultColor(GetColor(2)); # Setting color for SMA20
DailySMA20.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA50.SetDefaultColor(GetColor(3)); # Setting color for SMA50
DailySMA50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailySMA200.SetDefaultColor(GetColor(4)); # Setting color for SMA200
DailySMA200.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def aboveSMA5 = close > DailySMA5;
def aboveSMA9 = close > DailySMA9;
def aboveSMA20 = close > DailySMA20;
def aboveSMA50 = close > DailySMA50;
def aboveSMA200 = close > DailySMA200;
AddLabel(showDailySMA_labels, "Daily SMAs", Color.GRAY);
AddLabel(showDailySMA_labels, "5 SMA: " + Round(DailySMA5, 2), if aboveSMA5 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "9 SMA: " + Round(DailySMA9, 2), if aboveSMA9 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "20 SMA: " + Round(DailySMA20, 2), if aboveSMA20 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "50 SMA: " + Round(DailySMA50, 2), if aboveSMA50 then Color.LIGHT_GREEN else Color.LIGHT_RED);
AddLabel(showDailySMA_labels, "200 SMA: " + Round(DailySMA200, 2), if aboveSMA200 then Color.LIGHT_GREEN else Color.LIGHT_RED);
#addlabel(1, DailySMA50, color.gray);