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

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

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.

Capture.jpg

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!

@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...
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
201 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