Multi-Timeframe EMA Indicator RTH + EXT

raj999

New member
I tried to create a Multi-Timeframe EMA Indicator which calculates and plots Exponential Moving Averages (EMAs) for 5, 9, 12, 34, 50, and 200 periods across multiple timeframes (10-min, 30-min, 1-hour, 4-hour, daily) using data from regular trading hours (4 AM–8 PM). It displays the EMAs on the chart and adds bubbles at the last bar to show their values, making it easy to analyze trends across different timeframes. Can anyone optimized this code ;
Code:
#declare upper;
#Created By Razoo
# Identify the last bar
def lastBarNumber = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def isLastBar = BarNumber() == lastBarNumber;

# Define regular trading hours (4 AM to 8 PM)
def regularHours = SecondsFromTime(0400) >= 0 and SecondsTillTime(2000) > 0;

# Calculate EMAs at the last bar using only regular trading hours data
# 10-minute EMAs
def EMA5_10M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.TEN_MIN) else Double.NaN, 5)
else Double.NaN;
def EMA9_10M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.TEN_MIN) else Double.NaN, 9)
else Double.NaN;
def EMA12_10M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.TEN_MIN) else Double.NaN, 12)
else Double.NaN;
def EMA34_10M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.TEN_MIN) else Double.NaN, 34)
else Double.NaN;
def EMA50_10M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.TEN_MIN) else Double.NaN, 50)
else Double.NaN;
def EMA200_10M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.TEN_MIN) else Double.NaN, 200)
else Double.NaN;

# 30-minute EMAs
def EMA5_30M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.THIRTY_MIN) else Double.NaN, 5)
else Double.NaN;
def EMA9_30M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.THIRTY_MIN) else Double.NaN, 9)
else Double.NaN;
def EMA12_30M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.THIRTY_MIN) else Double.NaN, 12)
else Double.NaN;
def EMA34_30M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.THIRTY_MIN) else Double.NaN, 34)
else Double.NaN;
def EMA50_30M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.THIRTY_MIN) else Double.NaN, 50)
else Double.NaN;
def EMA200_30M_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.THIRTY_MIN) else Double.NaN, 200)
else Double.NaN;

# 1-hour EMAs
def EMA5_1H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.HOUR) else Double.NaN, 5)
else Double.NaN;
def EMA9_1H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.HOUR) else Double.NaN, 9)
else Double.NaN;
def EMA12_1H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.HOUR) else Double.NaN, 12)
else Double.NaN;
def EMA34_1H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.HOUR) else Double.NaN, 34)
else Double.NaN;
def EMA50_1H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.HOUR) else Double.NaN, 50)
else Double.NaN;
def EMA200_1H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.HOUR) else Double.NaN, 200)
else Double.NaN;

# 4-hour EMAs
def EMA5_4H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.FOUR_HOURS) else Double.NaN, 5)
else Double.NaN;
def EMA9_4H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.FOUR_HOURS) else Double.NaN, 9)
else Double.NaN;
def EMA12_4H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.FOUR_HOURS) else Double.NaN, 12)
else Double.NaN;
def EMA34_4H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.FOUR_HOURS) else Double.NaN, 34)
else Double.NaN;
def EMA50_4H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.FOUR_HOURS) else Double.NaN, 50)
else Double.NaN;
def EMA200_4H_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.FOUR_HOURS) else Double.NaN, 200)
else Double.NaN;

# Daily EMAs
def EMA5_Daily_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.DAY) else Double.NaN, 5)
else Double.NaN;
def EMA9_Daily_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.DAY) else Double.NaN, 9)
else Double.NaN;
def EMA12_Daily_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.DAY) else Double.NaN, 12)
else Double.NaN;
def EMA34_Daily_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.DAY) else Double.NaN, 34)
else Double.NaN;
def EMA50_Daily_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.DAY) else Double.NaN, 50)
else Double.NaN;
def EMA200_Daily_Last = if isLastBar then
ExpAverage(if regularHours then hl2(period = AggregationPeriod.DAY) else Double.NaN, 200)
else Double.NaN;

# Propagate the last bar's EMA value to all bars
# 10-minute EMAs
def EMA5_10M_Value = HighestAll(EMA5_10M_Last);
def EMA9_10M_Value = HighestAll(EMA9_10M_Last);
def EMA12_10M_Value = HighestAll(EMA12_10M_Last);
def EMA34_10M_Value = HighestAll(EMA34_10M_Last);
def EMA50_10M_Value = HighestAll(EMA50_10M_Last);
def EMA200_10M_Value = HighestAll(EMA200_10M_Last);

