EMA multi timeframe chart label?

9:1Trading

New member
Ok, so what I'm trying to do may seem self evident, but I'll explain anyway.

I'm trying to add chart labels for when the EMA 8 is above the EMA 50 on the following Time Frames;
Month
Week
Day
4 Hr
1 Hr

The following code works, but it's either all green or all red. I know that's not right, but I can't figure out how to fix it.

I'd also like to add a chart indicator (arrow up) when the 4 hr is false and crosses to true.

I'm sorry if that's not explained well. Any assistance would be amazing. Thanks in advance!
Code:
def monthlytrend = if (expAverage(close, 8) * aggregationPeriod.MONTH > expaverage(close, 50) * aggregationPeriod.MONTH)then 1 else 0;
def weeklytrend = if(MovAvgExponential(close, 8) * aggregationPeriod.WEEK > expaverage (close, 50) * aggregationPeriod.WEEK)then 1 else 0;
def dailytrend = if (MovAvgExponential(close, 8) * aggregationPeriod.DAY > expaverage (close, 50) * aggregationPeriod.DAY) then 1 else 0;
def trend4 = if (MovAvgExponential(close, 8) * aggregationPeriod.FOUR_hours > expaverage (close, 50) * aggregationPeriod.FOUR_hours) then 1 else 0;
def trend1 = if (MovAvgExponential(close, 8) * aggregationPeriod.hour > expaverage (close, 50) * aggregationPeriod.HOUR) then 1 else 0;

AddLabel(yes, if monthlytrend is true then "M" else "", if monthlytrend is true then Color.dark_Green else color.DARK_RED);
AddLabel(yes, if weeklytrend is true then "W" else "", if weeklytrend is true then Color.dark_Green else Color.dark_Red);
AddLabel(yes, if dailytrend is true then "D" else "", if dailytrend is true then Color.dark_Green else Color.dark_Red);
AddLabel(yes, if trend4 is true then "4" else "", if trend4 is true then Color.dark_Green else Color.dark_Red);
AddLabel(yes, if trend1 is true then "1" else "", if trend1 is true then Color.dark_Green else Color.dark_Red);
 
Last edited by a moderator:

horserider

Well-known member
VIP
@9:1Trading Possibly ????? Not tested at all.


def aggM = AggregationPeriod.MONTH;
def pricem = close(period = aggM);

def monthlytrend = if ExpAverage (pricem, 8) > ExpAverage(pricem, 50) then 1 else 0;
AddLabel(yes, "M", if monthlytrend is true then ( Color.DARK_GREEN) else Color.DARK_RED);
 

9:1Trading

New member
Thanks for the quick help. It appears to be using the last true statement and applying it to all of them.

Individually, it works, but when I combine them, it get confused.

For the sake of brevity, I eliminated the 4 hr and 1 hr and inserted the 2 hr.
Code:
def aggM = AggregationPeriod.MONTH;
def pricem = close(period = aggM);
def monthlytrend = if ExpAverage (pricem, 8) > ExpAverage(pricem, 50) then 1 else 0;
AddLabel(yes, "M", if monthlytrend is true then (Color.DARK_GREEN) else Color.DARK_RED);

def aggW = AggregationPeriod.WEEK;
def pricew = close(period = aggW);
def weeklytrend = if ExpAverage (pricew, 8) > ExpAverage(pricew, 50) then 1 else 0;
AddLabel(yes, "W", if weeklytrend is true then (Color.DARK_GREEN) else Color.DARK_RED);

def aggD = AggregationPeriod.DAY;
def priced = close(period = aggD);
def daillytrend = if ExpAverage (priced, 8) > ExpAverage(priced, 50) then 1 else 0;
AddLabel(yes, "D", if daillytrend is true then  (Color.DARK_GREEN) else Color.DARK_RED);

def agg2 = AggregationPeriod.TWO_HOURS;
def price2 = close(period = agg2);
def trend2 = if ExpAverage (price2, 8) > ExpAverage(price2, 50) then 1 else 0;
AddLabel(yes, "2", if trend2 is true then  (Color.DARK_GREEN) else Color.DARK_RED);

With this one on ROKU the aggM and aggW should be Green and aggD and agg2 should be red, but they're all red. I know I'm doing something wrong, but idk what...
 
Last edited by a moderator:

Pensar

Well-known member
VIP
Lifetime
@yman Something like this?
Code:
def c1 = close(period = aggregationperiod.min);
def c2 = close(period = aggregationperiod.five_min);
def c3 = close(period = aggregationperiod.fifteen_min);

def avg1 = average(c1,20);
def avg2 = average(c2,20);
def avg3 = average(c3,20);

addlabel(1, if close > avg1 then "1 min above 20 SMA" else "1 min below 20 SMA",
         if close > avg1 then color.green else color.red);
addlabel(1, if close > avg2 then "5 min above 20 SMA" else "5 min below 20 SMA",
         if close > avg2 then color.green else color.red);
addlabel(1, if close > avg3 then "15 min above 20 SMA" else "15 min below 20 SMA",
         if close > avg3 then color.green else color.red);
 
@yman Something like this?
Code:
def c1 = close(period = aggregationperiod.min);
def c2 = close(period = aggregationperiod.five_min);
def c3 = close(period = aggregationperiod.fifteen_min);

def avg1 = average(c1,20);
def avg2 = average(c2,20);
def avg3 = average(c3,20);

addlabel(1, if close > avg1 then "1 min above 20 SMA" else "1 min below 20 SMA",
         if close > avg1 then color.green else color.red);
addlabel(1, if close > avg2 then "5 min above 20 SMA" else "5 min below 20 SMA",
         if close > avg2 then color.green else color.red);
addlabel(1, if close > avg3 then "15 min above 20 SMA" else "15 min below 20 SMA",
         if close > avg3 then color.green else color.red);
Hi @Pensar, thank you very much for the awesome code. However, the labels only show up on 1 min chart but not on any other time frames like 3 or 5 min or 10 min. Any suggestions?
 

Pensar

Well-known member
VIP
Lifetime
Hi @Pensar, thank you very much for the awesome code. However, the labels only show up on 1 min chart but not on any other time frames like 3 or 5 min or 10 min. Any suggestions?
@binhvesting You would have to change the hard-coded aggregation periods. Since only higher aggregations may be used on lower aggregation charts and not vice versa, it will only display on a 1 minute chart.
 

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