# horizontal line for the 21 ema for daily, 1H, 2H and 4H that updates automatically in the chart

#### Elnino

##### New member
Looking to add a horizontal line for the 21 ema for daily, 1H, 2H and 4H that updates automatically in the chart. Can anyone help me? I’m using the thinkorswim platform and need a script to be able to add those to a single chart.

Solution
Looking to add a horizontal line for the 21 ema for daily, 1H, 2H and 4H that updates automatically in the chart. Can anyone help me? I’m using the thinkorswim platform and need a script to be able to add those to a single chart.

This has bubble option to assist display.

Ruby:
``````def xemaD = if IsNaN(close)
else ExpAverage(close(period = AggregationPeriod.DAY), 21);
def xema4 = if IsNaN(close)
then xema4[1]
else ExpAverage(close(period = AggregationPeriod.FOUR_HOURS), 21);
plot ema4 = xema4;
ema4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def xema2 = if IsNaN(close)...``````
Looking to add a horizontal line for the 21 ema for daily, 1H, 2H and 4H that updates automatically in the chart. Can anyone help me? I’m using the thinkorswim platform and need a script to be able to add those to a single chart.

This has bubble option to assist display.

Ruby:
``````def xemaD = if IsNaN(close)
else ExpAverage(close(period = AggregationPeriod.DAY), 21);
def xema4 = if IsNaN(close)
then xema4[1]
else ExpAverage(close(period = AggregationPeriod.FOUR_HOURS), 21);
plot ema4 = xema4;
ema4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def xema2 = if IsNaN(close)
then xema2[1]
else ExpAverage(close(period = AggregationPeriod.TWO_HOURS), 21);
plot ema2 = xema2;
ema2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def xema1 = if IsNaN(close)
then xema1[1]
else ExpAverage(close(period = AggregationPeriod.HOUR), 21);
plot ema1 = xema1;
ema1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

input showbubbles = yes;
input bubblemover = 3;
def   bm = bubblemover;
def  bm1 = bm + 1;
ema4[bm], "4", ema4.TakeValueColor());
ema2[bm], "2", ema2.TakeValueColor());
ema1[bm], "1", ema1.TakeValueColor());``````

This has bubble option to assist display.
Thank you so much. Is there a way that it can be only a straight horizontal line?

Thank you so much. Is there a way that it can be only a straight horizontal line?

Sure, with an option to have the lines only show on today or whole chart

Ruby:
``````input showtodayonly = yes;
def xemaD = if !IsNaN(close) and IsNaN(close[-1])
then ExpAverage(close(period = AggregationPeriod.DAY), 21)
else Double.NaN;
plot emaD = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
def xema4 = if !IsNaN(close) and IsNaN(close[-1])
then ExpAverage(close(period = AggregationPeriod.FOUR_HOURS), 21)
else Double.NaN;
plot ema4 = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else HighestAll(xema4);
ema4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def xema2 = if !IsNaN(close) and IsNaN(close[-1])
then ExpAverage(close(period = AggregationPeriod.TWO_HOURS), 21)
else Double.NaN;
plot ema2 = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else HighestAll(xema2);
ema2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def xema1 = if !IsNaN(close) and IsNaN(close[-1])
then ExpAverage(close(period = AggregationPeriod.HOUR), 21)
else Double.NaN;
plot ema1 = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else HighestAll(xema1);
ema1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

input showbubbles = yes;
input bubblemover = 3;
def   bm = bubblemover;
def  bm1 = bm + 1;
ema4[bm], "4", ema4.TakeValueColor());
ema2[bm], "2", ema2.TakeValueColor());
ema1[bm], "1", ema1.TakeValueColor());``````

This has bubble option to assist display.
I have an issue. It's currently not showing on the 4HR or the Daily Chart. It only shows on the 1HR and 1min-45min

I have an issue. It's currently not showing on the 4HR or the Daily Chart. It only shows on the 1HR and 1min-45min

Since you have an 1hour timeframe in your script then it will not display on charts higher than 1hour. This applies to any script that contains higher aggregations.

You can make a new script removing the timeframes below 4hrs to display this on a 4hr chart both the 4hr and Daily timeframes . See below.

Ruby:
``````input showtodayonly = yes;
def xemaD = if !IsNaN(close) and IsNaN(close[-1])
then ExpAverage(close(period = AggregationPeriod.DAY), 21)
else Double.NaN;
plot emaD = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
def xema4 = if !IsNaN(close) and IsNaN(close[-1])
then ExpAverage(close(period = AggregationPeriod.FOUR_HOURS), 21)
else Double.NaN;
plot ema4 = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else HighestAll(xema4);
ema4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

input showbubbles = yes;
input bubblemover = 3;
def   bm = bubblemover;
def  bm1 = bm + 1;
ema4[bm], "4", ema4.TakeValueColor());``````

Last edited:

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