# 30-minute EMAs
def EMA5_30M_Value = HighestAll(EMA5_30M_Last);
def EMA9_30M_Value = HighestAll(EMA9_30M_Last);
def EMA12_30M_Value = HighestAll(EMA12_30M_Last);
def EMA34_30M_Value = HighestAll(EMA34_30M_Last);
def EMA50_30M_Value = HighestAll(EMA50_30M_Last);
def EMA200_30M_Value = HighestAll(EMA200_30M_Last);

# 1-hour EMAs
def EMA5_1H_Value = HighestAll(EMA5_1H_Last);
def EMA9_1H_Value = HighestAll(EMA9_1H_Last);
def EMA12_1H_Value = HighestAll(EMA12_1H_Last);
def EMA34_1H_Value = HighestAll(EMA34_1H_Last);
def EMA50_1H_Value = HighestAll(EMA50_1H_Last);
def EMA200_1H_Value = HighestAll(EMA200_1H_Last);

# 4-hour EMAs
def EMA5_4H_Value = HighestAll(EMA5_4H_Last);
def EMA9_4H_Value = HighestAll(EMA9_4H_Last);
def EMA12_4H_Value = HighestAll(EMA12_4H_Last);
def EMA34_4H_Value = HighestAll(EMA34_4H_Last);
def EMA50_4H_Value = HighestAll(EMA50_4H_Last);
def EMA200_4H_Value = HighestAll(EMA200_4H_Last);

# Daily EMAs
def EMA5_Daily_Value = HighestAll(EMA5_Daily_Last);
def EMA9_Daily_Value = HighestAll(EMA9_Daily_Last);
def EMA12_Daily_Value = HighestAll(EMA12_Daily_Last);
def EMA34_Daily_Value = HighestAll(EMA34_Daily_Last);
def EMA50_Daily_Value = HighestAll(EMA50_Daily_Last);
def EMA200_Daily_Value = HighestAll(EMA200_Daily_Last);

# Plot EMAs across all bars
# 10-minute EMAs
plot EMA5_10M = EMA5_10M_Value;
EMA5_10M.SetDefaultColor(Color.LIME);
EMA5_10M.SetStyle(Curve.FIRM);
EMA5_10M.SetLineWeight(1);

plot EMA9_10M = EMA9_10M_Value;
EMA9_10M.SetDefaultColor(Color.GREEN);
EMA9_10M.SetStyle(Curve.FIRM);
EMA9_10M.SetLineWeight(1);

plot EMA12_10M = EMA12_10M_Value;
EMA12_10M.SetDefaultColor(Color.LIGHT_GREEN);
EMA12_10M.SetStyle(Curve.FIRM);
EMA12_10M.SetLineWeight(1);

plot EMA34_10M = EMA34_10M_Value;
EMA34_10M.SetDefaultColor(Color.DARK_GREEN);
EMA34_10M.SetStyle(Curve.FIRM);
EMA34_10M.SetLineWeight(1);

plot EMA50_10M = EMA50_10M_Value;
EMA50_10M.SetDefaultColor(Color.DARK_RED);
EMA50_10M.SetStyle(Curve.FIRM);
EMA50_10M.SetLineWeight(1);

plot EMA200_10M = EMA200_10M_Value;
EMA200_10M.SetDefaultColor(Color.RED);
EMA200_10M.SetStyle(Curve.FIRM);
EMA200_10M.SetLineWeight(1);

# 30-minute EMAs
plot EMA5_30M = EMA5_30M_Value;
EMA5_30M.SetDefaultColor(Color.CYAN);
EMA5_30M.SetStyle(Curve.FIRM);
EMA5_30M.SetLineWeight(1);

plot EMA9_30M = EMA9_30M_Value;
EMA9_30M.SetDefaultColor(Color.CYAN);
EMA9_30M.SetStyle(Curve.FIRM);
EMA9_30M.SetLineWeight(1);

plot EMA12_30M = EMA12_30M_Value;
EMA12_30M.SetDefaultColor(Color.YELLOW);
EMA12_30M.SetStyle(Curve.FIRM);
EMA12_30M.SetLineWeight(1);

