• Get $40 off VIP by signing up for a free account! Sign Up

Multiple Daily SMA on Intraday Charts with Labels

D-Man

New member
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);
 

Attachments

  • DailySMAs.png
    DailySMAs.png
    68.3 KB · Views: 156
Solution
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...
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);

The daily sma is dependent on the aggregationperiod = DAY,, which is only available during regular trading hours.

One possible way to assist you would be extending the prior day's dailysma until 0930, when the current day's regular trading data becomes available.

Here is the modification to extend the prior dailysma:
The first image is at 0929 with the prior day's sma extended. The second image is the new day's dailysma.
Screenshot 2024-01-24 151105.png
Screenshot 2024-01-24 151223.png
Code:
input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input length = 9;
input displace = 0;
input showOnlyLastPeriod = no;

def DSMA =
if IsNaN(close(period=aggregationPeriod.DAY))
then DSMA[1]
else if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1])
then Double.NaN
else if !showOnlyLastPeriod
then
Average(Fundamental(price, period = aggregationPeriod)[-displace], length)
else dsma[1]
;

plot dailysma = DSMA;
dailysma.SetDefaultColor(GetColor(1));
dailysma.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
291 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top