# How to show SMA lines for yesterday and today only?

#### c0der

##### Member
I primarily use price action and hence do not want to clutter my chart. Is it possible to script it such that the SMA lines are only shown for yesterday and today for a 1Y1D chart?

I primarily use price action and hence do not want to clutter my chart. Is it possible to script it such that the SMA lines are only shown for yesterday and today for a 1Y1D chart?

Try this

Code:
``````input bars  = 2;
def lastbar = highestall(if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan);
plot ema9   = if barnumber()<=lastbar-bars then double.nan else expaverage(close,9);``````

I want to add EMA21, SMA50, SMA200 and be able to customize the line color/thickness.. tried this but it doesn't work?

Code:
``````input bars  = 2;
def lastbar = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
plot ema21   = if BarNumber() <= lastbar - bars then Double.NaN else ExpAverage(close, 21);
plot sma50   = if BarNumber() <= lastbar - bars then Double.NaN else simpleaverage(close, 50);``````

I want to add EMA21, SMA50, SMA200 and be able to customize the line color/thickness.. tried this but it doesn't work?

Code:
``````input bars  = 2;
def lastbar = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
plot ema21   = if BarNumber() <= lastbar - bars then Double.NaN else ExpAverage(close, 21);
plot sma50   = if BarNumber() <= lastbar - bars then Double.NaN else simpleaverage(close, 50);``````

The code I gave you worked. You just misspelled simplemovingavg in your sma50 code. The 'red highlight' in your coding is TOS's way of letting you know if there is a syntax error.

Here is the revised code using the moving averages you indicated.

Code:
``````input bars       = 2;
input lineweight = 5;
def lastbar = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);

plot ema21   = if BarNumber() <= lastbar - bars then Double.NaN else ExpAverage(close, 21);
plot sma50   = if BarNumber() <= lastbar - bars then Double.NaN else simplemovingAvg(close, 50);
plot sma200  = if BarNumber() <= lastbar - bars then Double.NaN else simplemovingAvg(close, 200);

ema21.setlineweight(lineweight);
sma50.setlineweight(lineweight);
sma200.setlineweight(lineweight);
ema21.setdefaultColor(color.green);
sma50.setdefaultColor(color.yellow);
sma200.setdefaultColor(color.red);``````

works like a charm now. thank you so much!

works like a charm now. thank you so much!

@c0der For future reference, just as there is an EMA function, ExpAverage(price, length), there is also a function for SMA, Average(price, length)... When only the results are needed, such as within scans, it is more efficient to call the functions rather than Studies... You were halfway there in the code above with ExpAverage()... Happy trading...

87k+ Posts
201 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?