plot EMA34_30M = EMA34_30M_Value;
EMA34_30M.SetDefaultColor(Color.LIGHT_ORANGE);
EMA34_30M.SetStyle(Curve.FIRM);
EMA34_30M.SetLineWeight(1);

plot EMA50_30M = EMA50_30M_Value;
EMA50_30M.SetDefaultColor(Color.ORANGE);
EMA50_30M.SetStyle(Curve.FIRM);
EMA50_30M.SetLineWeight(1);

plot EMA200_30M = EMA200_30M_Value;
EMA200_30M.SetDefaultColor(Color.DARK_ORANGE);
EMA200_30M.SetStyle(Curve.FIRM);
EMA200_30M.SetLineWeight(1);

# 1-hour EMAs
plot EMA5_1H = EMA5_1H_Value;
EMA5_1H.SetDefaultColor(Color.LIME);
EMA5_1H.SetStyle(Curve.FIRM);
EMA5_1H.SetLineWeight(1);

plot EMA9_1H = EMA9_1H_Value;
EMA9_1H.SetDefaultColor(Color.PINK);
EMA9_1H.SetStyle(Curve.FIRM);
EMA9_1H.SetLineWeight(1);

plot EMA12_1H = EMA12_1H_Value;
EMA12_1H.SetDefaultColor(Color.MAGENTA);
EMA12_1H.SetStyle(Curve.FIRM);
EMA12_1H.SetLineWeight(1);

plot EMA34_1H = EMA34_1H_Value;
EMA34_1H.SetDefaultColor(Color.VIOLET);
EMA34_1H.SetStyle(Curve.FIRM);
EMA34_1H.SetLineWeight(1);

plot EMA50_1H = EMA50_1H_Value;
EMA50_1H.SetDefaultColor(Color.BLUE);
EMA50_1H.SetStyle(Curve.FIRM);
EMA50_1H.SetLineWeight(1);

plot EMA200_1H = EMA200_1H_Value;
EMA200_1H.SetDefaultColor(Color.BLUE);
EMA200_1H.SetStyle(Curve.FIRM);
EMA200_1H.SetLineWeight(1);

# 4-hour EMAs
plot EMA5_4H = EMA5_4H_Value;
EMA5_4H.SetDefaultColor(Color.CYAN);
EMA5_4H.SetStyle(Curve.FIRM);
EMA5_4H.SetLineWeight(2);

plot EMA9_4H = EMA9_4H_Value;
EMA9_4H.SetDefaultColor(Color.YELLOW);
EMA9_4H.SetStyle(Curve.FIRM);
EMA9_4H.SetLineWeight(2);

plot EMA12_4H = EMA12_4H_Value;
EMA12_4H.SetDefaultColor(Color.ORANGE);
EMA12_4H.SetStyle(Curve.FIRM);
EMA12_4H.SetLineWeight(2);

plot EMA34_4H = EMA34_4H_Value;
EMA34_4H.SetDefaultColor(Color.LIGHT_ORANGE);
EMA34_4H.SetStyle(Curve.FIRM);
EMA34_4H.SetLineWeight(2);

plot EMA50_4H = EMA50_4H_Value;
EMA50_4H.SetDefaultColor(Color.MAGENTA);
EMA50_4H.SetStyle(Curve.FIRM);
EMA50_4H.SetLineWeight(2);

plot EMA200_4H = EMA200_4H_Value;
EMA200_4H.SetDefaultColor(Color.BLUE);
EMA200_4H.SetStyle(Curve.FIRM);
EMA200_4H.SetLineWeight(2);

# Daily EMAs
plot EMA5_Daily = EMA5_Daily_Value;
EMA5_Daily.SetDefaultColor(Color.LIGHT_GRAY);
EMA5_Daily.SetStyle(Curve.FIRM);
EMA5_Daily.SetLineWeight(3);

plot EMA9_Daily = EMA9_Daily_Value;
EMA9_Daily.SetDefaultColor(Color.GRAY);
EMA9_Daily.SetStyle(Curve.FIRM);
EMA9_Daily.SetLineWeight(3);

plot EMA12_Daily = EMA12_Daily_Value;
EMA12_Daily.SetDefaultColor(Color.DARK_GRAY);
EMA12_Daily.SetStyle(Curve.FIRM);
EMA12_Daily.SetLineWeight(3);

