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.

Capture.jpg
Ruby:
def xemaD = if IsNaN(close)
            then xemaD[1]
            else ExpAverage(close(period = AggregationPeriod.DAY), 21);
plot emaD = xemaD;
emaD.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
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.

Capture.jpg
Ruby:
def xemaD = if IsNaN(close)
            then xemaD[1]
            else ExpAverage(close(period = AggregationPeriod.DAY), 21);
plot emaD = xemaD;
emaD.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
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;
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               emaD[bm], "D", emaD.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               ema4[bm], "4", ema4.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               ema2[bm], "2", ema2.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               ema1[bm], "1", ema1.TakeValueColor());
 
Solution

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

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

Capture.jpg
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
            else HighestAll(xemaD);
emaD.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
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;
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               emaD[bm], "D", emaD.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               ema4[bm], "4", ema4.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               ema2[bm], "2", ema2.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               ema1[bm], "1", ema1.TakeValueColor());
 
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.

Capture.jpg
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
            else HighestAll(xemaD);
emaD.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
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;
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               emaD[bm], "D", emaD.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(close[bm]) and !IsNaN(close[bm1]),
               ema4[bm], "4", ema4.TakeValueColor());
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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