plot EMA34_Daily = EMA34_Daily_Value;
EMA34_Daily.SetDefaultColor(Color.LIGHT_RED);
EMA34_Daily.SetStyle(Curve.FIRM);
EMA34_Daily.SetLineWeight(3);

plot EMA50_Daily = EMA50_Daily_Value;
EMA50_Daily.SetDefaultColor(Color.RED);
EMA50_Daily.SetStyle(Curve.FIRM);
EMA50_Daily.SetLineWeight(3);

plot EMA200_Daily = EMA200_Daily_Value;
EMA200_Daily.SetDefaultColor(Color.DARK_RED);
EMA200_Daily.SetStyle(Curve.FIRM);
EMA200_Daily.SetLineWeight(3);

# Chart bubbles for last known EMA values
def bubbleBar = BarNumber() == lastBarNumber + 10;
AddChartBubble(bubbleBar, EMA5_10M_Value, "10M/5", Color.LIME, yes);
AddChartBubble(bubbleBar, EMA9_10M_Value, "10M/9", Color.GREEN, yes);
AddChartBubble(bubbleBar, EMA12_10M_Value, "10M/12", Color.LIGHT_GREEN, yes);
AddChartBubble(bubbleBar, EMA34_10M_Value, "10M/34", Color.DARK_GREEN, yes);
AddChartBubble(bubbleBar, EMA50_10M_Value, "10M/50", Color.DARK_RED, yes);
AddChartBubble(bubbleBar, EMA200_10M_Value, "10M/200", Color.RED, yes);
AddChartBubble(bubbleBar, EMA5_30M_Value, "30M/5", Color.CYAN, yes);
AddChartBubble(bubbleBar, EMA9_30M_Value, "30M/9", Color.CYAN, yes);
AddChartBubble(bubbleBar, EMA12_30M_Value, "30M/12", Color.YELLOW, yes);
AddChartBubble(bubbleBar, EMA34_30M_Value, "30M/34", Color.LIGHT_ORANGE, yes);
AddChartBubble(bubbleBar, EMA50_30M_Value, "30M/50", Color.ORANGE, yes);
AddChartBubble(bubbleBar, EMA200_30M_Value, "30M/200", Color.DARK_ORANGE, yes);
AddChartBubble(bubbleBar, EMA5_1H_Value, "1H/5", Color.LIME, yes);
AddChartBubble(bubbleBar, EMA9_1H_Value, "1H/9", Color.PINK, yes);
AddChartBubble(bubbleBar, EMA12_1H_Value, "1H/12", Color.MAGENTA, yes);
AddChartBubble(bubbleBar, EMA34_1H_Value, "1H/34", Color.VIOLET, yes);
AddChartBubble(bubbleBar, EMA50_1H_Value, "1H/50", Color.BLUE, yes);
AddChartBubble(bubbleBar, EMA200_1H_Value, "1H/200", Color.BLUE, yes);
AddChartBubble(bubbleBar, EMA5_4H_Value, "4H/5", Color.CYAN, yes);
AddChartBubble(bubbleBar, EMA9_4H_Value, "4H/9", Color.YELLOW, yes);
AddChartBubble(bubbleBar, EMA12_4H_Value, "4H/12", Color.ORANGE, yes);
AddChartBubble(bubbleBar, EMA34_4H_Value, "4H/34", Color.LIGHT_ORANGE, yes);
AddChartBubble(bubbleBar, EMA50_4H_Value, "4H/50", Color.MAGENTA, yes);
AddChartBubble(bubbleBar, EMA200_4H_Value, "4H/200", Color.BLUE, yes);
AddChartBubble(bubbleBar, EMA5_Daily_Value, "D/5", Color.LIGHT_GRAY, yes);
AddChartBubble(bubbleBar, EMA9_Daily_Value, "D/9", Color.GRAY, yes);
AddChartBubble(bubbleBar, EMA12_Daily_Value, "D/12", Color.DARK_GRAY, yes);
AddChartBubble(bubbleBar, EMA34_Daily_Value, "D/34", Color.LIGHT_RED, yes);
AddChartBubble(bubbleBar, EMA50_Daily_Value, "D/50", Color.RED, yes);
AddChartBubble(bubbleBar, EMA200_Daily_Value, "D/200", Color.DARK_RED, yes);
 
Last edited by a moderator:

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
665 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